Problems on Python Code using Theano

163 views Asked by At

I am learning the Python Theano Library. I just encountered a block of code using Theano shown below:

import theano
import theano.tensor as T
from collections import OrderedDict
import numpy

params = OrderedDict()  ### params is a python ordered dictionary.
r = numpy.random.rand(10, 5)    ### Generate a random matrix with dimension of 10 * 5.
params['haha'] = (0.01 * r).astype('float32')   ### insert a key 'haha' with its value into the dictionary.

sparams = OrderedDict() ### sparams is another python ordered dictionary.
sparams['haha'] = theano.shared(params['haha'], name='haha')    ### Create Theano Shared Variable from the params.

x = T.matrix('x', dtype='int64') ### x is a theano tensor matrix

n1 = x.shape[0]     ### What does it mean?
n2 = x.shape[1]     ### What does it mean?

h = sparams['haha'][x.flatten()].reshape([n1, n2, 5])   ### What does it mean?  

My problem is I cannot understand this part of code:

n1 = x.shape[0]     ### What does it mean?
n2 = x.shape[1]     ### What does it mean?

h = sparams['haha'][x.flatten()].reshape([n1, n2, 5])   ### What does it mean? 

Also, I am wondering if there is any way in Python to check the values as well as dimensions of Theano tensor variables. I really appreciate if anyone can help me solve the problem.

1

There are 1 answers

1
gffbss On

According to the docs shape returns an lvector representing the shape of x. You can read more about that here

In the code block you are referencing n1 will return the object at index 0 and n2 will return the object at index 1.

You can read a bit more about lists in python here.

If you are running this script from the command line, you can use a print statement to see what is contained is those variables by adding a line like this:

n1 = x.shape[0] print n1