Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 保存和加载无变量_Python_Pickle_Theano - Fatal编程技术网

Python 保存和加载无变量

Python 保存和加载无变量,python,pickle,theano,Python,Pickle,Theano,我使用theano变量来定义函数。然后保存变量。当我加载变量时,no无法识别它与以前使用的变量相同 此代码: 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.

我使用
theano
变量来定义函数。然后保存变量。当我加载变量
时,no
无法识别它与以前使用的变量相同

此代码:

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'.
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Anaconda2\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第699行,在runfile中
execfile(文件名、命名空间)
文件“C:\Anaconda2\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第74行,在execfile中
exec(编译(脚本文本,文件名,'exec'),glob,loc)
文件“C:/Users/jpiabrants/Desktop/untitled6.py”,第28行,在
F=编号函数([x],y)
文件“C:\Anaconda2\lib\site packages\theano\compile\function.py”,第320行,在函数中
输出键=输出键)
pfunc中的文件“C:\Anaconda2\lib\site packages\theano\compile\pfunc.py”,第479行
输出键=输出键)
文件“C:\Anaconda2\lib\site packages\theano\compile\function\u module.py”,第1776行,在orig\u函数中
输出密钥=输出密钥)。创建(
文件“C:\Anaconda2\lib\site packages\theano\compile\function\u module.py”,第1415行,在\uu init中__
自我检查未使用的输入(输入、输出、开启未使用的输入)
文件“C:\Anaconda2\lib\site packages\theano\compile\function\u module.py”,第1553行,在未使用的输入中
i、 变量,err_msg))
theano.compile.function_module.unusedinputer错误:要求theano.function创建一个函数,在给定某些输入的情况下计算输出,但索引0处提供的输入变量不是计算输出所需的计算图的一部分:x。
要将此错误变成警告,可以将\u unused\u input='warn'上的参数传递给ano.函数。要完全禁用它,请使用on_unused_input='ignore'。

我还尝试使用
cPickle
保存/加载,结果相同。

我认为您的输入是正确的,但不要求必须使用您的函数或在函数中设置另一个参数“warn”或“ignore”。

我认为您的输入是正确的,但是不能声明您的函数必须使用或在函数中设置另一个参数“warn”或“ignore”。

当您从pickle加载
x
时,您会得到
x
的副本,而不是原始
x
。Pickle是一种序列化协议。就其性质而言,不能保证对象的身份是相同的

您还必须考虑泡菜是用来在不同的Python进程中转储/加载数据,而不是在同一个进程中(我在代码中使用泡菜没有任何好处)。 您应该做的是将

x
y
一起卸载和加载:

dump((x, y), f)
x, y = load(f)

Pickle可以保证转储的
x
被“链接”到转储的
y
(因此,加载的
x
被“链接”到加载的
y
)。

从Pickle加载
x
时,您会得到一份
x
,而不是原始的
x
。Pickle是一种序列化协议。就其性质而言,不能保证对象的身份是相同的

您还必须考虑泡菜是用来在不同的Python进程中转储/加载数据,而不是在同一个进程中(我在代码中使用泡菜没有任何好处)。 您应该做的是将

x
y
一起卸载和加载:

dump((x, y), f)
x, y = load(f)

Pickle可以保证转储的
x
被“链接”到转储的
y
(因此,加载的
x
被“链接”到加载的
y
)。因为他认为加载的x不同于用于定义y的x。Theano声称变量x不是计算y所必需的。因为他认为加载的x与用来定义y的x不同。我的代码只是为了举例说明我的问题。将变量转储并加载到一起解决了我的问题:)我的代码只是为了举例说明我的问题。将变量转储并加载到一起解决了我的问题:)