LSTM과 CNN의 조합을 이용한 영화 리뷰 분류하기
LSTM과 CNN의 조합을 이용한 영화 리뷰 분류하기.ipynb
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from keras.datasets import imdb
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, LSTM, Embedding
from keras.layers import Conv1D, MaxPooling1D, Dropout
from keras.preprocessing import sequence
# seed 값 설정
seed = 0
np.random.seed(seed)
tf.set_random_seed(seed)
# 데이터 불러오기
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=5000)
# 데이터 전처리
x_train = sequence.pad_sequences(x_train, maxlen=100)
x_test = sequence.pad_sequences(x_test, maxlen=100)
# 모델의 설정
model = Sequential()
model.add(Embedding(5000, 100))
model.add(Dropout(0.25))
model.add(Conv1D(64, 5, padding="valid", activation="relu", strides=1))
model.add(MaxPooling1D(pool_size=4))
model.add(LSTM(55))
model.add(Dense(1, activation="sigmoid"))
model.summary()
# 모델 컴파일
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
# 모델 평가
print("\n Test Accuracy : %.4f" % (model.evaluate(x_test, y_test)[1]))
# 그래프 작성
y_train_loss = history.history["loss"]
y_val_loss = history.history["val_loss"]
x_len = np.arange(len(y_val_loss))
plt.plot(x_len, y_train_loss, marker=".", color="blue", label="Train_Loss")
plt.plot(x_len, y_val_loss, marker=".", color="red", label="Test_Loss")
plt.legend(loc="upper right")
plt.grid()
plt.xlabel("epoch")
plt.ylabel("loss")
plt.show()
[출처] 모두의 딥러닝, 조태호 지음, 길벗, p246~253