Theano TensorType error

1.6k views Asked by At

When I am using nolearn to implement multi-label classification, I got this error:

'Bad input argument to theano function with name "/Users/lm/Documents/anaconda/lib/python2.7/site-packages/nolearn/lasagne/base.p‌​y:391" at index 1(0-based)', 'TensorType(float32, matrix) cannot store a value of dtype int64 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".', array([[0, 0, 0, ..., 0, 0, 1],

2

There are 2 answers

0
Tshilidzi Mudau On

In my case all I did was change the floatX flag (under [global]) to on the .theanorc file from :

[global]
floatX = float64

to:

[global]
floatX = float32

Notice that the 64 at the end was replaced by the 32.

0
P. Camilleri On

As told in the error message, you need to convert your input and output to the appropriate type (if you do not fear losing precision).

input = input.astype(np.float32)
output = output.astype(np.float32)

should work

Note: even if you do this, the error might remain if you have a BatchIterator which transforms your data (and by inadvertance uses float64 again). The solution is the same: inside the BatchIterator, cast the data to float32 right before returning it.