Creating Simple RNN

RNN은 간단하게, 파이썬 혹은 파이토치를 이용하여 구현해 볼 수 있다.

Using Python

import numpy as np

timesteps = 10
# 시점의 수. NLP에서는 보통 문장의 길이가 된다.
input_size = 4
# 입력의 차원. NLP에서는 보통 단어 벡터의 차원이 된다
hidden_size = 8
# 은닉 상태의 크기 (= 메모리 셀의 용량)

inputs = np.random.random((timesteps, input_size))
# 2D 텐서

hidden_state_t = np.zeros((hidden_size))
# 초기 상태는 0벡터로 초기화
# 은닉 상태의 크기는 hidden_size로 생성
print(hidden_state_t)
# [0. 0. 0. 0. 0. 0. 0. 0.]
Wx = np.random.random((hidden_size, input_size))
# (8,4)크기의 2D 텐서로 입력에 대한 가중치
Wh = np.random.random((hidden_size, hidden_size))
# (8,8)크기의 2D 텐서로 은닉 상태에 대한 가중치
b = np.random.random((hidden_size)) 
# (8,) 크기의 1D 텐서 생성. 이 값은 bias

print(np.shape(Wx))
print(np.shape(Wh))
print(np.shape(b))
# (8, 4)
# (8, 8)
# (8,)

Using Pytorch

다음 페이지에서는 앞서 언급 내용을 토대로, 데이터 전처리 과정부터 시작하여 '네이버 영화 데이터 리뷰 데이터셋'을 이용하여 감정 분석(Sentimental Classification) task를 다뤄보고자 한다.

사진 및 내용 출처: https://wikidocs.net/24996

Last updated

Was this helpful?