DataScience
article thumbnail
728x90

데이터 호출

 
from tensorflow.keras.layers import LSTM
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding
from tensorflow.keras.layers import Dense
 
import numpy as np
from tensorflow.keras.datasets import imdb

(x_train_all, y_train_all), (x_test, y_test) = imdb.load_data(skip_top=20, num_words=1000)

for i in range(len(x_train_all)):
    x_train_all[i] = [w for w in x_train_all[i] if w > 2]
    
x_train = x_train_all[random_index[:20000]]
y_train = y_train_all[random_index[:20000]]
x_val = x_train_all[random_index[20000:]]
y_val = y_train_all[random_index[20000:]]
maxlen=100  #타임 스텝의 길이가 100인 시퀀스 데이터를 만듭니다.
x_train_seq = sequence.pad_sequences(x_train, maxlen=maxlen)
x_val_seq = sequence.pad_sequences(x_val, maxlen=maxlen)

LSTM 순환 신경망 만들기

 
model_lstm = Sequential()
model_lstm.add(Embedding(1000, 32))
model_lstm.add(LSTM(8))
model_lstm.add(Dense(1, activation='sigmoid'))
model_lstm.summary()
Model: "sequential_3"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 embedding_2 (Embedding)     (None, None, 32)          32000     
                                                                 
 lstm_2 (LSTM)               (None, 8)                 1312      
                                                                 
 dense_1 (Dense)             (None, 1)                 9         
                                                                 
=================================================================
Total params: 33,321
Trainable params: 33,321
Non-trainable params: 0
_________________________________________________________________

모델 훈련하기

 
model_lstm.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

history = model_lstm.fit(x_train_seq, y_train, epochs=10, batch_size=32, 
                         validation_data=(x_val_seq, y_val))
Epoch 1/10
625/625 [==============================] - 6s 8ms/step - loss: 0.4474 - accuracy: 0.7947 - val_loss: 0.3754 - val_accuracy: 0.8362
Epoch 2/10
625/625 [==============================] - 5s 8ms/step - loss: 0.3344 - accuracy: 0.8604 - val_loss: 0.3557 - val_accuracy: 0.8408
Epoch 3/10
625/625 [==============================] - 5s 8ms/step - loss: 0.3116 - accuracy: 0.8686 - val_loss: 0.3571 - val_accuracy: 0.8416
Epoch 4/10
625/625 [==============================] - 5s 8ms/step - loss: 0.2954 - accuracy: 0.8756 - val_loss: 0.3573 - val_accuracy: 0.8432
Epoch 5/10
625/625 [==============================] - 5s 8ms/step - loss: 0.2809 - accuracy: 0.8821 - val_loss: 0.3708 - val_accuracy: 0.8410
Epoch 6/10
625/625 [==============================] - 5s 8ms/step - loss: 0.2681 - accuracy: 0.8887 - val_loss: 0.3861 - val_accuracy: 0.8394
Epoch 7/10
625/625 [==============================] - 5s 8ms/step - loss: 0.2563 - accuracy: 0.8921 - val_loss: 0.3869 - val_accuracy: 0.8372
Epoch 8/10
625/625 [==============================] - 5s 8ms/step - loss: 0.2433 - accuracy: 0.8978 - val_loss: 0.4016 - val_accuracy: 0.8344
Epoch 9/10
625/625 [==============================] - 5s 8ms/step - loss: 0.2325 - accuracy: 0.9032 - val_loss: 0.4087 - val_accuracy: 0.8316
Epoch 10/10
625/625 [==============================] - 5s 8ms/step - loss: 0.2252 - accuracy: 0.9071 - val_loss: 0.4177 - val_accuracy: 0.8336

손실 그래프와 정확도 그래프 그리기

 
import matplotlib.pyplot as plt
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.show()


plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.show()

검증 세트 정확도 평가하기

loss, accuracy = model_lstm.evaluate(x_val_seq, y_val, verbose=0)
print(accuracy)
0.8335999846458435
profile

DataScience

@Ninestar

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!