Seite 1 von 1

Theano for loop

Verfasst: Montag 30. Januar 2017, 11:26
von Romaxx
Hallo zusammen,

Wie sieht folgende for loop in Thenao code aus?

Code: Alles auswählen

        EPhiTPhi = T.zeros((M,M))     
        for i in range(0, N):       
            EPhiTPhi_temp = np.exp(-0.5 * (S_hat_minus**2 * (np.transpose(SIGMA_trf[i , :]))[None, None,:]).sum(2)) * np.cos((S_hat_minus * (np.transpose(MU[i , :]))[None, None,:]).sum(2) + big_sum_minus) + np.exp(-0.5 * (S_hat_plus**2 * (np.transpose(SIGMA_trf[i , :]))[None, None,:]).sum(2)) * np.cos((S_hat_plus * (np.transpose(MU[i , :]))[None, None,:]).sum(2) + big_sum_plus)
            EPhiTPhi = EPhiTPhi + EPhiTPhi_temp;  
            
        EPhiTPhi = (sf_trf/M) * EPhiTPhi # M x M
Vielen Dank und Grüße, Romaxx

Re: Theano for loop

Verfasst: Montag 30. Januar 2017, 12:36
von Romaxx
Es handelt sich prinzipiell um das vereinfachte Problem, in der sich die Variable, die hinzugefügt wird, mit dem Iterationsschritt ändert:

[codebox=python file=Unbenannt.txt] P = T.zeros((M,M))
for i in range(0, N):
P_temp = np.exp(SIGMA_trf[i , :]) * np.cos(MU[i , :])
P = P + P_temp[/code]

Für den Fall das P_temp constant ist, habe ich bereits etwas gefunden:

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

Grüße, Roman

Re: Theano for loop

Verfasst: Montag 30. Januar 2017, 13:10
von Romaxx
Ich habe es nun so gelöst, dass ich alles in ein größeres Array abspeichere, mit dem Nachteil des Speicherbedarfs.
Trotzdem erhalten ich wieder einen Fehler:

Code: Alles auswählen

  File "vSSGP_LVM_model.py", line 71, in get_EPhi
    EPhiTPhi = (sf_trf/M) * (T.exp(-0.5 * ((S_hat_minus**2)[ :, :,None] * SIGMA_trf[None, None,:]).sum(3)) * T.cos((S_hat_minus[ :, :,None] * MU[None, None,:]).sum(3) + big_sum_minus[ :, :,None]) + T.exp(-0.5 * ((S_hat_plus**2)[ :, :] * SIGMA_trf[None, None,:]).sum(3)) * T.cos((S_hat_plus[ :, :,None] * MU[None, None,:]).sum(3) + big_sum_plus[ :, :])).sum(2) # M x M

  File "theano\tensor\var.py", line 556, in __getitem__
    view = self.dimshuffle(pattern)

  File "theano\tensor\var.py", line 355, in dimshuffle
    pattern)

  File "theano\tensor\elemwise.py", line 174, in __init__
    (input_broadcastable, new_order))

ValueError: ('You cannot drop a non-broadcastable dimension.', ((False, False, False), (0, 1, 'x')))
[codebox=python file=Unbenannt.txt] def get_EPhi(self, lengthscale_trf, lengthscale_p_trf, sf_trf, S, MU, SIGMA_trf, U, b, N, M):
inv_lengthscale_p_trf, inv_lengthscale_trf = 2 * np.pi * lengthscale_p_trf**-1, lengthscale_trf**-1
S_hat = inv_lengthscale_trf * S + inv_lengthscale_p_trf # N x Q
EPhi = (2 * sf_trf/M)**0.5 * T.exp(-0.5 * ((S_hat**2)[None,:, :] * SIGMA_trf[:, None, :]).sum(2)) * T.cos((S_hat[None,:, :] * (MU[:, None, :] - U[None, :, :])).sum(2) + b) # N x M

S_hat_U_b = (S_hat * U).sum(1)[:,None] + b # M x M
big_sum_minus = S_hat_U_b - S_hat_U_b.T # M x M
big_sum_plus = S_hat_U_b + S_hat_U_b.T # M x M
S_hat_minus = S_hat[None,:,:] - S_hat[:,None,:] # M x M x Q
S_hat_plus = S_hat[None,:,:] + S_hat[:,None,:] # M x M x Q
EPhiTPhi = (sf_trf/M) * (T.exp(-0.5 * ((S_hat_minus**2)[ :, :,None] * SIGMA_trf[None, None,:]).sum(3)) * T.cos((S_hat_minus[ :, :,None] * MU[None, None,:]).sum(3) + big_sum_minus[ :, :,None]) + T.exp(-0.5 * ((S_hat_plus**2)[ :, :,None] * SIGMA_trf[None, None,:]).sum(3)) * T.cos((S_hat_plus[ :, :,None] * MU[None, None,:]).sum(3) + big_sum_plus[ :, :,None])).sum(2) # M x M

return EPhi, EPhiTPhi[/code]

Re: Theano for loop

Verfasst: Montag 30. Januar 2017, 13:45
von Romaxx
Der letzte Fehler ist behoben!

Ich interessiere mich aber weiterhin für die for loop in Theano.

Hat jemand eine Lösung?