Recurrent Neural Network, RNN(순환신경망)이란?

Hayun Lee·2021년 7월 7일
2
post-thumbnail

Recurrent Neural Network, RNN(순환 신경망)

RNN이란?

RNN은 Recurrent 단어 그대로 반복되는 신경망이다. 즉, 스스로를 반복하면서 이전 단계에서 얻은 정보가 지속되도록 한다. RNN은 기존 Neural Network와 구조가 상당히 비슷하다. CNN과 같은 신경망들은 전부 hidden layer에서 activation function을 지난 값은 오직 출력층으로만 향했다.(이러한 신경망을 Feed Forward Neural Network라고 한다.) 그러나 RNN은 hidden node에서 activation function을 통해 나온 출력을 출력층으로도 내보내고, hidden node의 다음 연산의 입력으로도 내보내는 특징을 가지고 있다. 다음은 RNN의 구조이다.

왼쪽을 보면 A는 입력으로 XtX_{t}를 받아 hth_{t}를 출력한다. A를 둘러싼 반복은 다음 단계에서의 network가 이전 단계의 정보를 받는다는 것을 의미한다. 왼쪽의 반복을 풀어서 보면 오른쪽이 된다. 이전 단계에서의 정보가 다음 단계에서 사용된다. CNN에서처럼, bias도 입력으로 존재할 수 있다. RNN에서 hidden layer에서 activation function을 통해 출력을 내보내는 역할을 하는 node를 cell이라고 한다.(위 그림에서 A) 이 cell은 이전의 값을 기억하는 역할을 하므로 memory cell 또는 RNN cell이라고 부른다.
Memory cell이 출력층 방향으로 또는 다음 시점인 t+1의 자신에게 보내는 값을 hidden state라고 한다. 즉, t 시점의 memory cell은 t-1 시점의 memory cell이 보낸 hidden state 값을 t 시점의 hidden state 계산을 위한 입력값으로 사용한다. RNN에서 XtX_{t}hth_{t}는 기본적으로 벡터 단위이다. 이러한 구조로 인해 RNN은 시퀀스를 처리할 때 유리하다.
RNN은 입력과 출력의 길이(XtX_{t}hth_{t}의 개수)를 다르게 설계할 수 있다. 입력과 출력의 길이에 따라 일 대 다(one-to-many/vector to sequence), 다 대 일(many-to-one/sequence to vector), 다 대 다(many-to-many/sequence to sequence) 등이 존재할 수 있다.

'pytorch'라는 단어를 예측하는 모델을 생각해보자. p라는 입력이 들어왔을 때 다음에 나올 알파벳으로 y를 기대하게 된다. 이러한 경우에 앞에 들어왔던 입력값에 대한 정보가 없다면 제대로 예측할 수 없을 것이다. RNN의 두번째 hidden state에서 값들이 계산되는 방식도 마찬가지이다. 이런 과정이 지정한 시간만큼 반복된다.

일정 시간 동안 모든 값이 계산되면, 모델을 학습하기 위해 결괏값과 목푯값의 차이를 loss function을 통해 계산하고 backpropagation(역전파)해야한다. 기존의 backpropagation과는 다르게 RNN은 계산에 사용된 시점의 수에 영향을 받는다. 예를 들어 t=0에서 t=2까지 계산에 사용됐다면 그 시간 전체에 대해 역전파를 해야한다. 이를 BPTT(Backpropagation Through Time), 시간에 따른 역전파라고 한다.

위 그림에서, t가 0, 1, 2인 시점에서 각각 결과값이 나오고 목표값과 비교되는 것을 볼 수 있다. 다시 단어 "pytorch"를 예로 들면, t=0에서 p가 input으로 들어가고 t=0에서 output으로 y가 출력되기를 기대한다. 따라서 target_0에는 y가 들어가고, 결과가 y와 같지 않다면 loss가 생길 것이다. t=1에서 y가 들어가면 target_1은 t이고, t=2에서는 input이 t, target_2는 o가 된다.

이때, 모델을 학습하려면 t=2 시점에서 발생한 loss를 backpropagation하기 위해 loss를 input과 hidden layer 사이의 가중치로 미분하여 loss에 대한 각각의 비중을 구해 업데이트 하면 된다. 이 과정에서 이전 시점의 값들이 연산에 포함되게 되는데 이전 시점의 값들은 다시 가중치, input, 그 전 시점의 값의 조합이다. RNN은 각 위치별로 같은 가중치를 공유하므로 t=2 시점의 loss를 backpropagation하려면 결과적으로 t=0 시점의 노드 값들에도 모두 영향을 주어야 한다. 즉, 시간을 역으로 거슬로 올라가는 방식으로 각 가중치들을 업데이트해야하는 것이다.
가중치를 표시하면 다음과 같다.

이 그림에서 t=2인 시점만 보면 다음과 같다. 기본적으로 RNN에서는 activation function으로 tanh함수를 사용한다.

이것을 수식으로 나타내면 다음과 같다.

o=who×h2out+biash2out=tanh(w12×h1+w22×h2t=1+bias)h1out=tanh(wih×i+w11×h1t=1+bias)h2in=w12×h1+w22×h2t=1+biash1in=wih×i+w11×h1t=1+biasow22=oh2out×h2outh2in×h2inw22o = w_{ho} ×h2_{out}+bias\\ h2_{out} = tanh(w_{12}×h1+w_{22}×h2_{t=1}+bias)\\ h1_{out} = tanh(w_{ih}×i+w_{11}×h1_{t=1}+bias)\\ h2_{in} = w_{12}×h1+w_{22}×h2_{t=1}+bias\\ h1_{in} = w_{ih}×i+w_{11}×h1_{t=1}+bias\\ \frac{\partial o}{\partial w_{22}}=\frac{\partial o}{\partial h2_{out}}×\frac{\partial h2_{out}}{\partial h2_{in}}×\frac{\partial h2_{in}}{\partial w_{22}}

여기서 w_22에 대한 기울기를 보면 h2_in을 미분하는 부분이 있는데 이를 계산해보면 h2_t=1이 나온다. h2_t=1 값은 이전 시점의 값들의 조합이며 내부적으로도 w_22를 포함하고 있기 때문에 이 값을 제대로 미분하기 위해서는 t=0까지 계속 미분을 해야한다.
t=2에서 발생한 loss는 t=2, 1, 0 시점에 모두 영향을 주고 t=1 시점의 loss는 t=1, 0에 영향을 주며 t=0 시점의 손실은 t=0의 가중치에 영향을 준다. 따라서 실제로 업데이트를 할 때는 가중치에 대해 시점별 기울기를 다 더해서 한 번에 업데이트 한다.

RNN의 응용

RNN의 가장 큰 특징은 이전 cell의 정보를 다음 cell의 연산에서 사용한다는 것이다. 따라서 순서가 중요할 때 유용하게 사용된다.
순서가 존재하는 데이터를 sequence 데이터라고 하며 대표적으로는 자연어가 포함된다. 또한 똑같이 순서가 존재하지만, 특별히 시간에 따른 의미가 존재하는 데이터를 시계열 데이터(time series data)라고 하며 대표적으로는 주가가 있다.

LSTM

위에서 살펴본 것처럼 RNN은 이전의 값에 영향을 받는다. 따라서 결과값을 얻기 위해 필요한 시간이 길어지면(==RNN이 길어지면) 시간 격차가 상당히 커지게 된다. 이 격차가 늘어날수록 RNN은 학습하기 힘들어진다. 이론적으로는 RNN의 이러한 긴 기간의 의존성(long-term dependencies)를 잘 다룰 수 있다. 그것을 가능하게 하는 것이 LSTM인데, 이것은 RNN의 한 종류이다. LSTM에 대해서는 따로 포스팅 할 예정이다.

9개의 댓글

comment-user-thumbnail
2023년 2월 7일

I guess the best of you can extract out of this is that during the session I do have on this website I have just found this one just worth listening quit concise to the topic in which you need to understand methodological study from online assignment writing help UAE and consequent knowledge of birds though there are several others.

답글 달기
comment-user-thumbnail
2023년 3월 3일

Were you anticipating a process as demanding as your tax assignments? You were mistaken. In every conceivable way, we are busy. You only need to make your payment and send your assignments essay help online to us by email or WhatsApp. Just that. Simple, right? We'll be eagerly awaiting your first purchase from us!

답글 달기
comment-user-thumbnail
2023년 8월 28일

Fantastic breakdown of Recurrent Neural Networks! The blog brilliantly explains their architecture and how they excel in sequence data. This clarity is a valuable resource for both beginners and tech enthusiasts. Just as this blog demystifies complex topics, consider Vancouver resume writing services to unravel the intricacies of crafting a winning resume for your career journey.

답글 달기
comment-user-thumbnail
2023년 9월 20일

Assignment help services in the UAE offer a lifeline to students by providing expert guidance, timely delivery, and plagiarism-free content. These services cater to a wide range of subjects and levels, ensuring that students receive customized solutions tailored to their unique needs. Whether it's assistance with research, writing, or structuring assignments, UAE assignment help services are committed to helping students achieve academic success.

답글 달기
comment-user-thumbnail
2023년 9월 22일

Great to hear that you found the explanation of Recurrent Neural Networks (RNNs) fantastic and valuable! RNNs are indeed a powerful tool for working with sequence data.

When it comes to crafting a winning resume for your career journey, Professional resume services can be beneficial. They can help you highlight your skills, experiences, and achievements in a way that stands out to potential employers. In particular, may cater to the specific job market and preferences in the USA areas.

Remember that a well-written resume is crucial in making a positive first impression on employers and increasing your chances of landing interviews. So, seeking professional assistance from online resume services, is a wise choice if you want to ensure your resume effectively communicates your qualifications and aspirations.

답글 달기
comment-user-thumbnail
2023년 12월 1일

google

답글 달기
comment-user-thumbnail
2024년 1월 11일

Did you expect a procedure as rigorous as your tax DissertationBuy? You were not correct. We are busy in every sense of the word. All you have to do is submit your cash and your online dissertation writing assistance to us over WhatsApp or email. That's all. Easy enough, huh? We are excited to see what you buy from us for the first time!

1개의 답글
comment-user-thumbnail
2024년 4월 8일

It depends upon the individual and circumstance before submission your statement. Writer or Authors also proofread his material before submission. Experts help in editing and formatting, then print your book or novel through The Book Printing UK. Because review is most important, it can effect outcomes.

답글 달기