Theano: get matrix size and matrix value (SharedVariable)

I would like to know how to get the SharedVariable size from theano.

This here, for example, does not work:

from theano import * from numpy import * import numpy as np w = shared( np.asarray(zeros((1000,1000)), np.float32) ) print np.asarray(w).shape print np.asmatrix(w).shape 

and returns only

 () (1, 1) 

I am also interested in printing / extracting matrix or vector values.

+6
source share
1 answer

You can get the value of a shared variable as follows:

 w.get_value() 

Then it will work:

 w.get_value().shape 

But this will copy the contents of the shared variable. To delete a copy, you can use the borrow option as follows:

 w.get_value(borrow=True).shape 

But if the shared variable is on the GPU, it will still copy the data from the GPU to the CPU. To not do this:

 w.get_value(borrow=True, return_internal_type=True).shape 

This is an easier way to do this, compile the Anano function, which returns the form:

 w.shape.eval() 

w.shape returns a symbolic variable..eval () compiles the Anano function and returns the form value.

If you want to know more about how Theano handles memory, check out this web page: http://www.deeplearning.net/software/theano/tutorial/aliasing.html

+14
source

All Articles