我正在创建一个将图像发送到服务器的应用程序,服务器将通过神经网络运行图像,并输出结果。 这是我正在使用的神经网络类:
class Network(object):
def __init__(self, sizes):
self.num_layer = len(sizes)
self.sizes = sizes
self.biases = [np.random.randn(y,1) for y in sizes [1:]]
self.weights = [np.random.randn(y,x) for x, y in zip(sizes[:-1], sizes[1:])]
def feedforward(self, a):
for b, w in zip(self.biases, self.weights):
a = sigmoid(np.dot(w, a) + b)
return a
def SGD(self, training_data, epochs, mini_batch_size, eta, test_data = None):
list_training_data = list(training_data)
if test_data:
list_test_Data = list(test_data)
n_test = len(list_test_Data)
n = len(list_training_data)
for j in range(epochs):
random.shuffle(list_training_data)
mini_batches = [list_training_data[k:k+mini_batch_size]for k in range(0,n,mini_batch_size)]
for mini_batch in mini_batches:
self.update_mini_batch(mini_batch,eta)
if test_data:
evulate = self.evaluate(list_test_Data);
print("Epoch{0}:{1}/{2}" .format( j, evulate, n_test))
else:
print("Epoch {0} complete". format(j))
def backdrop(self, x, y):
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
activation = x
activations = [x]
zs = []
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation) + b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
delta = self.cost_derivative(activations[-1], y) * \
sigmoid_prime(zs[-1])
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-1-1].transpose())
return (nabla_b, nabla_w)
def update_mini_batch(self, mini_batch, eta):
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
for x, y in mini_batch:
delta_nabla_b, delta_nabla_w = self.backdrop(x, y)
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights = [w-(eta/len(mini_batch))*nw
for w ,nw in zip(self.weights, nabla_w)]
self.biases = [b - (eta/len(mini_batch))*nb
for b, nb in zip(self.biases, nabla_b)]
def evaluate(self, test_data):
test_results = [(np.argmax(self.feedforward(x)), y)for (x, y) in test_data]
final = sum(int(x==y)for (x,y) in test_results)
return final
def cost_derivative(self, output_activatoins, y):
return (output_activatoins - y)
def sigmoid(z):
return 1.0/(1.0 + np.exp(-z))
def sigmoid_prime(z):
return sigmoid(z)*(1-sigmoid(z))
我从numpy得到这个错误,我不知道如何解决这个问题。
C:/Users/name/Desktop/server.py:91: RuntimeWarning: underflow encountered in exp
np.getter()
ERROR:__main__:Exception on / [POST]
Traceback (most recent call last):
File "C:\Users\name\Downloads\WinPython-64bit-3.4.3.7\python-3.4.3.amd64\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\name\Downloads\WinPython-64bit-3.4.3.7\python-3.4.3.amd64\lib\site-packages\flask\app.py", line 1478, in full_dispatch_request
response = self.make_response(rv)
File "C:\Users\name\Downloads\WinPython-64bit-3.4.3.7\python-3.4.3.amd64\lib\site-packages\flask\app.py", line 1577, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "C:\Users\name\Downloads\WinPython-64bit-3.4.3.7\python-3.4.3.amd64\lib\site-packages\werkzeug\wrappers.py", line 847, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "C:\Users\name\Downloads\WinPython-64bit-3.4.3.7\python-3.4.3.amd64\lib\site-packages\werkzeug\test.py", line 871, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'numpy.int64' object is not callable
这是发生错误的行:
def sigmoid(z):
np.seterr(over='ignore')
return 1.0/(1.0 + np.exp(-z))
奇怪的是,当我运行神经网络时,我得到了正确的结果。当我通过服务器运行时,我得到TypeError: 'numpy.int64' object is not callable
答案 0 :(得分:1)
使用此Sigmoid实现来防止sigmoid上溢和下溢。
def sigmoid_function( signal, derivative=False ):
# Prevent overflow.
signal = np.clip( signal, -500, 500 )
# Calculate activation signal
signal = 1.0 / (1 + np.exp( -signal ))
if derivative:
# Return the partial derivation of the activation function
return np.multiply(signal, 1-signal)
else:
# Return the activation signal
return signal
#end activation function
我们无法帮助您解决此错误,因为您尚未发布完整代码。但是,当您输入错字并尝试将数字变量作为函数调用时,通常会发生该错误。最后一个问题更适合Code Review StackExchange。
答案 1 :(得分:1)
事实证明代码没有任何问题。我收到此错误消息的原因是因为服务器没有以JSON类型发送结果。为了解决这个错误,我从Flask导入了jsonify,并写了return jsonify(results = answer)