hemen cevap, bunu turşu https://wiki.python.org/moin/UsingPickle
görmektir Ama IBMModel1 bir lambda işlevini döndürür, bunun nedeni pickle
/cPickle
biz
Yani (https://github.com/nltk/nltk/blob/develop/nltk/align/ibm1.py#L74 ve https://github.com/nltk/nltk/blob/develop/nltk/align/ibm1.py#L104 bakınız) varsayılan ile turşu mümkün değildir dill
kullanın. Öncelikle, dill
yükleme bölümüne bakın Sonra Can Python pickle lambda functions?
$ pip install dill
$ python
>>> import dill as pickle
: Bir olan IBMModel1
nesne, turşu çalışırsanız
>>> import dill as pickle
>>> from nltk.corpus import comtrans
>>> bitexts = comtrans.aligned_sents()[:100]
>>> with open('model1.pk', 'rb') as fin:
... ibm = pickle.load(fin)
...
>>> aligned_sent = ibm.align(bitexts[0])
>>> aligned_sent.words
['Wiederaufnahme', 'der', 'Sitzungsperiode']
:
>>> import dill
>>> import dill as pickle
>>> from nltk.corpus import comtrans
>>> from nltk.align import IBMModel1
>>> bitexts = comtrans.aligned_sents()[:100]
>>> ibm = IBMModel1(bitexts, 20)
>>> with open('model1.pk', 'wb') as fout:
... pickle.dump(ibm, fout)
...
>>> exit()
turşusu modeli kullanmak için lambda işlevi, bu ile biter:
>>> import cPickle as pickle
>>> from nltk.corpus import comtrans
>>> from nltk.align import IBMModel1
>>> bitexts = comtrans.aligned_sents()[:100]
>>> ibm = IBMModel1(bitexts, 20)
>>> with open('model1.pk', 'wb') as fout:
... pickle.dump(ibm, fout)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle function objects
(Not: Yukarıdaki kod parçası 3.0.0 NLTK sürümü geliyor):
IBMModel1 bir lambda fonksiyonu döndürdüğü için NLTK 3.0.0 ile python3 olarak
, ayrıca aynı sorunla karşı karşıya gelecek
[email protected]:~$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> from nltk.corpus import comtrans
>>> from nltk.align import IBMModel1
>>> bitexts = comtrans.aligned_sents()[:100]
>>> ibm = IBMModel1(bitexts, 20)
>>> with open('mode1.pk', 'wb') as fout:
... pickle.dump(ibm, fout)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
_pickle.PicklingError: Can't pickle <function IBMModel1.train.<locals>.<lambda> at 0x7fa37cf9d620>: attribute lookup <lambda> on nltk.align.ibm1 failed'
>>> import dill
>>> with open('model1.pk', 'wb') as fout:
... dill.dump(ibm, fout)
...
>>> exit()
[email protected]:~$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> from nltk.corpus import comtrans
>>> with open('model1.pk', 'rb') as fin:
... ibm = dill.load(fin)
...
>>> bitexts = comtrans.aligned_sents()[:100]
>>> aligned_sent = ibm.aligned(bitexts[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'IBMModel1' object has no attribute 'aligned'
>>> aligned_sent = ibm.align(bitexts[0])
>>> aligned_sent.words
['Wiederaufnahme', 'der', 'Sitzungsperiode']
(Not: http://docs.pythonsprints.com/python3_porting/py-porting.html bkz python3 olarak, pickle
cPickle
olan) Sen hizalama modeli tasarrufu tartıştılar
: Bundan sonra
Eğer turşu olarak dereotu ile kaydedebilirsiniz, Ne denediğinden emin değilim, ama lambdalar görmedim ve vanilya turşusu ile "modeli" nin seçilmesinden ve çıkarılmasından hiç problem yaşamadım. – alexis
@alexis Bu ilginç, güncellenmiş cevapla aynı hatayı aldınız mı? – alvas
Henüz denemek için bir şansım olmadı; ama farklı deneyimleri (modülün çok değişmiş olduğunu fark etmemiştim) açıklayan python 2 ile soldurma işlemini test etmiş olabilirim. Denediğimde size haber vereceğim. – alexis