blue-sky blue-sky - 4 months ago 28
Python Question

Python beginner, understanding some code

Here is a Python representation of a Neural Network Neuron that I'm trying to understand

class Network(object):

def __init__(self, sizes):
self.num_layers = 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:])]


Here is my current understanding :


  • self.num_layers = len(sizes)
    : Return the number of items in sizes

  • self.sizes = sizes
    : assign self instance sizes to function parameter sizes

  • self.biases = sizes
    : generate an array of elements from the standard normal distribution (indicated by
    np.random.randn(y, 1)
    )



What is the following line computing?

self.weights = [np.random.randn(y, x)
for x, y in zip(sizes[:-1], sizes[1:])]


I'm new to Python. Can this code be used within a Python shell so I can gain a better understanding by invoking each line separately ?

Answer

The zip() function pairs up elements from each iterable; zip('foo', 'bar') for example, would produce [('f', 'b'), ('o', 'a'), ('o', 'r')]; each element in the two strings has been paired up into three new tuples.

zip(sizes[:-1], sizes[1:]) then, creates pairs of elements in the sequence sizes with the next element, because you pair up all elements except the last (sizes[:-1]) with all elements except the first (sizes[1:]). This pairs up the first and second element together, then the second and third, etc. all the way to the last two elements.

For each such pair a random sample is produced, using a list comprehension. So for each x, y pair, a new 2-dimensional numpy matrix is produced with random values divided over y rows and x columns.

Note that the biases value only uses sizes[1:], all but the first, to produce y-by-1 matrices for each such size.

Quick demo of these concepts:

>>> zip('foo', 'bar')
[('f', 'b'), ('o', 'a'), ('o', 'r')]
>>> zip('foo', 'bar', 'baz')  # you can add more sequences
[('f', 'b', 'b'), ('o', 'a', 'a'), ('o', 'r', 'z')]
>>> sizes = [5, 12, 18, 23, 42]
>>> zip(sizes[:-1], sizes[1:])  # a sliding window of pairs
[(5, 12), (12, 18), (18, 23), (23, 42)]
# 0, 1 ..  1,  2 ..  2,  3 ..  3,  4   element indices into sizes
>>>