Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theano: Get matrix dimension and value of matrix (SharedVariable)

Tags:

python

theano

I would like to know how to retrieve the dimension of a SharedVariable from theano.

This here e.g. 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 only returns

()
(1, 1)

I am also interested in printing/retriving the values of a matrix or vector ..

like image 219
Stefan Falk Avatar asked Mar 22 '14 15:03

Stefan Falk


1 Answers

You can get the value of a shared variable like this:

w.get_value()

Then this would work:

w.get_value().shape

But this will copy the shared variable content. To remove the copy you can use the borrow parameter like this:

w.get_value(borrow=True).shape

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

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

Their is a simpler way to do this, compile a Theano function that return the shape:

w.shape.eval()

w.shape return a symbolic variable. .eval() will compile a Theano function and return the value of shape.

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

like image 118
nouiz Avatar answered Nov 13 '22 02:11

nouiz