Hi, I am using Theano 0.10.0.dev1 which was built from source on Ubuntu 16.04, Python3.5.
When I compile the following code fragment, I met a type error which does not occur in Theano 0.8 or 0.9.
Maybe it is a bug of the dev version? Hope it could be fixed soon. Thx!
import numpy as np
import theano
import theano.tensor as TN, M = 1000, 78
X = np.random.randn(N, M)
Y = np.random.randint(low=0, high=2, size=N)train_X = theano.shared(value=X)
train_Y = theano.shared(value=Y, allow_downcast=True)batch_size = 100
print(type(batch_size))
w_value = np.random.randn(M)w = theano.shared(value=w_value, name='w')
b = theano.shared(value=0.0, name='b')x = T.dmatrix('x')
y = T.ivector('y')index = T.iscalar('index')
output = 1.0 / (1.0 + T.exp(-T.dot(x, w)-b))
predict = output > 0.5loss = -y * T.log(output) - (1-y)*T.log(1-output)
cost = T.mean(loss)+0.001(w*2).sum()
gw, gb = T.grad(cost, [w, b])train = theano.function(
inputs=[index],
outputs=cost,
updates=[(w, w-0.1 * gw), (b, b - 0.1 * gb)],
givens={x: train_X[index * batch_size: (index + 1) * batch_size],
y: train_Y[index * batch_size: (index + 1) * batch_size]})
Traceback (most recent call last):
File "/home/xxx/demo_01.py", line 37, in
y: train_Y[index * batch_size: (index + 1) * batch_size]})
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/compile/function.py", line 325, in function
output_keys=output_keys)
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/compile/pfunc.py", line 449, in pfunc
no_default_updates=no_default_updates)
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/compile/pfunc.py", line 219, in rebuild_collect_shared
cloned_v = clone_v_get_shared_updates(v, copy_inputs_over)
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
clone_v_get_shared_updates(i, copy_inputs_over)
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
clone_v_get_shared_updates(i, copy_inputs_over)
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
clone_v_get_shared_updates(i, copy_inputs_over)
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
clone_v_get_shared_updates(i, copy_inputs_over)
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
clone_v_get_shared_updates(i, copy_inputs_over)
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/compile/pfunc.py", line 96, in clone_v_get_shared_updates
[clone_d[i] for i in owner.inputs], strict=rebuild_strict)
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/gof/graph.py", line 239, in clone_with_new_inputs
new_inputs[i] = curr.type.filter_variable(new)
File "/usr/local/lib/python3.5/dist-packages/Theano-0.10.0.dev1-py3.5.egg/theano/tensor/type.py", line 234, in filter_variable
self=self))
TypeError: Cannot convert Type TensorType(int64, vector) (of Variable Subtensor{int32:int32:}.0) into Type TensorType(int32, vector). You can try to manually convert Subtensor{int32:int32:}.0 into a TensorType(int32, vector).
This is not a problem in Theano, but in your code. This could happen depending of the python version (32 bit python vs 64 bit python) for example. The problem is that you not sure that those 2 variables have the same dtype:
Y = np.random.randint(low=0, high=2, size=N)
index = T.iscalar('index')
Y here will have the dtype given by numpy. Currently, it is int64. but index is forced to have dtype int32.
You should decide if you want int32 or int64 and use one of those version that will force the same dtype for both variable on all python version:
Y = np.random.randint(low=0, high=2, size=N, dtype='int32')
index = T.iscalar('index')
or
Y = np.random.randint(low=0, high=2, size=N, dtype='int64')
index = T.lscalar('index')
Hi @nouiz Many thanks for your clear explanation! :+1:
Most helpful comment
This is not a problem in Theano, but in your code. This could happen depending of the python version (32 bit python vs 64 bit python) for example. The problem is that you not sure that those 2 variables have the same dtype:
Y here will have the dtype given by numpy. Currently, it is int64. but index is forced to have dtype int32.
You should decide if you want int32 or int64 and use one of those version that will force the same dtype for both variable on all python version:
or