2차원 데이터셋(sliding window) ➡️ timesteps 단위로 잘라서 3차원 구조 생성
model = Sequential([Input(shape = (timesteps, nfeatures)),
SimpleRNN(1, return_sequences=False),
Dense(1) ])
return_sequences : 출력 데이터를 다음 레이어에 전달할 크기 결정
model = Sequential([Input(shape = (timesteps, nfeatures)),
SimpleRNN(8, return_sequences = True),
SimpleRNN(4),
#Flatten(),
Dense(1)])
return_sequencesFlatten으로 펼친 후 Dense Layer로 연결
💡 RNN의 장기 의존성(long-term dependencies) 문제
💡 긴 기간동안 정보 유지와 활용에 어려움

| Cell State 업뎃 | Hidden State 업뎃 |
|---|---|
| - 불필요한 과거는 잊어라(Forget Gate) - 현재 정보 중 중요한 것은 기억하라(Input Gate) - Input Gate 와 Forget Gate 을 결합해서 장기 기억 메모리에 태워라(Update) |
[업데이트된 Cell State]와 [input, 이전셀의 hidden state]으로 새 hidden state 값 생성해서 넘기기 |
# x, y 분리
x = data.loc[:,'AvgTemp']
y = data.loco[:,'y']
#스케일링
scaler = MinMaxScaler()
x = scaler.fit_trainsform(x)
#3차원 구조 만들기
x2, y2 = temporalize(x, y, 4)
#데이터 분할
# shuffle = False : 섞지마라, 랜덤분할 하지마라
# test_size : 소수=비율, 자연수=개수
x_train, x_val, y_train, y_val = train_test_split(x2, y2, test_size= 53, shuffle = False)
# 모델구조 설계
x_train.shape #(208,4,1)
timesteps = x_train.shape[1]
nfeatures = x_train.shape[2]
clear_session()
model = Sequential([Input(shape = (timesteps, nfeatures)),
SimpleRNN(8), #LSTM
Dense(1)])
model.summary()
#컴파일 & 학습
model.compile(optimizer = Adam(learning_rate = 0.01), loss = 'mse' )
history = model.fit(x_train, y_train, epochs = 100, validation_split = .2).history
#학습곡선
dl_history_plot(history)
# 예측
pred = model.predict(x_val)
# 평가
print(mean_absolute_error(y_val, pred))
print(mean_absolute_percentage_error(y_val, pred))
# 학습곡선 함수
def dl_history_plot(history):
plt.figure(figsize=(10,6))
plt.plot(history['loss'], label='train_err', marker = '.')
plt.plot(history['val_loss'], label='val_err', marker = '.')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend()
plt.grid()
plt.show()