the computational graph needed to compute the outputs报错解决

theano是老古董了,但是鉴于既然碰到了、解决了,那就记录下来吧,方便后来者.
环境:
ubuntu18.10
jupyter3.6(终端运行下面的代码的话,会无法显示图形的)

#---------------------------------------------------

完整的故障代码如下:

import theano
import numpy as np
import theano.tensor as T
from IPython.display import Image
from IPython.display import display


W = T.dmatrix('W')
V = T.dmatrix('V')
x = T.dvector('x')
#-------------------------------------------------------
f = T.dot(x,W)
VJ = T.Lop(f,W,V)
result= theano.function([V,W,x], VJ)
display(Image(theano.printing.pydotprint(result,return_image=True, var_with_name_simple=True)))

报错如下:

UnusedInputError: theano.function was asked to create 
a function computing outputs given certain inputs, 
but the provided input variable at index 1 is not part of 
the computational graph needed to compute the outputs: W.

----------------------------------------------------解决方案1------------------------------------------------------------------
直接修改目标函数,让f对W求导后仍然有W的存在

import theano
import numpy as np
import theano.tensor as T
from IPython.display import Image
from IPython.display import display
W = T.dmatrix('W')
V = T.dmatrix('V')
x = T.dvector('x')
#-------------------------------------------------------
f = T.dot(x,W*W)
VJ = T.Lop(f,W,V)
result= theano.function([V,W,x], VJ)
display(Image(theano.printing.pydotprint(result,return_image=True, var_with_name_simple=True)))

运行结果如下(这个图就是上面报错信息中提到的graph):
在这里插入图片描述
----------------------------------------------------解决方案2------------------------------------------------------------------
不要输入W,因为 f = x ⋅ w f=x·w f=xw对w求导后,w就没用了,所以去掉theano.function后面的括号中的W

import theano
import numpy as np
import theano.tensor as T
from IPython.display import Image
from IPython.display import display


W = T.dmatrix('W')
V = T.dmatrix('V')
x = T.dvector('x')
#-------------------------------------------------------
f = T.dot(x,W)
VJ = T.Lop(f,W,V)
result= theano.function([V,x], VJ)
display(Image(theano.printing.pydotprint(result,return_image=True, var_with_name_simple=True)))
# display(Image(theano.printing.pydotprint(jvp,   return_image=True, var_with_name_simple=True)))

运行结果如下:
在这里插入图片描述

#########################################################################
好了,说下这个报错怎么回事,大意是:
f = x ⋅ w f=x·w f=xw,
f ′ = x f'=x f=x
也就是说求导后w不再被需要了,在上面的graph中没有了,所以再输入就报错了.

有的人会反驳,上面的例子是Lop,如果改成Rop的话,theano.function中填入W为什么不报错呢?
这个只能理解为theano的优化问题了 ,可以参考:
https://stackoverflow.com/questions/35422047/matrix-value-not-needed-for-lop
theano已经停止更新了,没必要再深究,解决问题就好.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值