IMDbデータを用いたLSTMで学習

train_keras_lstm.py

from keras.datasets import imdb
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Embedding, LSTM, Dense
import matplotlib.pyplot as plt

max_features = 10000 # 特徴量として考慮する単語の数
max_len =  500# この数の単語を残してテキストをカット
batch_size = 32

# IMDbデータの前処理
print('Loading data...')
# データの整数のリストとして読み込む
(input_train, y_train), (input_test, y_test) = \
    imdb.load_data(num_words=max_features)
print(len(input_train), 'train seguences')
print(len(input_test), 'test seguences')

print('Pad sequences (samples * time)')
input_train = sequence.pad_sequences(input_test, maxlen=max_len)
input_test = sequence.pad_sequences(input_test, maxlen=max_len)
print('input_train: shape:', input_train.shape)
print('input_test: shape', input_test.shape)

# LSTM層でのモデルの訓練
model = Sequential()
model.add(Embedding(max_features, 32))
model.add(LSTM(32))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(input_train, y_train, epochs=10, batch_size=128, 
validation_split=0.2)

acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

# 正解率をプロット
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and Validation accuracy')
plt.legend()

plt.figure()

# 損失率をプロット
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and Validation loss')
plt.legend()

plt.show()

実行結果

  • 1枚目が正解率と2枚目が損失率
 40%
 40%
 40%

疑問点

  • 層のことがよくわかってない
    • モデルの構築の部分は1行1行何やっているのか -モデルの評価の精度ってどれなのか --そもそもモデルの評価の精度を出力してないかもしれない

seminar-personal/yamasaki2019


添付ファイル: fileIMDb_LSTM_terminal.jpg 276件 [詳細] fileIMDb_LSTM_loss.jpg 271件 [詳細] fileIMDb_LSTM_acc.jpg 259件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2020-07-07 (火) 13:40:27