
import numpy as np
# RNN이 몇 번 펼쳐질 것인가? => 문장의 길이
timesteps = 10 # 문장의 길이 t
# RNN의 입력. 일반적으로는 단어의 벡터 차원
input_size = 4 # 단어의 차원 D
# RNN cell에서 hidden unit의 갯수 (cell 용량)
hidden_size = 8
# RNN 입력 데이터 (projection layer)
inputs = np.random.random((timesteps, input_size)) # D x t
# hidden state
hidden_state_t = np.zeros((hidden_size,)) # h_0 = 0
W_x = np.random.random((hidden_size, input_size))
W_h = np.random.random((hidden_size, hidden_size))
b = np.random.random((hidden_size,))
total_hidden_states = []
# RNN 작동
# 단어 벡터 하나씩 순서대로 꺼낸다
for input_t in inputs:
# output_t 가 실제로는 h_t 의 역할 (현 시점의 hidden state)
output_t = np.tanh(W_x @ input_t + W_h @ hidden_state_t + b)
# 각 시점의 은닉 상태의 값을 계속해서 기록
total_hidden_states.append(list(output_t))
hidden_state_t = output_t
# 출력 시 값을 깔끔하게 만들어줌
total_hidden_states = np.stack(total_hidden_states, axis=0)
print(total_hidden_states)
>>> [[0.57233133 0.83320468 0.62405135 0.5563321 0.73192675 0.75505076
0.76346329 0.60714161]
[0.99970929 0.99934681 0.9984037 0.99568118 0.99852231 0.99908636
0.99948534 0.99836627]
[0.99996845 0.99974602 0.99983944 0.99857436 0.99958013 0.99974239
0.99980824 0.99979578]
[0.99998353 0.99992415 0.99991764 0.99967546 0.99969424 0.99987357
0.99983488 0.99987852]
[0.99997063 0.99985376 0.9999276 0.99925842 0.99982861 0.99987391
0.99987287 0.99986328]
[0.99996383 0.99974692 0.99985283 0.99863859 0.99954205 0.99976828
0.99978305 0.99979495]
[0.99998859 0.99992327 0.99991825 0.99944892 0.99989465 0.99988418
0.99994144 0.99990164]
[0.99998844 0.99994257 0.9999174 0.99947613 0.99985617 0.99991842
0.99993841 0.99990751]
[0.99998721 0.99991942 0.99992409 0.9995987 0.99986405 0.99986662
0.99991239 0.99989391]
[0.99998451 0.99989823 0.99988029 0.9995821 0.99958533 0.9998003
0.99980867 0.99985471]]


이 고기는 맛있지만, 이븐하게 익지 않았다
