본문 바로가기

Deep Learning

4. RNN


ㅁ RNN



 RNN은 히든 노드가 방향을 가진 엣지로 연결돼 순환구조를 이루는(directed cycle) 인공신경망의 한 종류입니다. 번역, 언어 모델과 같은 Time Series 한 or 연속적인 데이터 처리시에 좋은 성능을 보인다 하여 이미지 데이터 모델에서 많이 쓰이는 Convolutional Neural Networks(CNN)과 더불어 최근 들어 각광 받고 있는 알고리즘입니다. ( 아 물론 CNN 도 최근 페이스북에서 번역기술에 CNN 을 이용한 기술들이 나오고 있어요 ^^;; )


종류

 - One to Many

 - Many to One

 - Many to Many





ㅁ Cell




  - Forward Propagation


def forward_propagation(self, x):
    # The total number of time steps
    T = len(x)
    # During forward propagation we save all hidden states in s because need them later.
    # We add one additional element for the initial hidden, which we set to 0
    s = np.zeros((T + 1, self.hidden_dim))
    s[-1] = np.zeros(self.hidden_dim)
    # The outputs at each time step. Again, we save them for later.
    o = np.zeros((T, self.word_dim))
    # For each time step...
    for t in np.arange(T):
        # Note that we are indxing U by x[t]. This is the same as multiplying U with a one-hot vector.
        s[t] = np.tanh(self.U[:,x[t]] + self.W.dot(s[t-1]))
        o[t] = softmax(self.V.dot(s[t]))
    return [o, s]

RNNNumpy.forward_propagation = forward_propagation




 - Backward Propagation







ㅁ BPTT ( Backward Propagation Through Time )


def bptt(self, x, y):
    T = len(y)
    # Perform forward propagation
    o, s = self.forward_propagation(x)
    # We accumulate the gradients in these variables
    dLdU = np.zeros(self.U.shape)
    dLdV = np.zeros(self.V.shape)
    dLdW = np.zeros(self.W.shape)
    delta_o = o
    delta_o[np.arange(len(y)), y] -= 1.
    # For each output backwards...
    for t in np.arange(T)[::-1]:
        dLdV += np.outer(delta_o[t], s[t].T)
        # Initial delta calculation: dL/dz
        delta_t = self.V.T.dot(delta_o[t]) * (1 - (s[t] ** 2))
        # Backpropagation through time (for at most self.bptt_truncate steps)
        for bptt_step in np.arange(max(0, t-self.bptt_truncate), t+1)[::-1]:
            # print "Backpropagation step t=%d bptt step=%d " % (t, bptt_step)
            # Add to gradients at each previous step
            dLdW += np.outer(delta_t, s[bptt_step-1])
            dLdU[:,x[bptt_step]] += delta_t
            # Update delta for next step dL/dz at t-1
            delta_t = self.W.T.dot(delta_t) * (1 - s[bptt_step-1] ** 2)
    return [dLdU, dLdV, dLdW]



ㅁ LSTM ( Long Short-Term Memory )







참조 사이트

https://ratsgo.github.io/natural%20language%20processing/2017/03/09/rnnlstm/

http://aikorea.org/blog/rnn-tutorial-3/

'Deep Learning' 카테고리의 다른 글

3. CNN  (0) 2017.05.26
2. 학습관련 기술들  (0) 2017.05.14
1. 오차역전파법 ( Backpropagation )  (0) 2017.05.14
0. 딥러닝 기초지식  (0) 2017.05.12