我试图在函数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)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译函数"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.')
Run Code Online (Sandbox Code Playgroud)
我认为这可能是learning_rate变量的问题,因为它可能与x共享变量的类型不同,但如果我修改代码如下:
updates = [(x, x - dy_dx)]
Run Code Online (Sandbox Code Playgroud)
我仍然得到同样的错误.
我被卡住了:(有什么想法吗?
问题是您的共享变量x没有指定类型,因此推断出一个.由于您提供的值是Python整数文字,因此假定类型为int32.这是一个问题,因为渐变不能很好地与整数一起使用,因此dy_dx实际上是一个float64.这反过来也使更新值float64.共享变量只能使用相同类型的值进行更新(这是错误消息),因此您遇到了问题:共享变量是一个int32但更新是一个float64.
一种解决方案是使共享变量也是浮点数.这可以通过简单地将小数点添加到初始值来实现x.
x = theano.shared(2.)
Run Code Online (Sandbox Code Playgroud)