Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: ('An update must have the same type as the original shared variable #1

Closed
StatML opened this issue Jul 1, 2017 · 14 comments

Comments

@StatML
Copy link

StatML commented Jul 1, 2017

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]})


TypeError Traceback (most recent call last)
in ()
4 updates=[(w, w - 0.1 * gw), (b, b - 0.1 * gb)],
5 givens = {x : train_X[index * batch_size : (index + 1) * batch_size],
----> 6 y : train_Y[index * batch_size : (index + 1) * batch_size]})

/usr/local/lib/python2.7/dist-packages/Theano-0.10.0.dev1-py2.7.egg/theano/compile/function.pyc in function(inputs, outputs, mode, updates, givens, no_default_updates, accept_inplace, name, rebuild_strict, allow_input_downcast, profile, on_unused_input)
324 on_unused_input=on_unused_input,
325 profile=profile,
--> 326 output_keys=output_keys)
327 # We need to add the flag check_aliased inputs if we have any mutable or
328 # borrowed used defined inputs

/usr/local/lib/python2.7/dist-packages/Theano-0.10.0.dev1-py2.7.egg/theano/compile/pfunc.pyc in pfunc(params, outputs, mode, updates, givens, no_default_updates, accept_inplace, name, rebuild_strict, allow_input_downcast, profile, on_unused_input, output_keys)
447 rebuild_strict=rebuild_strict,
448 copy_inputs_over=True,
--> 449 no_default_updates=no_default_updates)
450 # extracting the arguments
451 input_variables, cloned_extended_outputs, other_stuff = output_vars

/usr/local/lib/python2.7/dist-packages/Theano-0.10.0.dev1-py2.7.egg/theano/compile/pfunc.pyc in rebuild_collect_shared(outputs, inputs, replace, updates, rebuild_strict, copy_inputs_over, no_default_updates)
206 ' function to remove broadcastable dimensions.')
207
--> 208 raise TypeError(err_msg, err_sug)
209 assert update_val.type == store_into.type
210

TypeError: ('An update must have the same type as the original shared variable (shared_var=b, shared_var.type=TensorType(int64, scalar), update_val=Elemwise{sub,no_inplace}.0, update_val.type=TensorType(float64, scalar)).', 'If the difference is related to the broadcast pattern, you can call the tensor.unbroadcast(var, axis_to_unbroadcast[, ...]) function to remove broadcastable dimensions.')

@innovation-cat
Copy link
Owner

innovation-cat commented Jul 26, 2017 via email

@StatML
Copy link
Author

StatML commented Aug 4, 2017

@innovation-cat 您好!这段代码来自您那本书的第29页2.3.2节Logistic回归。请问该如何修正这个错误呢?

@innovation-cat
Copy link
Owner

innovation-cat commented Aug 4, 2017 via email

@StatML
Copy link
Author

StatML commented Aug 4, 2017

@innovation-cat 您好!第29页的完整代码如下

import numpy as np
import theano
import theano.tensor as T

N, 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)

batch_size = 100
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.5

loss = -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]}
)

@innovation-cat
Copy link
Owner

innovation-cat commented Aug 4, 2017

这个运行后并没有报错误

@StatML
Copy link
Author

StatML commented Aug 4, 2017

Hi @innovation-cat 我刚又用Python3.5运行了一遍,依然有错误提示。

/usr/bin/python3.5 demo_01.py
Using cuDNN version 6021 on context None
Mapped name None to device cuda0: GeForce GTX 970 (0000:01:00.0)
Traceback (most recent call last):
File "/home/xxx/demo_01.py", line 33, 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).

Process finished with exit code 1

@innovation-cat
Copy link
Owner

我分别在python2.7+theano0.8和python3.6+theano0.9两个环境运行过,代码并没有报错,我看了你用的是theano0.10dev,这个版本还是一个测试版本,估计起码明年才正式发布,试试安装0.8或者0.9,不要用0.10,这个版本可能还不稳定

@StatML
Copy link
Author

StatML commented Aug 7, 2017

Hi @innovation-cat 谢谢!我看到最后一句提示说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). 请问下这个提示是针对哪条语句的?为什么会有这样的提示?我Theano不太熟,请问这个类型转换应怎么写?

@innovation-cat
Copy link
Owner

innovation-cat commented Aug 7, 2017 via email

@StatML
Copy link
Author

StatML commented Aug 7, 2017

Hi @innovation-cat 加上allow_input_downcast=True后,仍有错误提示,而且提示内容没有变化。请问下

Type TensorType(int64, vector) (of Variable Subtensor{int32:int32:}.0) into Type TensorType(int32, vector)

这句是由哪个参数引起的?TensorType(int64, vector) (of Variable Subtensor{int32:int32:}.0)具体是指哪个变量?另外为什么会需要转换成 TensorType(int32, vector)?怎么用cast实现这个转换?谢谢!

@innovation-cat
Copy link
Owner

innovation-cat commented Aug 7, 2017 via email

@StatML
Copy link
Author

StatML commented Aug 8, 2017

Hi @innovation-cat 谢谢您的回复。请问下TensorType(int64, vector) (of Variable Subtensor{int32:int32:}.0)具体是指y : train_Y[index * batch_size : (index + 1) * batch_size]}中的哪个变量?如果是y的话,我在y的定义语句之后加上y=T.cast(y, 'int32')也不起作用。

@innovation-cat
Copy link
Owner

不是说y,而是说train_Y[index * batch_size : (index + 1) * batch_size]的结果是TensorType(int64, vector),但y的定义是TensorType(int32, vector),所以报了这个类型错误,不过这里面 train_Y[index * batch_size : (index + 1) * batch_size]的结果应该也是TensorType(int32, vector),所以应该是版本的bug,不是你的代码有bug

@StatML
Copy link
Author

StatML commented Aug 8, 2017

@innovation-cat 是版本bug的说法有道理,我之前把train_Y的定义修改为train_Y = theano.shared(value=Y, allow_downcast=True)也还是会报这个错误,谢谢!

@StatML StatML closed this as completed Aug 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants