将lambda函数转储到文件中并将其加载到另一个函数中

时间:2017-01-03 20:58:52

标签: python lambda pickle sympy dill

首先,这是我想要做的工作流程的大纲: 1.使用sympy来“做数学”并开发一些表达方式 2. Lambdify相应的表达式 3.将相应的lambda函数存储在文件中 4.将它们加载到我的代码的独立部分

一切顺利,直到第3步。 我尝试过不同的东西,特别是在阅读this之后,这是一个我希望工作的简约例子:

export JAVA_HOME=/<absolute_path_where_jdk_is_extracted>/jdk1.7.0_71
export HADOOP_INSTALL=/<absolute_path_where_hdp_is_extracted>/hadoop-2.3.4
export HADOOP_MAPRED_HOME=$HADOOP_INSTALL
export HADOOP_COMMON_HOME=$HADOOP_INSTALL
export HADOOP_HDFS_HOME=$HADOOP_INSTALL
export YARN_HOME=$HADOOP_INSTALL
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_INSTALL/lib/native
export HADOOP_OPTS="-Djava.library.path=$HADOOP_INSTALL/lib"
export HADOOP_CONF_DIR=$HADOOP_INSTALL/etc/hadoop
export PATH=$PATH:$HADOOP_INSTALL/bin:$HADOOP_INSTALL/sbin:$JAVA_HOME/bin

我收到此错误(在重要数量import sympy as sp import dill as pickle x, y = sp.symbols("x, y") f_example = 2*x**2 + 2*x*y f_lbda= sp.lambdify((x, y),f_example ) pickle.settings['recurse'] = True fileW = open('file_where_I_dump.dill', 'wb') # the following line crashes pickle.dump([f_lbda, f_lbda], fileW) fileR = open('file_where_I_dump.dill', 'rb') f_lbda_loaded = pickle.load(fileR) 之后:

During handling of the above exception, another exception occurred

我错过了一些重要的事情吗?

注意:当我转储sympy表达式并在pickle.load之后lambdify函数时,一切都很顺利。 但这并不是我需要的工作流程!

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

这是您的代码的略微修改版本(如下所示):它看起来像一个错误。注意,pickle lambda表达式会破坏原始的符号表达式对象!

我是dill作者。我建议将其发布为sympy github问题 - 如果您将其发布为dill问题,我会深入研究它,然后将其发布到sympy

>>> import sympy as sp
>>> x,y = sp.symbols('x,y')
>>> f_ex = 2*x**2 + 2*x*y
>>> f_la = sp.lambdify((x,y),f_ex)
>>> f_ex
2*x**2 + 2*x*y
>>> import dill
>>> dill.settings['recurse'] = True
>>> la = dill.loads(dill.dumps(f_la))
>>> la
<function <lambda> at 0x10b4ed668>
>>> f_ex
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/basic.py", line 392, in __repr__
    return sstr(self, order=None)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/str.py", line 748, in sstr
    s = p.doprint(expr)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/printer.py", line 233, in doprint
    return self._str(self._print(expr))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/printer.py", line 257, in _print
    return getattr(self, printmethod)(expr, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/str.py", line 51, in _print_Add
    terms = self._as_ordered_terms(expr, order=order)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/printing/printer.py", line 271, in _as_ordered_terms
    return expr.as_ordered_terms(order=order)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/expr.py", line 888, in as_ordered_terms
    terms, gens = self.as_terms()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/expr.py", line 920, in as_terms
    coeff = complex(coeff)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/expr.py", line 229, in __complex__
    result = self.evalf()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/evalf.py", line 1377, in evalf
    prec = dps_to_prec(n)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpmath/libmp/libmpf.py", line 67, in dps_to_prec
    return max(1, int(round((int(n)+1)*3.3219280948873626)))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2293, in amax
    out=out, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/_methods.py", line 26, in _amax
    return umr_maximum(a, axis, None, out, keepdims)
ValueError: 'axis' entry is out of bounds

答案 1 :(得分:0)

作为一种解决方法,我提出以下建议:

from sympy import *
from cloudpickle import dump, load

var("x, y") 
f_example = 2*x**2 + 2*x*y
f_lbda= lambdify((x, y),f_example ) 

with open('file_where_I_dump_flbda', 'wb') as f: 
    dump((f_lbda), f)

在另一个代码中,我有一些复杂的同情表达式,我将其用于以后使用,使用来自cloudpickle的dumpload作品!井