我必須保存並加載一個cython類實例。 我用Cython類是該加的幾種方法:pickle cython class
import numpy as np
cimport numpy as np
cimport cython
cdef class Perceptron_avg_my:
cdef int wlen,freePos
cdef np.ndarray w,wtot,wac,wtotC#np.ndarray[np.int32_t]
cdef np.ndarray wmean #np.ndarray[np.float32_t]
cdef public dict fpos
def __cinit__(self,np.int64_t wlen=4*10**7):
self.fpos= dict()
self.freePos=1
self.wlen=wlen
self.w=np.zeros(wlen,np.int32)
self.wtot=np.zeros(wlen,np.int32)
self.wac=np.zeros(wlen,np.int32)
self.wtotc=np.zeros(wlen,np.int32)
self.wmean=np.zeros(wlen,np.float32)
cpdef evaluate_noavg(self,list f):
cdef np.ndarray[np.int32_t] w = self.w
cdef dict fpos = self.fpos
cdef bytes ff
cdef int i
cdef long int score=0
for ff in f:
i=fpos.get(ff,0)
if i != 0:
score += w[i]
return score
我想使用的cPickle模塊。 我明白,我必須實現一個__reduce __(個體經營)方法,但我有一些問題找到一個例子,清楚的瞭解文件
我想這樣的事情添加到Perceptron_avg_my而不是作品:
def rebuild(self,l):
self.fpos=l[0]
self.freePos=l[1]
def __reduce__(self):
#print 'reduce call'
return (Perceptron_avg_my.rebuild,(self.fpos,self.freePos))
有什麼建議嗎? 非常感謝!
這裏也有幫助:https://ask.sagemath.org/question/8376/pickling-extension-classes/ –