我试图按照
上的例子http://neuralnetworksanddeeplearning.com/chap1.html
靠近页面底部。代码是从python 2.7编写的,我使用的是3.5。在尝试运行代码时,我收到以下错误消息
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/lukasz/Documents/Machine Learning/network.py", line 65, in SGD
random.shuffle(training_data)
File "/home/lukasz/anaconda3/lib/python3.5/random.py", line 269, in shuffle
for i in reversed(range(1, len(x))):
TypeError: object of type 'zip' has no len()
SGD
函数的代码是
def SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None):
if test_data:
n_test = sum(1 for _ in test_data)
n = sum(1 for _ in training_data)
for j in range(epochs):
random.shuffle(training_data)
mini_batches = [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:
print("Epoch %s: %s / %s"
%(j, self.evaluate(test_data), n_test))
else:
print("Epoch %s complete" %(j))
在导入mnist数据并执行后运行:
net = network.Network([784, 30, 10])
net.SGD(training_data, 30, 10, 3.0, test_data=test_data)
我试图创建一个索引并随机播放,但我没有运气得到正确的输出。我特定部分的代码如下:
def SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None):
if test_data:
n_test = sum(1 for _ in test_data)
n = sum(1 for _ in training_data)
for j in range(epochs):
indices = np.arange(n) #### This Is Modified
random.shuffle(indices) #### This Is Modified
mini_batches = [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:
print("Epoch %s: %s / %s"
%(j, self.evaluate(test_data), n_test))
else:
print("Epoch %s complete" %(j))
产生:
Epoch 0: 0 / 0
Epoch 1: 0 / 0
Epoch 2: 0 / 0
Epoch 3: 0 / 0
Epoch 4: 0 / 0
Epoch 5: 0 / 0
Epoch 6: 0 / 0
Epoch 7: 0 / 0
Epoch 8: 0 / 0
Epoch 9: 0 / 0
Epoch 10: 0 / 0
...
/////////////////////////////////////////////// //////////////////////
这个例子的解决方案由@Nicolas Turgeon发布,可以在这里找到: