TypeError:“ numpy.ndarray”对象不可调用我该怎么办?

时间:2018-12-07 17:09:56

标签: python neural-network deep-learning backpropagation gradient-descent

我正在建立前馈神经网络,但不确定为什么它不起作用:  这是代码:

import numpy as np
import random 


#Hyperparameters
num_iterations = 100000
errors_shown = 1
num_iterations_per_error = num_iterations // errors_shown
learning_rate = 0.01

def nonlin(x, deriv=False):
    if deriv == True:
        return x * (1 - x)
    else:
        return 1 / (1 + np.exp(-x))
   #Input Data
x = np.array([[9, 2],
        [7, 0],
        [3, 1],
        [5, 0.5],
        [5, 0],
        [5.6, 0.7],
        [10, 0.5],
        [9, 0.5],
        [8, 3.7],
        [3, 0.5],
        [4, 2],
        [4, 2.5],
        [9, 2.5],
        [10, 0],
        [5, 1],
        [8, 3],
        [7, 1.7],
        [9, 0],
        [3, 3]])

#Output Data
y = np.array([[0.87],
            [0.12],
            [0.09],
            [0.12],
            [0.13],
            [0.47],
            [0.39],
            [0.36],
            [0.97],
            [0.19],
            [0.23],
            [0.67],
            [0.94],
            [0.33],
            [0.57],
            [0.91],
            [0.73],
            [0.63],
            [0.32]])

np.random.seed(1)

#Random synapses 
syn0 = np.random.random((2, 3))
syn1 = np.random.random((3, 4))
syn2 = np.random.random((4, 3))
syn3 = np.random.random((3, 2))
syn4 = np.random.random((2, 1))
syns = [syn0, syn1, syn2, syn3, syn4]

#Random bias'
b0 = np.random.random((1, 3))
b1 = np.random.random((1, 4))
b2 = np.random.random((1, 3))
b3 = np.random.random((1, 2))
b4 = np.random.random((1, 1))
bias = [b0, b1, b2, b3, b4]

#Neural Network

class NN:
    def __init__(self, input_data, expected_output_data, num_iterations, num_iterations_per_error, learning_rate, syns, bias):
        self.input_data = input_data
        self.expected_output_data = expected_output_data
        self.num_iterations = num_iterations
        self.num_iterations_per_error = num_iterations_per_error
        self.learning_rate = learning_rate
        self.syn0 = syns[0]
        self.syn1 = syns[1]
        self.syn2 = syns[2]
        self.syn3 = syns[3]
        self.syn4 = syns[4]
        self.b0 = bias[0]
        self.b1 = bias[1]
        self.b2 = bias[2]
        self.b3 = bias[3]
        self.b4 = bias[4]

#predict
def pred(self):
    self.l0 = self.input_data
    self.l1 = nonlin(np.dot(self.l0, self.syn0) + self.b0)
    self.l2 = nonlin(np.dot(self.l1, self.syn1) + self.b1)
    self.l3 = nonlin(np.dot(self.l2, self.syn2) + self.b2)
    self.l4 = nonlin(np.dot(self.l3, self.syn3) + self.b3)
    self.z = (np.dot(self.l4, self.syn4) + self.b4)

    self.pred = nonlin(self.z)

    return self.pred 

#Cost func
def cost_func(self):
    self.l4_error = np.square(self.l4 - self.expected_output_data)

    self.l4_delta = self.l4_error * nonlin(self.l4, deriv=True)

    self.l3_error = self.l4_delta.dot(self.syn3.T)

    self.l3_delta = self.l3_error * nonlin(self.l3, deriv=True)

    self.l2_error = self.l3_delta.dot(self.syn2.T)

    self.l2_delta = self.l2_error * nonlin(self.l2, deriv=True)

    self.l1_error = self.l2_delta.dot(self.syn1.T)

    self.l1_delta = self.l1_error * nonlin(self.l1, deriv=True)

    self.total_error += self.l4_error

    return self.l4_error

#Backprop 
def backprop(self):
    self.syn3 += self.l3.T.dot(self.l4_delta * self.learning_rate)
    self.syn2 += self.l2.T.dot(self.l3_delta * self.learning_rate)
    self.syn1 += self.l1.T.dot(self.l2_delta * self.learning_rate)
    self.syn0 += self.l0.T.dot(self.l1_delta * self.learning_rate)

def runner(self):
    self.total_error = 0

    for iteration in range(self.num_iterations): 
        pred = self.pred()
        cost = self.cost_func()
        self.backprop()

neuralNet = NN(x, y, num_iterations, num_iterations_per_error, learning_rate, syns, bias)

neuralNet.runner()

所以我不知道这是怎么回事,所以您能帮我吗?  这是错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-55-e63563ac83ed> in <module>
     72 neuralNet = NN(x, y, num_iterations, num_iterations_per_error, learning_rate, syns, bias)
     73 
---> 74 neuralNet.runner()

<ipython-input-55-e63563ac83ed> in runner(self)
     66 
     67         for iteration in range(self.num_iterations):
---> 68             pred = self.pred()
     69             cost = self.cost_func()
     70             self.backprop()

TypeError: 'numpy.ndarray' object is not callable

请仅在底部的东西中张贴您的答案 它在def pred中说是的,是的,请帮忙,它只是一个深层的前馈神经网络

1 个答案:

答案 0 :(得分:1)

问题出在功能self.pred中:

self.pred = nonlin(self.z)
return self.pred

函数self.pred用数字或ndarray替换自身,因为nonlin()返回。这意味着您只能调用一次self.pred,因为它在第一次运行时会自行擦除。相反,请不要使用函数名称来存储函数的结果,因为这样实际上就将静态结果替换为函数,而静态结果不能像函数一样调用。