为什么在尝试更新共享变量时会遇到Theano TypeError?

问题描述:

我想在函数y = x^2上运行一个非常简单的渐变下降。 我试着用下面的代码实现它:为什么在尝试更新共享变量时会遇到Theano TypeError?

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.') 

我想这可能是一个问题与learning_rate变量,因为它可能不是同一类型的共享变量X,但如果我修改代码如下:

updates = [(x, x - dy_dx)] 

我仍然得到同样的错误。

我坚持:(任何想法?

的问题是,你的共享变量x没有规定这样一个类型被推断出来的。既然你提供的价值是一个Python整数文字,类型假设为int32。这是一个问题,因为渐变不适用于整数,因此dy_dx实际上是float64。这又使得更新值为float64。共享变量只能用相同类型的值更新(这是错误消息),所以你有一个问题:共享变量是int32,但更新是float64

一个解决方案是使共享变量也是浮动的。这可以通过简单地将小数点添加到初始值x来实现。

x = theano.shared(2.)