theano scan

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Romaxx
User
Beiträge: 62
Registriert: Donnerstag 26. Januar 2017, 18:53

Hall zusammen,

ich möchte über die 1. dimension mehrerer mehrdim. arrays mit der theano scan function iterieren :
Mein example code ist:

[codebox=python file=Unbenannt.txt]import numpy as np
import theano
import theano.tensor as T

N = 100
M = 50
D = 2
EPhiTPhi = np.zeros((M,M))
MU = T.matrix("MU")
SIGMA_trf = T.matrix("SIGMA_trf")
Z_n_W = T.matrix("Z_n_W")
MU_S_hat_plus = T.matrix("MU_S_hat_plus")

def EPhiTPhi_loop(EPhiTPhi, MU, SIGMA_trf, Z_n_W, MU_S_hat_plus):
EPhiTPhi = EPhiTPhi + Z_n_W * (T.exp(-0.5 * (MU_S_hat_plus**2 * SIGMA_trf[:, :, None]).sum(2)) * T.cos((MU_S_hat_plus * MU[:, :, None]).sum(2))

EPhiTPhi_out, _ = theano.scan(EPhiTPhi_loop,
outputs_info = EPhiTPhi,
sequences = [MU, SIGMA_trf, Z_n_W],
non_sequences = [MU_S_hat_plus])

OUT = theano.function(inputs=[MU, SIGMA_trf, Z_n_W], outputs=EPhiTPhi_out)

MU = np.zeros((N,D))
SIGMA_trf = np.zeros((N,D))
Z_n_W = np.zeros((N,M,M))
MU_S_hat_minus = np.zeros((M,M,D)) [/code]

Soweit so gut.
Ich erhalte aber hier schon den Fehler:

Code: Alles auswählen

runfile('C:/Users/flo9fe/untitled1.py', wdir='C:/Users/flo9fe')
Traceback (most recent call last):

  File "<ipython-input-1-3dd424f4749a>", line 1, in <module>
    runfile('C:/Users/flo9fe/untitled1.py', wdir='C:/Users/flo9fe')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/flo9fe/untitled1.py", line 22, in <module>
    non_sequences = [MU_S_hat_minus, MU_S_hat_plus])

  File "C:\ProgramData\Anaconda3\lib\site-packages\theano\scan_module\scan.py", line 773, in scan
    condition, outputs, updates = scan_utils.get_updates_and_outputs(fn(*args))

  File "C:/Users/flo9fe/untitled1.py", line 16, in EPhiTPhi_loop
    EPhiTPhi = EPhiTPhi + Z_n_W * (T.exp(-0.5 * (MU_S_hat_minus**2 * SIGMA_trf[:, :, None]).sum(2)) * T.cos((MU_S_hat_minus * MU[:, :, None]).sum(2)) + T.exp(-0.5 * (MU_S_hat_plus**2 * SIGMA_trf[:, :, None]).sum(2)) * T.cos((MU_S_hat_plus * MU[:, :, None]).sum(2)));

  File "C:\ProgramData\Anaconda3\lib\site-packages\theano\tensor\var.py", line 560, in __getitem__
    view = self.dimshuffle(pattern)

  File "C:\ProgramData\Anaconda3\lib\site-packages\theano\tensor\var.py", line 355, in dimshuffle
    pattern)

  File "C:\ProgramData\Anaconda3\lib\site-packages\theano\tensor\elemwise.py", line 159, in __init__
    (i, j, len(input_broadcastable)))

ValueError: new_order[1] is 1, but the input only has 1 axes.
Mir würde es schon weiterhelfen, wenn man mir sagt, was die fehlermeldung ausdrücken möchte.
Romaxx
User
Beiträge: 62
Registriert: Donnerstag 26. Januar 2017, 18:53

Hallo,

ich habe meine beispiel etwas modifiziert:

[codebox=python file=Unbenannt.txt]import numpy as np
import theano
import theano.tensor as T

N = 100
M = 50
D = 2
EPhiTPhi = np.zeros((M,M))
EPhiTPhi = EPhiTPhi.astype(np.float32)
Z_n_W = T.tensor3("Z_n_W")

def EPhiTPhi_loop(EPhiTPhi, Z_n_W):
EPhiTPhi = EPhiTPhi + Z_n_W;
return EPhiTPhi

EPhiTPhi_out, _ = theano.scan(EPhiTPhi_loop,
outputs_info = EPhiTPhi,
sequences = [Z_n_W])

OUT = theano.function(inputs=[Z_n_W], outputs = EPhiTPhi_out)

Z_n_W = np.ones((N,M,M))
Z_n_W = Z_n_W.astype(np.float32)

LIST = {'Z_n_W': Z_n_W}

TEST = OUT(**LIST)

print(OUT(**LIST))[/code]

Es funktioniert nun. Aber wie kann ich hier nur die letzte the iterierten als wert abspeichern.
Im moment gibt OUT(**LIST) alle iterierten aus und bei TEST = OUT(**LIST) speichert es nur die erste.

** ich war zu voreilig. es gibt doch alles aus !
Antworten