Python 神经网络类can';无法访问其属性
我已经练习了以下简单的代码Python 神经网络类can';无法访问其属性,python,Python,我已经练习了以下简单的代码 import numpy as np class NeuralNetwork(): def _init_(self): np.random.seed(1) # generating numbers between [-1 1] self.synaptic_weights = 2 * np.random.random((3, 1))-1 def sigmoid(self, x): r
import numpy as np
class NeuralNetwork():
def _init_(self):
np.random.seed(1)
# generating numbers between [-1 1]
self.synaptic_weights = 2 * np.random.random((3, 1))-1
def sigmoid(self, x):
return 1/(1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def think(self, inputs):
inputs=inputs.astype(float)
output = self.sigmoid(np.product(inputs,self.synaptic_weights))
return output
def train(self,training_inputs,training_outputs,training_iterations):
for iteration in range(training_iterations):
output = self.think(training_inputs)
error = training_outputs - output
adjustment =np.dot(training_inputs.T, error * self.sigmoid_derivative(output))
self.synaptic_weights += adjustment
if __name__ == "__main__":
# initializing the neuron class
neural_network = NeuralNetwork()
print("Beginning Randomly Generated Weights: ")
print(neural_network.synaptic_weights)
# training data consisting of 4 examples--3 input values and 1 output
training_inputs = np.array([[0, 0, 1],
[1, 1, 1],
[1, 0, 1],
[0, 1, 1]])
training_outputs = np.array([[0, 1, 1, 0]]).T
# training taking place
neural_network.train(training_inputs, training_outputs, 15000)
print("Ending Weights After Training: ")
print(neural_network.synaptic_weights)
user_input_one = str(input("User Input One: "))
user_input_two = str(input("User Input Two: "))
user_input_three = str(input("User Input Three: "))
print("Considering New Situation: ", user_input_one, user_input_two, user_input_three)
print("New Output data: ")
print(neural_network.think(np.array([user_input_one, user_input_two, user_input_three])))
print("Wow, we did it!")
但当我运行它时,它给了我以下错误:
Traceback (most recent call last):
Beginning Randomly Generated Weights:
File "I:/scientific_python/deep_learning_machine_learning/deep_learning.py", line 34, in <module>
print(neural_network.synaptic_weights)
AttributeError: 'NeuralNetwork' object has no attribute 'synaptic_weights'
Process finished with exit code 1
回溯(最近一次呼叫最后一次):
开始随机生成的权重:
文件“I:/scientific\u python/deep\u learning\u machine\u learning/deep\u learning.py”,第34行,在
打印(神经网络、突触权重)
AttributeError:“NeuralNetwork”对象没有属性“synaptic_权重”
进程已完成,退出代码为1
我不明白为什么?在NeuralNetwork()类中,我已经完成了synaptic_权重的初始化,所以为什么类无法访问?提前感谢示例代码片段:
class A:
def __init__(self):
self.test = 0
a = A()
print(a.test)
检查init方法的定义,方法名称应该是
\uuuu init\uuuu
您的代码有两个问题:\u init\uu
函数需要双下划线
其次,可以使用np.dot
代替np.product
将权重与输入矩阵相乘
以下是工作代码:
import numpy as np
class NeuralNetwork():
def __init__(self):
np.random.seed(1)
# generating numbers between [-1 1]
self.synaptic_weights = 2 * np.random.random((3, 1))-1
def sigmoid(self, x):
return 1/(1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def think(self, inputs):
inputs=inputs.astype(float)
output = self.sigmoid(np.dot(inputs,self.synaptic_weights))
return output
def train(self,training_inputs,training_outputs,training_iterations):
for iteration in range(training_iterations):
output = self.think(training_inputs)
error = training_outputs - output
adjustment =np.dot(training_inputs.T, error * self.sigmoid_derivative(output))
self.synaptic_weights += adjustment
if __name__ == "__main__":
# initializing the neuron class
neural_network = NeuralNetwork()
print("Beginning Randomly Generated Weights: ")
print(neural_network.synaptic_weights)
# training data consisting of 4 examples--3 input values and 1 output
training_inputs = np.array([[0, 0, 1],
[1, 1, 1],
[1, 0, 1],
[0, 1, 1]])
training_outputs = np.array([[0, 1, 1, 0]]).T
# training taking place
neural_network.train(training_inputs, training_outputs, 15000)
print("Ending Weights After Training: ")
print(neural_network.synaptic_weights)
user_input_one = str(input("User Input One: "))
user_input_two = str(input("User Input Two: "))
user_input_three = str(input("User Input Three: "))
print("Considering New Situation: ", user_input_one, user_input_two, user_input_three)
print("New Output data: ")
print(neural_network.think(np.array([user_input_one, user_input_two, user_input_three])))
print("Wow, we did it!")
我认为您缺少了
\u init\u
函数中的双下划线。谢谢,这个问题怎么样?TypeError:只有整数标量数组可以转换为标量索引关于这个问题,请检查@user2314737 answer为什么我应该使用点?@datodatuashvili您必须考虑:您的输入代表什么?由于是think
中的代码逐行执行矩阵乘法,也就是说,对于输入矩阵中的每一行,它会生成行中元素与权重列的线性组合。这个组合是一个数字。进行反向传播时,调整权重以获得更好的数字。因此基本上,输入
矩阵每行包含一个示例(一个示例用于训练网络=一维向量)。