python - 如何修复:(ipython)类名定义和名称错误:名称未定义
问题描述
我正在尝试构建可以识别数字(0~9)的nuerualnetwork
但由于某些错误,它不起作用。
所以我该怎么做?
当我尝试构建该代码时,会出现此错误消息。
---------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-bdb6f33f1c38> in <module>
5 get_ipython().run_line_magic('matplotlib', 'inline')
6
----> 7 class neuralNetwork:
8 def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
9 self.inodes = inputnodes
<ipython-input-3-bdb6f33f1c38> in neuralNetwork()
56 learing_rate = 0.1
57
---> 58 n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)
59
60 training_data_file = open("C:/ProgramData/Anaconda3/pkgs/notebook-6.0.0-py37_0/Lib/site-packages/notebook/mnist/mnist_trian.csv", 'r')
NameError: name 'neuralNetwork' is not defined
导入 numpy 导入 scipy.special 导入 matplotlib.pyplot
%matplotlib 内联
类神经网络:def init(self,inputnodes,hiddennodes,outputnodes,learningrate):self.inodes = inputnodes self.hnodes = hiddennodes self.onodes = outputnodes self.lr =learningrate
self.wih = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes))
self.who = numpy.random.normal(0.0, pow(self.onondes, -0.5), (self.onodes, self,hnodes))
self.activation_function = lambda x: scipy.special.expit(x)
pass
def train(self, inputs_list, targets_list):
inputs = numpy.array(inputs_list, ndmin=2).T
tagets = numpy.array(targets_list, ndmin=2).T
hidden_inputs = numpy.dot(self.wih, inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = numpy.array(self.who, hidden_outputs)
final_outputs = self.activation_function(final_inputs)
output_errors = targets - final_outputs
hidden_errors = numpy.dot(self.who.T, output_errors)
self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))
self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs))
pass
def query(self, inputs_list):
inputs = numpy.array(inputs_list, ndmin=2).T
hidden_inputs = numpy.dot(self.wih, inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = numpy.array(self.who, hidden_outputs)
final_outputs = self.activation_function(final_inputs)
return final_outputs
input_nodes = 784
hidden_nodes = 200
output_nodes = 10
learing_rate = 0.1
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)
training_data_file = open("C:/ProgramData/Anaconda3/pkgs/notebook-6.0.0-py37_0/Lib/site-packages/notebook/mnist/mnist_trian.csv", 'r')
training_data_list = training_data_file.readlines()
traning_data_file.close()
epochs = 5
for e in range(epochs):
for record in training_data_list:
all_values = record.split(',')
inputs - (numpy.asfarray(all_values[1:])/255.0 * 0.99) + 0.01
targets = numpy.zeros(output_nodes)+0.01
targets[int(all_values[0])] = 0.99
n.train(inputs, targets)
pass
pass
test_data_file = open("C:/ProgramData/Anaconda3/pkgs/notebook-6.0.0-py37_0/Lib/site-packages/notebook/mnist/mnist_test.csv")
test_data_list = test_data_file.readlines()
test_data_file.close()
scorecard = []
for record in test_data_list:
all_vales - record.split(',')
correct_label = int(all_values[0])
inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
outputs - n.query(inputs)
label = numpy.argmax(outputs)
if(lavel == correct_label):
scorecard.append(1)
else :
scorecard.append(0)
pass
pass
scorecard_array = numpy.asarray(scorecard) print("performance = ", scorecard_array.sum() / scorecard_array.size)
解决方案
您正在尝试在类中初始化您的神经网络。确保你的缩进是正确的,并确保你用正确的值初始化你的类(所以不要简单地复制参数名称)
推荐阅读
- ruby-on-rails - 不断收到“nil:NilClass 的未定义方法‘电子邮件’”我做错了什么?
- python - Django 视图不包含 HTTP_REFERRER 标头
- docker - 在 Ubuntu docker 容器中崩溃时自动重启进程
- python-3.x - Python中类的加载方法
- tensorflow-lite - tensorflow lite 现在是否支持设备训练
- intellij-idea - IntelliJ idea 自动复制文件
- go - 如何摆脱自动完成后出现的烦人的文本框,其中 VSCode 的智能感知不起作用?
- json - 为什么这个承诺返回一个 [object Promise] 而不是值?
- image - 在 Postman 中发送带有原始选项的图像
- html - html "maxlength" 属性在应用于 chrome 时不计入逗号,但在 IE 中计算。为什么是这样?