'float'对象不可调用

时间:2018-02-08 12:53:23

标签: python numpy machine-learning neural-network

我坚持错误:

  Traceback (most recent call last):
  File "neural_network.py", line 239, in <module>
    demo()
  File "neural_network.py", line 227, in demo
    NN.train(X)
  File "neural_network.py", line 168, in train
    error += self.backPropagate(targets)
  File "neural_network.py", line 133, in backPropagate
    change = output_deltas * np.reshape(self.ah(int), (self.ah.shape[0],1),dtype=int)
TypeError: 'float' object is not callable

我在神经网络的backPropagate函数中出现此错误

   # update the weights connecting hidden to output
    change = output_deltas * np.reshape(self.ah, (self.ah.shape[0],1),dtype=int)
            self.wo -= self.learning_rate * change + self.co * self.momentum 
            self.co = change 

    # update the weights connecting input to hidden
    change = hidden_deltas * np.reshape(self.ai, (self.ai.shape[0], 1),dtype=int)
            self.wi -= self.learning_rate * change + self.ci * self.momentum 
            self.ci = change

我用来实现数字识别的MLP_NeuralNetwork类看起来像这样有两个主要函数feedForward和backPropagate,其中feedForword工作正常但在backPropogate函数中遇到问题:

class MLP_NeuralNetwork(object):
    def __init__(self, input, hidden, output, iterations, learning_rate, momentum, rate_decay):
        # initialize arrays
        self.input = input + 1 # add 1 for bias node
        self.hidden = hidden
        self.output = output

        # set up array of 1s for activations
        self.ai = np.ones(self.input)
        self.ah = np.ones(self.hidden)
        self.ao = np.ones(self.output)
        input_range = 1.0 / self.input ** (1/2)
        self.wi = np.random.normal(loc = 0, scale = input_range, size = (self.input, self.hidden))
        self.wo = np.random.uniform(size = (self.hidden, self.output)) / np.sqrt(self.hidden)
        self.ci = np.zeros((self.input, self.hidden))
        self.co = np.zeros((self.hidden, self.output))


    def feedForward(self, inputs):
        if len(inputs) != self.input-1:
            raise ValueError('Wrong number of inputs you silly goose!')
        self.ai[0:self.input -1] = inputs
        # hidden activations
        Z = np.dot(self.wi.T, self.ai)
        sum=np.add.reduce(Z)
        self.ah = tanh(sum)

        # output activations
        Z=np.dot(self.wo.T, self.ah)
        sum = np.add.reduce(Z)
        self.ao = sigmoid(sum)

        return self.ao


    def backPropagate(self, targets):
        if len(targets) != self.output:
        raise ValueError('Wrong number of targets you silly goose!')
        # calculate error terms for output
        # the delta tell you which direction to change the weights
        output_deltas = dsigmoid(self.ao) * -(targets - self.ao)

        # calculate error terms for hidden
        # delta tells you which direction to change the weights
        error = np.dot(self.wo, output_deltas)
        hidden_deltas = dtanh(self.ah) * error

        # update the weights connecting hidden to output
        change = output_deltas * np.reshape(self.ah, (self.ah.shape[0],1))
        self.wo -= self.learning_rate * change + self.co * self.momentum 
        self.co = change 

        # update the weights connecting input to hidden
        change = hidden_deltas * np.reshape(self.ai, (self.ai.shape[0], 1))
        self.wi -= self.learning_rate * change + self.ci * self.momentum 
        self.ci = change

        # calculate error
        error = sum(0.5 * (targets - self.ao)**2)

        return error

错误我是:

Traceback (most recent call last):
  File "neural_network.py", line 240, in <module>
    demo()
  File "neural_network.py", line 228, in demo
    NN.train(X)
  File "neural_network.py", line 169, in train
    error += self.backPropagate(targets)
  File "neural_network.py", line 134, in backPropagate
    change = output_deltas * np.reshape(self.ah, (self.ah.shape[0],1))
AttributeError: 'float' object has no attribute 'shape'

由于此错误,我无法继续前进。任何帮助,将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果self.ah是一个浮点数或numpy数组,你可能试图用参数int来调用它:

change = output_deltas * np.reshape(self.ah(int), (self.ah.shape[0],1),dtype=int)

由于self.ah不是您可以像这样调用的对象,因此您可能会收到错误。

此外,如果您在某个地方缺少数学运算符,也可能导致此错误(例如x (y+z)而不是x*(y+z))。