質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1813閲覧

学習の様子を可視化できない

taiyo2017

総合スコア170

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2018/09/17 03:22

編集2018/09/24 00:58

学習の様子を可視化できないです。

・ ・ ・ # placeholders input_sequence = Input((story_maxlen,)) question = Input((query_maxlen,)) # encoders # embed the input sequence into a sequence of vectors input_encoder_m = Sequential() input_encoder_m.add(Embedding(input_dim=vocab_size, output_dim=64)) input_encoder_m.add(Dropout(0.3)) # output: (samples, story_maxlen, embedding_dim) # embed the input into a sequence of vectors of size query_maxlen input_encoder_c = Sequential() input_encoder_c.add(Embedding(input_dim=vocab_size, output_dim=query_maxlen)) input_encoder_c.add(Dropout(0.3)) # output: (samples, story_maxlen, query_maxlen) # embed the question into a sequence of vectors question_encoder = Sequential() question_encoder.add(Embedding(input_dim=vocab_size, output_dim=64, input_length=query_maxlen)) question_encoder.add(Dropout(0.3)) # output: (samples, query_maxlen, embedding_dim) # encode input sequence and questions (which are indices) # to sequences of dense vectors input_encoded_m = input_encoder_m(input_sequence) input_encoded_c = input_encoder_c(input_sequence) question_encoded = question_encoder(question) # compute a 'match' between the first input vector sequence # and the question vector sequence # shape: `(samples, story_maxlen, query_maxlen)` match = dot([input_encoded_m, question_encoded], axes=(2, 2)) match = Activation('softmax')(match) # add the match matrix with the second input vector sequence response = add([match, input_encoded_c]) # (samples, story_maxlen, query_maxlen) response = Permute((2, 1))(response) # (samples, query_maxlen, story_maxlen) # concatenate the match matrix with the question vector sequence answer = concatenate([response, question_encoded]) # the original paper uses a matrix multiplication for this reduction step. # we choose to use a RNN instead. answer = LSTM(32)(answer) # (samples, 32) # one regularization layer -- more would probably be needed. answer = Dropout(0.3)(answer) answer = Dense(vocab_size)(answer) # (samples, vocab_size) # we output a probability distribution over the vocabulary answer = Activation('softmax')(answer) # build the final model model = Model([input_sequence, question], answer) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # train hist = model.fit([inputs_train, queries_train], answers_train, batch_size=32, epochs=120, validation_data=([inputs_test, queries_test], answers_test)) model_path1 = 'model1.h5' model.save(model_path1) val_acc = hist.history['val_acc'] plt.rc('font',family='serif') fig = plt.figure() plt.plot(range(240),val_acc,label='acc',color='black') plt.xlabel('epochs') plt.show()

のようにコードを書いて学習の様子を可視化しようとしました。
しかし、何エポックになってもy軸が0のところにずっと線が引かれており学習の様子を可視化できませんでした。学習自体はできており、可視化がだけがうまくいっていないです。なぜうまく行かないのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tiitoi

2018/09/18 13:04

学習できていることはどのように確認したのでしょうか?hist.history['val_acc'] を print すると、どうなりますか?
taiyo2017

2018/09/18 14:23

予測と予測結果が表示されているからです。
taiyo2017

2018/09/18 14:24

hist.history['val_acc']は、[0.0, 0.0, 0.0, 0.0, 0.0, 0.0・・・・0.0]のようになっています
tiitoi

2018/09/18 14:46

model.fit() で学習を開始すると、学習やバリデーションの accuracy が表示されていると思いますが、そちらはちゃんと精度が出ているけど、history の値は全部0ということでしょうか?
taiyo2017

2018/09/19 13:58

はい、そうです
tiitoi

2018/09/19 16:12 編集

model.fit() の返り値について回答にて整理しました。 Keras のコードを確認しましたが、history にある値は、model.fit() のときに表示される値と同じになるはずです。 お手数ですが、`acc` でなく、`val_acc` の値がどうなっているか確認できますか。
guest

回答1

0

ベストアンサー

MNIST 学習サンプルコード

MNIST のサンプルで試してみました。

python

1from keras import backend as K 2from keras.datasets import mnist 3from keras.layers import Activation, BatchNormalization, Dense, Dropout 4from keras.models import Sequential 5from keras.utils.np_utils import to_categorical 6import numpy as np 7 8# MNIST データを取得する。 9(x_train, y_train), (x_test, y_test) = mnist.load_data() 10print('x_train.shape', x_train.shape) # x_train.shape (60000, 28, 28) 11print('y_train.shape', y_train.shape) # y_train.shape (60000,) 12print('x_test.shape', x_test.shape) # x_test.shape (10000, 28, 28) 13print('y_test.shape', y_test.shape) # y_test.shape (10000,) 14 15# 1次元配列にする。 (28, 28) -> (784,) にする 16x_train = x_train.reshape(len(x_train), -1) 17x_test = x_test.reshape(len(x_test), -1) 18 19# one-hot 表現に変換する。 20y_train = to_categorical(y_train) 21y_test = to_categorical(y_test) 22 23# モデルを作成する。 24model = Sequential() 25model.add(Dense(10, input_dim=784)) 26model.add(BatchNormalization()) 27model.add(Activation('relu')) 28model.add(Dense(10)) 29model.add(BatchNormalization()) 30model.add(Activation('relu')) 31model.add(Dense(10)) 32model.add(BatchNormalization()) 33model.add(Activation('softmax')) 34model.compile(optimizer='adam', 35 loss='categorical_crossentropy', 36 metrics=['accuracy']) 37 38# 学習する。 39history = model.fit(x_train, y_train, epochs=10, batch_size=128, 40 validation_data=(x_test, y_test))

model.fit() の返り値

model.fit() は学習が終了すると、keras.callbacks.History オブジェクトを返す。

これの history 属性に辞書で以下のキーで値が入っている。

  • loss: 各エポックの学習誤差
  • val_loss: 各エポックのバリデーション誤差
  • acc: 各エポックの学習精度
  • val_acc: 各エポックのバリデーション精度

バリデーションのログは、model.fit() 時に引数で次のいずれかでバリデーション機能を有効にした場合のみ、キーが存在している。

  • validation_data=(x_test, y_test) でバリデーション用のデータを与える。
  • validation_split で学習データのうち、バリデーション用に使う割合を指定する。

精度のログは、model.compile() 時に引数で metrics=['accuracy'] を指定した場合のみ、キーが存在している。

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

ログをグラフ化するコード

python

1import matplotlib.pyplot as plt 2fig, [axes1, axes2] = plt.subplots(1, 2, figsize=(8, 4)) 3 4epochs = np.arange(1, len(history.history['loss']) + 1) 5 6# 各エポックの誤差の推移 7axes1.set_title('loss') 8axes1.plot(epochs, history.history['loss'], label='train') 9axes1.plot(epochs, history.history['val_loss'], label='validation') 10axes1.set_xticks(epochs) 11 12# 各エポックの精度の推移 13axes2.set_title('accuracy') 14axes2.plot(epochs, history.history['acc'], label='train') 15axes2.plot(epochs, history.history['val_acc'], label='validation') 16axes2.set_xticks(epochs) 17 18plt.show()

イメージ説明

バリデーションの精度がすべて 0 である問題について

model.fit() で学習を開始すると、エポックごとに4つの指標 loss, acc, val_loss, val_acc の値が表示されると思いますが、ここの val_acc の値は本当に 0 になっていませんか? ここの値も 0 になっているなら、実際そのとおりの結果なのだと思います。

Epoch 1/10 60000/60000 [==============================] - 1s 24us/step - loss: 0.9237 - acc: 0.7781 - val_loss: 0.5519 - val_acc: 0.8862 Epoch 2/10 60000/60000 [==============================] - 1s 11us/step - loss: 0.4600 - acc: 0.8953 - val_loss: 0.3564 - val_acc: 0.9146

また以下のコードで history の値も確認してみてください。

from pprint import pprint for key, value in history.history.items(): print('key', key) pprint(value)

投稿2018/09/19 16:06

編集2018/09/19 16:13
tiitoi

総合スコア21956

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

taiyo2017

2018/09/24 01:14 編集

ありがとうございます!今更なのですが、オレンジと青の2つの線がそれぞれどちらを示しているか見分けをつける方法はあるのでしょうか?
tiitoi

2018/09/24 09:45

Axes.legend() を呼び出すことで、plot() した際に label 引数で指定したラベル名が表示できます。 例 ``` axes1.plot(epochs, history.history['loss'], label='train') axes1.plot(epochs, history.history['val_loss'], label='validation') axes1.legend() ```
taiyo2017

2018/09/24 12:05

なるほど!ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問