![](/img/trans.png)
[英]When I send a numpy array to a givens parameter in a theano function, why do I get this Theano TypeError
[英]Why do I get a Theano TypeError when trying to update a shared variable?
我試圖在函數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.')
我認為這可能是learning_rate變量的問題,因為它可能與x共享變量的類型不同,但如果我修改代碼如下:
updates = [(x, x - dy_dx)]
我仍然得到同樣的錯誤。
我被卡住了:(有什么想法嗎?
問題是你的共享變量x
沒有指定類型,因此推斷出一個。 由於您提供的值是Python整數文字,因此假定類型為int32
。 這是一個問題,因為漸變不能很好地處理整數,所以dy_dx
實際上是一個float64
。 這反過來又使更新值為float64
。 共享變量只能使用相同類型的值進行更新(這是錯誤消息),因此您遇到了問題:共享變量是int32
但更新是float64
。
一種解決方案是使共享變量也是浮點數。 這可以通過簡單地將小數點添加到x
的初始值來實現。
x = theano.shared(2.)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.