2014-12-07 2 views
1

내 RNN 구현에 관한 질문이 있습니다.Theano (스캔 op)와 생성 모드의 RNN

내가

def one_step(x_t, h_tm1, W_ih, W_hh, b_h, W_ho, b_o): 

    h_t = T.tanh(

       theano.dot(x_t, W_ih) + 

       theano.dot(h_tm1, W_hh) + 

       b_h 

      ) 

    y_t = theano.dot(h_t, W_ho) + b_o 

    return [h_t, y_t] 


n_hid = 3 
n_in = 1 
n_out = 1 


W_hh_values = np.array(np.random.uniform(size=(n_hid, n_hid), low=-.01, high=.01), dtype=dtype) 
W_hh2_values = np.array(np.random.uniform(size=(n_hid, n_hid), low=-.01, high=.01), dtype=dtype) 
h0_value = np.array(np.random.uniform(size=(n_hid), low=-.01, high=.01), dtype=dtype) 
b_h_value = np.array(np.random.uniform(size=(n_hid), low=-.01, high=.01), dtype=dtype) 
b_h2_value = np.array(np.random.uniform(size=(n_hid), low=-.01, high=.01), dtype=dtype) 
W_ih_values = np.array(np.random.uniform(size=(n_in, n_hid), low=-.01, high=.01), dtype=dtype) 
W_ho_values = np.array(np.random.uniform(size=(n_hid, n_out), low=-.01, high=.01), dtype=dtype) 
b_o_value = np.array(np.random.uniform(size=(n_out), low=-.01, high=.01), dtype=dtype) 


# parameters of the rnn 
b_h = theano.shared(b_h_value) 
b_h2 = theano.shared(b_h_value) 
h0 = theano.shared(h0_value) 
W_ih = theano.shared(W_ih_values) 
W_hh = theano.shared(W_hh_values) 
W_hh2 = theano.shared(W_hh_values) 
W_ho = theano.shared(W_ho_values) 
b_o = theano.shared(b_o_value) 

params = [W_ih, W_hh, b_h, W_ho, b_o, h0] 

# target values 
t = T.matrix(dtype=dtype) 


# hidden and outputs of the entire sequence 
[h_vals, y_vals], _ = theano.scan(fn=one_step, 
          sequences = dict(input = x, taps=10), 
          outputs_info = [h0, None], # corresponds to the return type of one_step 
          non_sequences = [W_ih, W_hh, b_h, W_ho, b_o] 
         ) 

learn_rnn_fn = theano.function([], 
         outputs = cost, 
         updates = updates, 
         givens = { 
          x: s_, 
          t: t_ 
         } 
      ) 

지금 훈련 후 나는이 같은 과정의 출력을 예측할 수있는 다음과 같은 코드가 있습니다

test_rnn_fn = theano.function([], 
       outputs = y_vals, 
         givens = {x: s_2} 
      ) 

그러나이 예측 모드에서 네트워크를 실행에게 (X의 조치를 취할 즉, 입력을 예측하고 출력을 예측). 이것을 생성 모드로 실행하고 싶습니다. 즉, 초기 상태에서 시작하여 임의의 단계에 대해 RNN을 실행하고 출력을 입력으로 되돌리고 싶습니다.

어떻게하면됩니까?

감사합니다.

답변

3

n_steps 매개 변수를 사용하여 임의의 단계로 스캔을 실행할 수 있습니다. 다음 계산에 y_t를 전달합니다 - 그리고 x_t.shape == y_t.shape 가정 - 스캔과 theano.scan_module.until()으로 생성 할 수 one_step(h_tm1, y_tm1, W_ih, W_hh, b_h, W_ho, b_o)

더 복잡한 종료 기준에 스텝 기능을 수정하는 인수로 outputs_info=[h0, x_t]을 사용할 수 있지만 사람들은의 질문입니다 디자인은 구현되지 않습니다. 예제는 here을 참조하십시오.