Search code examples
numpytheanolasagnenolearn

Theano TypeError


I am reading jpg images and then reshaping them into a tensor. I am casting the images as float32:

def load(folder,table):
X=[]

train = pd.read_csv(table)

for i,img_id in enumerate(train['Image']):

    img = io.imread(folder+img_id[2:])

    X.append(img)

X = np.array(X)/255.
X = X.astype(np.float32)
X = X.reshape(-1, 1, 225, 225)
return X

However, I am getting this error

TypeError: ('Bad input argument to theano function with name "/Users/mas/PycharmProjects/Whale/nolearn_convnet/Zahraa5/lib/python2.7/site-packages/nolearn/lasagne/base.py:435"  at index 1(0-based)', 'TensorType(int32, vector) cannot store a value of dtype float32 without risking loss of precision. If you do not mind this loss, you can: 1) explicitly cast your data to int32, or 2) set "allow_input_downcast=True" when calling "function".', array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],

       [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32))

Solution

  • This is a cross-post to the theano-users mailing list.

    Doug provided an answer there:

    The theano variable you are using is defined as integer, but you passed in a float, hence the error 'TensorType(int32, vector) cannot store a value of dtype float32...'. You can either modify your data loading code to cast it as int32, or change the symbolic variable to something that supports float32.

    So somewhere you have a line that looks something like:

    x = T.ivector()
    

    or

    x = T.vector(dtype='int32')
    

    It looks like you need to change this to something like

    x = T.tensor4()
    

    where the dtype has been changed to equal theano.config.floatX and the dimensionality as been changed to 4 to match the 4-dimensional nature of X.