我已经用以下简单的代码进行了练习
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
我不明白为什么?在NeuralNetwork()类内部,我已经完成了synaptic_weights的初始化,那么为什么类不能访问?谢谢?
答案 0 :(得分:2)
您的代码有两个问题:_init_
函数需要两个下划线。
第二,您可以使用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!")
答案 1 :(得分:1)
示例代码段:
class A:
def __init__(self):
self.test = 0
a = A()
print(a.test)
检查init方法的定义,方法名称应为__init__