Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python strange syntaxis

I have been learning about ANN but the book I'm reading has examples in Python. The problem is that I have never written in Python and these lines of code are too hard for me to understand:

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

I read some things about it and found out that the randn() function returns an array with y elements and x dimensions populated with random numbers between 0 and 1. zip() connects two arrays into one. sizes[:-1] returns the last element and sizes[1:] return the array without its first element.

But with all of this I still can't explain to myself what would this generate.

like image 294
Rokner Avatar asked Dec 06 '25 01:12

Rokner


2 Answers

sizes[:-1] will return the sublist [3,2] (that is, all the elements except the last one).

sizes[1:] will return the sublist [2,4] (that is, all the elements except the first one).

zip([a,b], [c,d]) gives [(a,c), (b,d)].

So zipping the two lists above gives you [(3,2), (2,4)]

The construction of weights is a list comprehension. Therefore this code is equivalent to

weights = []

for x,y in [(3,2), (2,4)]:
       weights.append(np.random.randn(y, x))

So the final result would be the same as

[ np.random.randn(2,3), 
  np.random.randn(4,2) ]
like image 66
Chad S. Avatar answered Dec 08 '25 15:12

Chad S.


Let's break this up into chunks:


self.weights = [some junk]

is going to be a list comprehension. Meaning, do the some junk stuff and you'll end up with a list of elements from that. Usually these look like so:

self.weights = [some_func(x) for x in a_list]

This is the equivalent of:

self.weights = []
for x in a_list:
    self.weights.append(some_func(x))

zip(a, b)

Will piecewise combine the elements of a and b into tuple pairs:

(a1, b1), (a2, b2), (a3, b3), ...

for x, y in zip(a, b):

This iterates through that tuple pairs talked about above


sizes[:-1]

This is stating to get all the elements of list sizes except the last item (-1).

sizes[1:]

This is stating to get the all the elements of list sizes except the first item.


So, finally piecing this all together you get:

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

Which is a list comprehension iterating through the tuple pairs of first sizes from 2nd item to last and second from 1st item to next to last, create a random number based on those two parameters, then append to a list that is stored as self.weights

like image 31
James Mertz Avatar answered Dec 08 '25 15:12

James Mertz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!