반응형

예제 코드

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM

# 1, 2, 3, 4, 5 다음에 6이 나오는걸 예측하기위한 학습 데이터
x_train = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]])
y_train = np.array([6, 7, 8])

# LSTM의 입력 디멘션은 3(행, 열, 피처)이므로 이를 맞추기 위해 reshape
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)  # (3, 5, 1)

model = Sequential()
model.add(LSTM(7, input_shape=(5, 1), activation='relu'))
model.add(Dense(4))
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam', metrics=['mse'])
model.fit(x_train, y_train, epochs=100, batch_size=1)

x_predict = np.array([[4, 5, 6, 7, 8]])
x_predict = x_predict.reshape(x_predict.shape[0], x_predict.shape[1], 1)

y_predict = model.predict(x_predict)
print(y_predict)
[[9.856894]]
반응형

'Development > Deep Learning' 카테고리의 다른 글

[Deep Learning] Keras 기초  (0) 2020.12.28
[Deep Learning] Multi Variable Linear Regression  (0) 2020.12.28
[Deep Learning] Linear Regression  (0) 2020.12.28
[Deep Learning] 기본  (0) 2020.12.28

+ Recent posts