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,)
total_hidden_states = []
# 메모리 셀 동작
for input_t in inputs:
output_t = np.tanh(np.dot(Wx, input_t) + np.dot(Wh, hidden_state_t) + b)
total_hidden_states.append(list(output_t))
print(np.shape(total_hidden_states))
# 각 시점인 t별 메모리 셀의 출력 크기는 (timestep, output_dim)
hidden_state_t = output_t
total_hidden_states = np.stack(total_hidden_states, axis=0)
print(total_hidden_states)
# total_hidden_states는 (10, 8)의 모양을 가진