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=x⋅w对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=x⋅w,
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已经停止更新了,没必要再深究,解决问题就好.