- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2020-07-07T00:42:20+00:00","default:f-lab","f-lab")
#author("2020-07-07T04:40:27+00:00","default:f-lab","f-lab")
*IMDbデータを用いたLSTMで学習 [#sf5f9288]
**train_keras_lstm.py [#reeb1ec9]
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()
**実行結果 [#qebc3244]
-1枚目が正解率と2枚目が損失率
#ref(yamasaki2019-20200707/IMDb_LSTM_acc.jpg, 40%);
#ref(yamasaki2019-20200707/IMDb_LSTM_loss.jpg, 40%);
#ref(yamasaki2019-20200707/IMDb_LSTM_terminal.jpg, 40%);
***疑問点 [#udb8e613]
-層のことがよくわかってない
--モデルの構築の部分は1行1行何やっているのか
%%-モデルの評価の精度ってどれなのか %%
%%--そもそもモデルの評価の精度を出力してないかもしれない%%
[[seminar-personal/yamasaki2019]]