Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error because of Theano and NumPy variable types

I am writing this code using numpy 1.9 and the latest version of Theano but I get an error which I can't fix. I doubt it could be the way I declare variable types but I can't work it around. I appreciate your suggestions. I want to product a matrix with a vector and sum it with a bias.

import theano.tensor as T
from theano import function
import numpy as np
import pprint
def test_theano_matrix():
   pp = pprint.PrettyPrinter(indent=3)
   W= T.fmatrix()
   x=T.fvector()
   b= T.fvector()
   y = T.dot(W,x) + b
   lin_func = function([W,x,b],y)
   dt = np.dtype(np.float)
   w_inp = np.matrix('1 0;0 1',dtype=dt)
   x_inp = np.matrix('2;1',dtype=dt)
   b_inp = np.matrix('0;0',dtype=dt)
   lin_func(w_inp,x_inp,b_inp)

 if __name__ == '__main__':
   test_theano_matrix()

I get the following error:

raise TypeError(err_msg, data)
TypeError: ('Bad input argument to theano function at index 0(0-based)',
'TensorType(float32, matrix) cannot store a value of dtype float64 without risking loss of precision. If you do not mind this loss, you can: 1) explicitly cast your data to float32, or 2) set "allow_input_downcast=True" when calling "function".', matrix([[ 1.,  0.],[ 0.,  1.]]))

Thanks for your time!

like image 340
Ehsan K. Mohammadi Avatar asked Apr 07 '14 14:04

Ehsan K. Mohammadi


3 Answers

I had a similar error and was able to resolve it by adding a .theanorc file containing the following two lines:

[global]

floatX = float32

That seemed to fix everything. However, your problem shows a slightly different error. But I think it's worth trying.

like image 164
idnavid Avatar answered Sep 21 '22 01:09

idnavid


This answer comes from Theano-users google group.

You define your x variable as:

x=T.vector(dtype=theano.config.floatX)

This is it is a vector(i.e. it only have 1 dimensions).

x_inp = np.matrix('2;1',dtype=dt)

create a matrix, not a vector.

Theano graph are strongly typed, you must defined the good number of dimensions. Just use:

x_inp = np.asarray([2,1]) 

I actually ended up defining x and b as matrices.

like image 43
Ehsan K. Mohammadi Avatar answered Sep 20 '22 01:09

Ehsan K. Mohammadi


Error looks quite self-explanatory; have you tried:

dt = np.dtype(np.float32) 

??

like image 1
John Greenall Avatar answered Sep 20 '22 01:09

John Greenall