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

与世无争的帅哥 提交于 2019-11-26 20:45:36

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=xwf=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=xwf=x·w,
f=xf'=x
也就是说求导后w不再被需要了,在上面的graph中没有了,所以再输入就报错了.

有的人会反驳,上面的例子是Lop,如果改成Rop的话,theano.function中填入W为什么不报错呢?
这个只能理解为theano的优化问题了 .
theano已经停止更新了,没必要再深究,解决问题就好.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!