This is what is called list comprehension . You can create the same effect if you use the usual for
loop:
self.weights = [] for x, y in zip(sizes[:-1], sizes[1:]): self.weights.append(np.random.randn(y, x))
Now with this loop, you can see that self.weights
is actually just a bunch of np.random.randn(y, x)
, where y
and x
defined for each x
and y
in zip(sizes[:-1], sizes[1:])
. You can just say it to yourself when you read the list comprehension: self.weights = [np.random.randn(y, x)) for x, y in zip(sizes[:-1], sizes[1:])]
. The word order finally makes sense. If you did not know, zip is a function that returns a list of tuples of each corresponding element in its arguments. For example, zip([1, 2, 3, 4], [4, 3, 2, 1])
will return [(1, 4), (2, 3), (3, 2), (4, 1)]
. (In Python3, this is actually a tuple generator)
zondo source share