Numpy Neural Network错误:'NeuralNetwork'对象没有属性'think'

时间:2017-08-18 12:02:25

标签: python numpy neural-network

我有兴趣学习一些机器学习,所以我开始点击。我开始按照说明创建此代码。

from numpy import exp, array, random, dot

class NeuralNetwork():
    def __init__(self):
        # seed the genarator
        random.seed(1)
        self.synaptic_weights = 2 * random.random((3,1)) - 1

    def __sigmoid(self, x):
        return 1 /(1 + exp(-x))

    def predict(self, inputs):
        return self.__sigmoid(dot(inputs, self.synaptic_weights))

    def __sigmoid_derivative(self, x):
        return x * (x - 1)

    def train(self, trainingSetInputs, trainingSetOutputs, numberOfIterations):
        for iteration in range(numberOfIterations):
            output = self.predict(trainingSetInputs)
            error = trainingSetOutputs - output
            adjustment = dot(trainingSetInputs.T, error * 
self.__sigmoid_derivative(output))
            self.synaptic_weights += adjustment





if __name__ == '__main__':
    # make one network
    neuralNetwork = NeuralNetwork()

    print('random starting synaptic weights')
    print(neuralNetwork.synaptic_weights)

    # training data
    trainingSetInputs = array([[0,0,1], [1,1,1], [1,0,1], [0,1,1]])
    trainingSetOutputs = array([[0,1,1,0]]).T

    #train the network 10000 times
    neuralNetwork.train(trainingSetInputs, trainingSetOutputs, 10000)

    print('new wheights')
    print(neuralNetwork.synaptic_weights)

    # test the network
    print("testing")
    print(neuralNetwork.think(array([1,0,1])))

我遵循了这封信的指示,但也许我错过了什么? 他的教程是here

编辑:我得到的错误是:'NeuralNetwork'对象没有属性'think'

1 个答案:

答案 0 :(得分:2)

您需要创建think()方法。看看你的代码应该是:

def think(self, inputs):
    #pass inputs through our single neuron(our single neuron)
    return self.___sigmoid(dot(inputs, self.synaptic_weights))

这样做,你就没事了!