2017-01-08 2 views
3

Ich habe die folgenden Definitionen der Theano Tutorial MLP inspiriert:verschachtelte Schleife in Theano

Weights = rng.uniform(low=low, high=high, size=(n_in, n_out)) 
     W_values = numpy.asarray(Weights, dtype=theano.config.floatX) 
     W = theano.shared(value=W_values, name='W', borrow=True) 
b_values = numpy.zeros((n_out,), dtype=theano.config.floatX) 
     b = theano.shared(value=b_values, name='b', borrow=True) 
sigmas = rng.uniform(low=0.001, high=100, size=(n_out,)) 
     s_values = numpy.asarray(sigmas, dtype=theano.config.floatX) 
     s = theano.shared(value=s_values, name='s', borrow=True) 
input = T.matrix("input") 

Ich möchte eine Gauß-Aktivierung wie folgt Comput:

output = array([[numpy.exp(-numpy.linalg.norm(w_s - x_s, 2) ** 2/2*s_s ** 2) for w_s, s_s in zip(W.T, s)] for x_s in X]) + b 

Dies ist jedoch nicht direkt möglich, weil weder W noch X noch s iterierbar sind. Wie kann man eine Theano-Version effizient programmieren?

Vielen Dank.

EDIT:

ich eine Lösung konfiguriert haben:

# Declare an intermediate shared variable: 
h_values = numpy.zeros((batch_s, n_out), dtype=theano.config.floatX) 
dot_H = theano.shared(value=h_values, name='h', borrow=True) 
# compute the kernel for an input vector and then for the whole batch 
for i in range(batch_s): 
    dot_H=T.set_subtensor(dot_H[i],theano.scan(lambda w, sig, bias: \ 
        T.exp(-ops.norm(w - input[i], 2) ** 2/2*sig ** 2) 
         +bias, 
        sequences=[self.W.T, self.s, self.b])[0]) 
# retrieve the solution as a numpy matrix 
output = dot_H 

aber .. Ich habe beobachtet, dass dies gibt eine alle Elemente Nullen in dot_H. Es scheint, dass sogar der Unterschied w - input[i] nicht korrekt berechnet werden kann.

EDIT_2 Ich habe das Problem behoben, aber ich denke, die Mine ist nicht die effizienteste Lösung, kann mir jemand bitte etwas besseres vorschlagen?

import theano.tensor as T 
import numpy 
import theano 

batch_s=5 
dims=10 
hidd_s=3 
out_s=2 

missing_param = None #"ignore" 

rng = numpy.random.RandomState(1234) 
input = T.matrix("input") 
X = numpy.asarray(rng.uniform(low=-2.1, high=5.0, size=(batch_s, dims))) 

def layer(x): 

    W=theano.shared(
     value=numpy.asarray(
      rng.uniform(low=0.001, high=1.0, size=(dims, hidd_s)), 
       dtype=theano.config.floatX), 
     name='W', borrow=True) 

    S=theano.shared(
     value=numpy.asarray(
      rng.uniform(low=10.0, high=100.0, size=(hidd_s,)), 
       dtype=theano.config.floatX), 
     name='S', borrow=True) 

    dot_H = theano.shared(
     value=numpy.zeros((batch_s, hidd_s), 
      dtype=theano.config.floatX), 
     name='dot_H', borrow=True) 

    for i in range(batch_s): 
     for j in range(hidd_s): 
      dot_H = T.set_subtensor(dot_H[i,j], 
         T.exp(-(W.T[j] - x[i]).norm(2) ** 2)/2 * S[j] ** 2) 


    return dot_H 

layer_out = theano.function(
          inputs=[input], 
          outputs=layer(input), 
          on_unused_input=missing_param 
          ) 

print layer_out(X) 

Antwort

1

T.set_subtensor(x[...], y) ein "symbolisch" Variable mit dem angegebenen subtensor durch y ersetzt zurückzukehren. Es wird nicht wirklich innerhalb der x funktionieren. Um den gemeinsamen Wert x zu ändern, müssen Sie theano.function und "update" x mit einem neuen Wert verwenden.