Theano tutorial Error: A Real Example: Logistic Regression giving 露TensorType(float32, matrix) cannot store a value of dtype float64 error

Created on 15 Feb 2016  路  5Comments  路  Source: Theano/Theano

Continuing on my quest to try the tutorial code, in A Real Example: Logistic Regression section of:
http://deeplearning.net/software/theano/tutorial/examples.html

Line 46 is "updates=((w, w - 0.1 * gw), (b, b - 0.1 * gb)))"

I get:
/home/paul/anaconda2/bin/python /home/paul/PycharmProjects/theano/babysteps.py
Using gpu device 0: GeForce GTX TITAN X (CNMeM is disabled)
Initial model:
[ -8.02277167e-01 2.53127694e+00 -1.90313675e-02 -1.94322864e-02
-4.36142983e-01 1.10588365e+00 -7.82309924e-01 2.04402100e+00
8.13440701e-01 -2.12165081e-01 -6.09025927e-01 4.33268854e-02
... (I deleted the middle data)
6.62826085e-01 1.38587548e+00 -1.78021162e+00 -4.82619739e-01
8.70103597e-01 -8.23913747e-01 -2.32737178e-02 9.44344276e-01
-8.61341619e-02 -1.10208577e-01 -1.00235892e+00 -9.88523968e-01
1.06682718e+00 6.45284096e-01 -8.89386194e-01 -1.91487302e+00]
0.0
Traceback (most recent call last):
File "/home/paul/PycharmProjects/theano/babysteps.py", line 51, in
pred, err = train(D[0], D[1])
File "/home/paul/anaconda2/lib/python2.7/site-packages/theano/compile/function_module.py", line 786, in call
allow_downcast=s.allow_downcast)
File "/home/paul/anaconda2/lib/python2.7/site-packages/theano/tensor/type.py", line 139, in filter
raise TypeError(err_msg, data)
TypeError: ('Bad input argument to theano function with name "/home/paul/PycharmProjects/theano/babysteps.py:46" 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".', array([[-0.01857429, 1.22857714, -0.48929571, ..., -1.94223939,
0.23996558, 0.4519743 ],
[-0.13008184, 0.11168626, -0.16731811, ..., 0.25794437,
2.95469348, -0.44313368],
[ 1.47581277, -0.58837768, 1.14549575, ..., -1.35833589,
0.88407607, 0.01507807],
...,
[ 0.80721067, -1.53967922, -1.54502727, ..., 0.86132203,
1.40398551, 0.50810689],
[ 0.65062272, 0.52819684, -0.31719044, ..., -0.31490942,
0.75725309, -0.53901438],
[-0.08787195, 0.65129177, 1.05209097, ..., 1.52410757,
-1.33422687, 0.02812553]]))

Process finished with exit code 1

Most helpful comment

The error message indicates that x and y are of type float32, but the values you give them (from D) are float64. w and b are float64.
As @nouiz said, that example is meant to be run with the default options. If you want to use floatX=float32 to run it, the solution is in the error message: either

  1. cast the components of D to float32, or
  2. pass allow_input_downcast=True to the call to theano.function().

In both cases, you will probably want to cast the initial value of b and w so they are float32 as well.
Another option would be to explicitly use float64 everywhere, by declaring x = T.dmatrix('x') and y = T.dvector('y').

All 5 comments

I get the same error. The weird thing is that the error message seems to indicate that b and the elements in w are of type float32, but when I write

print(w.type)
print(b.type)

I get

TensorType(float64, vector)
TensorType(float64, scalar)

This example is made to work with floatX=float64. We didn't introduce floatX at this time.

The error message indicates that x and y are of type float32, but the values you give them (from D) are float64. w and b are float64.
As @nouiz said, that example is meant to be run with the default options. If you want to use floatX=float32 to run it, the solution is in the error message: either

  1. cast the components of D to float32, or
  2. pass allow_input_downcast=True to the call to theano.function().

In both cases, you will probably want to cast the initial value of b and w so they are float32 as well.
Another option would be to explicitly use float64 everywhere, by declaring x = T.dmatrix('x') and y = T.dvector('y').

Could somehow Theano be made to provide information about which assignment it is that causes the error?

Currently, the error message refers to the last row of a functions call, while in reality the assignment that triggers the error is a couple of lines above, although still in the same function call. As a newcomer to Theano, this is confusing/misleading, since it seems to indicate that it is in the last row of the function call that the error is triggered (since the number of that row is given in the error message), which is wrong.

Theano knows that the variables to which the values of D[0] and D[1] are assigned have the labels "x" and "y", right? In that case, it would help if those labels where printed in the error message.

The way the stack trace is reported only depends on Python, there is not much Theano can do about that.
You have a point about using the name of Variables if available, rather than simply "at index 0(0-based)", I created #4445 about it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

junku901 picture junku901  路  4Comments

dnikolayev picture dnikolayev  路  8Comments

subzerofun picture subzerofun  路  8Comments

grayfall picture grayfall  路  3Comments

dawidjk picture dawidjk  路  5Comments