当我尝试更新共享变量时,为什么会出现Theano TypeError?

4

我正在尝试对函数y=x^2运行一个非常简单的梯度下降算法。 我已经尝试使用以下代码实现:

import theano
from theano import tensor as T
x = theano.shared(2)
y = x ** 2

dy_dx = T.grad(y, x)
learning_rate = 1
updates = [(x, x - learning_rate * dy_dx)]
fn = theano.function([], [y], updates = updates)

但当我尝试编译函数“fn”时,会出现以下错误:
TypeError: ('An update must have the same type as the original shared 
variable (shared_var=<TensorType(int64, scalar)>, 
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.')

我认为这可能是由于学习率变量的问题,因为它可能与共享变量x的类型不同,但如果我按照以下方式修改代码:

updates = [(x, x - dy_dx)]

我仍然遇到相同的错误。 我卡住了 :( 有什么想法吗?
1个回答

7
问题是您的共享变量 x 没有指定类型,因此推断出了一个类型。由于提供的值是 Python 整数文字,所以假定其类型为 int32。这是个问题,因为梯度不适用于整数,因此 dy_dx 实际上是一个 float64。这反过来又使更新值成为一个 float64。共享变量只能使用相同类型的值进行更新(这就是错误消息),因此您有一个问题:共享变量是一个 int32,但更新是一个 float64
其中一个解决方案是将共享变量也变为浮点型。可以通过在 x 的初始值中添加小数点来实现这一点。
x = theano.shared(2.)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接