Seite 1 von 1

zip() python 3.6 TypeError: can't pickle fortran objects

Verfasst: Sonntag 16. April 2017, 18:14
von Romaxx
Hallo zusammen,
Ich benutzte cPickle um eine Function in python 2.7 zu pickeln.
Seit ich versuche von python 2.7 auf python 3.6 zu wechseln muss ich etwas code umstellen.
Hierbei macht mir der Befehl zip() den ich seither genutzt habe um eine liste zu erstellen probleme.
Ich dachte ich kann diesen in python 3.6 einfach durch list(zip()) ersetzen, anscheinend ist das aber im zusammenhang mit _pickle ( cPickle version von python 3.6) nicht so einfach möglich.

Folgender programmausschnitt macht probleme, wobei LL und KL outputs meiner funktion sind, die ich pickeln möchte:

[codebox=python file=Unbenannt.txt]
LL, KL = get_model_exact_A(lengthscale_trf, lengthscale_p_trf, sn_trf, sf_trf, S, MU, SIGMA_trf, MU_A, SIGMA_A_trf, U, b, X, y, D, D_sum, layers, order, N, M)

inputs = {'X': X, 'MU_A': MU_A, 'SIGMA_A': SIGMA_A, 'MU': MU, 'SIGMA': SIGMA, 'S': S, 'U': U, 'b': b, 'hyp': hyp, 'y': y, 'sn': sn, 'sf': sf}

f = list(zip(['LL', 'KL'],[LL, KL]))
self.f = {fn: theano.function(input_values, fv, name=fn, on_unused_input='ignore') for fn,fv in f}

g = list(zip(['LL', 'KL'],[LL, KL]))
wrt = {'MU_A': MU_A, 'SIGMA_A': SIGMA_A, 'MU': MU, 'SIGMA': SIGMA, 'S': S, 'U': U, 'b': b, 'hyp': hyp, 'sn': sn, 'sf': sf}
self.g = {vn: {gn: theano.function(input_values, T.grad(gv, vv), name='d'+gn+'_d'+vn,
on_unused_input='ignore') for gn,gv in g} for vn, vv in wrt.items()}

sys.setrecursionlimit(10000)
cPickle.dump([self.f, self.g], file_handle, protocol=1)[/code]

Der Fehler lautet:
File "C:\Users\flo9fe\Desktop\DEEPvSSGPCopy\DEEPvSSGP_model_SV2.py", line 14, in __init__
cPickle.dump([self.f, self.g], file_handle, protocol=1)

File "C:\ProgramData\Anaconda3\lib\copyreg.py", line 65, in _reduce_ex
raise TypeError("can't pickle %s objects" % base.__name__)

TypeError: can't pickle fortran objects
Wie gesagt habe ich vorher für f und g nur zip() in python 2.6 verwendet und es lief.

Danke für eure Hilfe.

Re: zip() python 3.6 TypeError: can't pickle fortran objects

Verfasst: Sonntag 16. April 2017, 18:44
von Sirius3
@Romaxx: das hat jetzt nunmal gar nichts mit zip zu tun, das list ist überflüssig und eigentlich zip auch, weil man die zwei zweielementigen Listen gleich in der richtigen Form schreiben könnte, oder als Wörterbuch, wie es ja nach der Umwandlung auch benutzt wird. Statt die Funktionen zu pickeln, was nicht geht, solltest Du einfach nur die Parameter speichern.

Re: zip() python 3.6 TypeError: can't pickle fortran objects

Verfasst: Sonntag 16. April 2017, 21:25
von Romaxx
Hallo Sirius3,

gut:

[codebox=python file=Unbenannt.txt] LL, KL = get_model_exact_A(lengthscale_trf, lengthscale_p_trf, sn_trf, sf_trf, S, MU, SIGMA_trf, MU_A, SIGMA_A_trf, U, b, X, y, D, D_sum, layers, order, N, M)

inputs = {'X': X, 'MU_A': MU_A, 'SIGMA_A': SIGMA_A, 'MU': MU, 'SIGMA': SIGMA, 'S': S, 'U': U, 'b': b, 'hyp': hyp, 'y': y, 'sn': sn, 'sf': sf}

f = {'LL': LL, 'KL': KL}
f = f.items()
self.f = {fn: theano.function(input_values, fv, name=fn, on_unused_input='ignore') for fn,fv in f}

g = {'LL': LL, 'KL': KL}
g = g.items()
wrt = {'MU_A': MU_A, 'SIGMA_A': SIGMA_A, 'MU': MU, 'SIGMA': SIGMA, 'S': S, 'U': U, 'b': b, 'hyp': hyp, 'sn': sn, 'sf': sf}
self.g = {vn: {gn: theano.function(input_values, T.grad(gv, vv), name='d'+gn+'_d'+vn,
on_unused_input='ignore') for gn,gv in g} for vn, vv in wrt.items()}
[/code]

Und wie speichere ich diese, sodass ich sie bei nochmaligem gebrauch wieder laden kann? Danke für deine Hilfe.

Re: zip() python 3.6 TypeError: can't pickle fortran objects

Verfasst: Dienstag 25. April 2017, 12:17
von Romaxx
Hat hierzu jemand noch eine alternativen Vorschlag, wie ich diese speichere und lade?
Ich bin bei meinen Ausführung zu Beginn lediglich diesem hier gefolgt, was nicht zu funktionieren scheint.

http://deeplearning.net/software/theano ... aving.html

Hier der Code:

[codebox=python file=Unbenannt.txt]LL, KL = get_model_exact_A(lengthscale_trf, lengthscale_p_trf, sn_trf, sf_trf, S, MU, SIGMA_trf, MU_A, SIGMA_A_trf, U, b, X, y, D, D_sum, layers, order, N, M)

inputs = {'X': X, 'MU_A': MU_A, 'SIGMA_A': SIGMA_A, 'MU': MU, 'SIGMA': SIGMA, 'S': S, 'U': U, 'b': b, 'hyp': hyp, 'y': y, 'sn': sn, 'sf': sf}

f = {'LL': LL, 'KL': KL}
f = f.items()
self.f = {fn: theano.function(input_values, fv, name=fn, on_unused_input='ignore') for fn,fv in f}

g = {'LL': LL, 'KL': KL}
g = g.items()
wrt = {'MU_A': MU_A, 'SIGMA_A': SIGMA_A, 'MU': MU, 'SIGMA': SIGMA, 'S': S, 'U': U, 'b': b, 'hyp': hyp, 'sn': sn, 'sf': sf}
self.g = {vn: {gn: theano.function(input_values, T.grad(gv, vv), name='d'+gn+'_d'+vn,
on_unused_input='ignore') for gn,gv in g} for vn, vv in wrt.items()}

with open('model_exact_A_SV1.save', 'wb') as file_handle:
print('Saving model...')
sys.setrecursionlimit(10000)
cPickle.dump([self.f, self.g], file_handle, protocol=cPickle.HIGHEST_PROTOCOL)[/code]

p.s. mir ist aufgefallen, dass ich im ersten post den Befehl 'with open('model_exact_A_SV1.save', 'wb') as file_handle:
print('Saving model...')' im Code vergessen hatte.