【发布时间】:2019-04-21 05:37:41
【问题描述】:
您我一直在尝试运行此脚本,但最后总是出现缩进错误 backprop(x,y) 函数。我真的很感激任何帮助!
import cPickle
import gzip
def load_data():
f = gzip.open('mnist.pkl.gz', 'rb')
training_data, validation_data, test_data = cPickle.load(f)
f.close()
return (training_data, validation_data, test_data)
import numpy as np
class Network(object):
def __init__(self, layers):
self.layers = layers
self.biases = [np.random.randn(y,1) for y
in layers[1:]]
self.weights = [np.transpose(np.random.randn(x,y))
for x,y
in zip(layers[:-1],layers[1:])]
self.num_layers = len(layers)
def backprop(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]
# feedforward
activation = x
activations = [x] # list to store all the activations, layer by layer
zs = [] # list to store all the z vectors, layer by layer
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation)+b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
# backward pass
delta = self.cost_derivative(activations[-1], y) * \
sigmoid_prime(zs[-1])#set first delta
nabla_b[-1] = delta#set last dC/db to delta vector
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
#calculate nabla_b, nabla_w for the rest of the layers
for l in xrange(2, self.num_layers):
z = zs[-l]
sp = sigmoid_prime(z)
delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
#this is where python says there is an indent error!
return (nabla_b, nabla_w)
【问题讨论】:
-
我已经选择了您的所有代码并在编辑器中按下了
{}按钮。这会将所选文本格式化为代码。请查看问题以确保它看起来像您的原始代码。您可以随时 edit 解决或改进您的问题。 -
尝试用
spaces替换tab。 -
@CrisLuengo 感谢您帮助我格式化代码;我对此有点麻烦......但我还是发布了它,哈哈。再次感谢哥们!
-
@HaBom 解决了问题!非常感谢!!
标签: python-2.7 image-processing neural-network