保存和加载 theano 变量
Saving and Loading theano variables
我正在使用 theano
变量来定义一个函数。然后我保存变量。当我加载变量时 theano
无法将其识别为之前使用的同一变量。
此代码:
import theano
import theano.tensor as T
from theano.misc.pkl_utils import dump,load
x = T.scalar('x')
y = x**2
f = file('temp','wb')
dump(x,f)
f.close()
del x
f = file('temp','rb')
x=load(f)
f.close()
F=theano.function([x],y)
print F(2)
产生错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/jpiabrantes/Desktop/untitled6.py", line 28, in <module>
F=theano.function([x],y)
File "C:\Anaconda2\lib\site-packages\theano\compile\function.py", line 320, in function
output_keys=output_keys)
File "C:\Anaconda2\lib\site-packages\theano\compile\pfunc.py", line 479, in pfunc
output_keys=output_keys)
File "C:\Anaconda2\lib\site-packages\theano\compile\function_module.py", line 1776, in orig_function
output_keys=output_keys).create(
File "C:\Anaconda2\lib\site-packages\theano\compile\function_module.py", line 1415, in __init__
self._check_unused_inputs(inputs, outputs, on_unused_input)
File "C:\Anaconda2\lib\site-packages\theano\compile\function_module.py", line 1553, in _check_unused_inputs
i.variable, err_msg))
theano.compile.function_module.UnusedInputError: theano.function was asked to create a function computing outputs given certain inputs, but the provided input variable at index 0 is not part of the computational graph needed to compute the outputs: x.
To make this error into a warning, you can pass the parameter on_unused_input='warn' to theano.function. To disable it completely, use on_unused_input='ignore'.
我也尝试 Save/Load 使用 cPickle
结果相同。
我认为你的输入是正确的,但是 theano 声称必须使用你的函数或设置另一个参数 'warn' 或者'ignore' 在你的函数中。
当您从 pickle 加载 x
时,您会得到 x
的 副本,而不是原始 x
。 Pickle 是一种序列化协议。就其性质而言,无法保证对象的同一性。
您还必须考虑到 pickle 旨在用于 dump/load 跨不同 Python 进程的数据,而不是在同一进程中(我看不出在您的代码中使用 pickle 有任何好处).
你应该做的是转储和加载 x
连同 y
:
dump((x, y), f)
x, y = load(f)
Pickle 可以保证转储的 x
比转储的 y
"linked"(因此,加载的 x
对转储的 "linked"已加载 y
).
我正在使用 theano
变量来定义一个函数。然后我保存变量。当我加载变量时 theano
无法将其识别为之前使用的同一变量。
此代码:
import theano
import theano.tensor as T
from theano.misc.pkl_utils import dump,load
x = T.scalar('x')
y = x**2
f = file('temp','wb')
dump(x,f)
f.close()
del x
f = file('temp','rb')
x=load(f)
f.close()
F=theano.function([x],y)
print F(2)
产生错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/jpiabrantes/Desktop/untitled6.py", line 28, in <module>
F=theano.function([x],y)
File "C:\Anaconda2\lib\site-packages\theano\compile\function.py", line 320, in function
output_keys=output_keys)
File "C:\Anaconda2\lib\site-packages\theano\compile\pfunc.py", line 479, in pfunc
output_keys=output_keys)
File "C:\Anaconda2\lib\site-packages\theano\compile\function_module.py", line 1776, in orig_function
output_keys=output_keys).create(
File "C:\Anaconda2\lib\site-packages\theano\compile\function_module.py", line 1415, in __init__
self._check_unused_inputs(inputs, outputs, on_unused_input)
File "C:\Anaconda2\lib\site-packages\theano\compile\function_module.py", line 1553, in _check_unused_inputs
i.variable, err_msg))
theano.compile.function_module.UnusedInputError: theano.function was asked to create a function computing outputs given certain inputs, but the provided input variable at index 0 is not part of the computational graph needed to compute the outputs: x.
To make this error into a warning, you can pass the parameter on_unused_input='warn' to theano.function. To disable it completely, use on_unused_input='ignore'.
我也尝试 Save/Load 使用 cPickle
结果相同。
我认为你的输入是正确的,但是 theano 声称必须使用你的函数或设置另一个参数 'warn' 或者'ignore' 在你的函数中。
当您从 pickle 加载 x
时,您会得到 x
的 副本,而不是原始 x
。 Pickle 是一种序列化协议。就其性质而言,无法保证对象的同一性。
您还必须考虑到 pickle 旨在用于 dump/load 跨不同 Python 进程的数据,而不是在同一进程中(我看不出在您的代码中使用 pickle 有任何好处).
你应该做的是转储和加载 x
连同 y
:
dump((x, y), f)
x, y = load(f)
Pickle 可以保证转储的 x
比转储的 y
"linked"(因此,加载的 x
对转储的 "linked"已加载 y
).