我找到了一个实现神经网络的程序。但是给出了一个错误。
x=np.array(([3,5], [5, 1], [10, 2]), dtype=float)
y=np.array(([75], [82],[93]), dtype=float)
x=x/np.amax(x, axis=0)
y=y/100
print(x)
class NeuralNetwork(object):
def _init_(self):
#Define Hyperparameters
self.inputLayerSize=2
self.hiddenLayerSize=3
self.outputLayerSize=1
#weight Parameters
self.w1=np.random.randn(self.inputLayerSize, self.hiddenLayerSize)
self.w2=np.random.randn(self.hiddenLayerSize, self.outputLayerSize)
def forwardPropagation(self,X):
self.z2=np.dot(X, self.w1)
self.a2=self.sigmoid(self.z2)
self.z3=np.dot(self.a2, self.w2)
yHat=self.sigmoid(self.z3)
return yHat
def sigmoid(self,z):
return 1/(1+np.exp(-z))
NN=NeuralNetwork()
YHat=NN.forwardPropagation(x)
Error:
AttributeError Traceback (most recent call last)
<ipython-input-83-b2ae5338432a> in <module>()
1 NN=NeuralNetwork()
----> 2 YHat=NN.forwardPropagation(x)
3 #testInput=np.arange(-6,6,0.01)
4 #plt.plot(testInput, NN.sigmoid(testInput), linewidth=2)
5 #plt.show()
<ipython-input-82-5a9b5641f6b6> in forwardPropagation(self, X)
13
14 def forwardPropagation(self,X):
---> 15 self.z2=np.dot(X, self.w1)
16 self.a2=self.sigmoid(self.z2)
17 self.z3=np.dot(self.a2, self.w2)
AttributeError: 'NeuralNetwork' object has no attribute 'w1'
答案1
你的 init 写错了。它应该是双下划线,例如__init__