-1

I'm attempting to follow the example on

http://neuralnetworksanddeeplearning.com/chap1.html

near the bottom of the page. The code was written fro python 2.7 and I'm using 3.5. While attempting to run the code I get following error message

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()

the code for the SGD function is

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))

which is run after import the mnist data and executing:

net = network.Network([784, 30, 10])
net.SGD(training_data, 30, 10, 3.0, test_data=test_data)

I've attempted to create an index and shuffle that, but I had no luck getting a proper output. My code for this particular section looks like:

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))

which produces:

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
   ...

/////////////////////////////////////////////////////////////////////

A solution for this example was posted by @Nicolas Turgeon and can be found here:

Python 2 --> 3: object of type 'zip' has no len()

Community
  • 1
  • 1
Lukasz
  • 2,476
  • 10
  • 41
  • 51

0 Answers0