teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

修正

2019/07/17 23:05

投稿

wrkig
wrkig

スコア15

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  Kerasでhistoryをグラフに出力したいのですが、学習の数値だけでグラフが出ません。
2
2
 
3
- 環境は macOS version:10.14.5でjupyter labを使っています。
3
+ 環境は macOS version:10.14.5、python3.7でjupyter labを使っています。
4
4
 
5
5
  ```Python
6
6
  import numpy as np
@@ -101,8 +101,9 @@
101
101
 
102
102
  **追記**
103
103
 
104
- `plt.plot`の前に、`print`文挿入してみたのですが、出力は上と変わらずでした。どうやら`model.fit`の行で処理が終わっているようです。
104
+ `plt.plot([1, 5], [1, 10])`を書いてみたのですが、グラフが表示されませんでした。
105
- さらに、別セルで`print(history)`で確認したです、"__NameError: name 'history' is not defined__" となって`model.fit`が`history`に代入されませんでした。
105
+ pythonやkerasバージョン原因で表示されことはあるのでしょうか?
106
+
106
107
  ```Python
107
108
  import numpy as np
108
109
  import matplotlib.pyplot as plt
@@ -161,12 +162,13 @@
161
162
  epochs = range(1, len(acc) + 1)
162
163
 
163
164
 
164
- print("-" * 10)
165
+ print("-" * 20)
165
166
  print(epochs)
166
167
  print(history)
167
- print("-" * 10)
168
+ print("-" * 20)
168
169
 
169
170
 
171
+ plt.plot([1, 5], [1, 10]) # 追記
170
172
  plt.plot(epochs, acc, label="acc", ls="-", marker="o")
171
173
  plt.plot(epochs, val_acc, label="val_acc", ls="-", marker="x")
172
174
  plt.ylabel("accuracy")

1

追記

2019/07/17 23:05

投稿

wrkig
wrkig

スコア15

title CHANGED
File without changes
body CHANGED
@@ -94,4 +94,82 @@
94
94
  ```
95
95
  となっています。
96
96
 
97
- 何かわかることがございましたら、ご教示いただければ幸いです。
97
+ 何かわかることがございましたら、ご教示いただければ幸いです。
98
+
99
+
100
+ ---
101
+
102
+ **追記**
103
+
104
+ `plt.plot`の前に、`print`文を挿入してみたのですが、出力は上と変わらずでした。どうやら`model.fit`の行で処理が終わっているようです。
105
+ さらに、別セルで`print(history)`で確認したのですが、"__NameError: name 'history' is not defined__" となって`model.fit`が`history`に代入されていませんでした。
106
+ ```Python
107
+ import numpy as np
108
+ import matplotlib.pyplot as plt
109
+ from keras.datasets import mnist
110
+ from keras.layers import Activation, Dense, Dropout
111
+ from keras.models import Sequential, load_model
112
+ from keras.utils.np_utils import to_categorical
113
+ from keras import optimizers
114
+ from keras import models
115
+ from keras import layers
116
+ import tensorflow
117
+ import tensorboard
118
+ from livelossplot import PlotLossesKeras
119
+
120
+ %matplotlib inline
121
+
122
+ (X_train, y_train), (X_test, y_test) = mnist.load_data()
123
+
124
+ X_train = X_train.reshape(X_train.shape[0], 784)[:6000]
125
+ X_test = X_test.reshape(X_test.shape[0], 784)[:1000]
126
+ y_train = to_categorical(y_train)[:6000]
127
+ y_test = to_categorical(y_test)[:1000]
128
+
129
+ model = Sequential()
130
+ model.add(Dense(256, input_dim=784))
131
+ model.add(Activation("sigmoid"))
132
+ model.add(Dense(128))
133
+ model.add(Activation("sigmoid"))
134
+
135
+ model.add(Dropout(rate=0.5))
136
+
137
+ model.add(Dense(10))
138
+ model.add(Activation("softmax"))
139
+
140
+ sgd = optimizers.SGD(lr=0.1)
141
+
142
+ model.compile(optimizer=sgd, loss="categorical_crossentropy", metrics=["accuracy"])
143
+
144
+ # callbacks = [PlotLossesKeras()]
145
+
146
+ history = model.fit(
147
+ X_train,
148
+ y_train,
149
+ batch_size=32,
150
+ epochs=5,
151
+ verbose=1,
152
+ validation_data=(X_test, y_test),
153
+ # callbacks=[PlotLossesKeras()]
154
+ )
155
+
156
+ acc = history.history["acc"]
157
+ val_acc = history.history["val_acc"]
158
+ loss = history.history["loss"]
159
+ val_loss = history.history["val_loss"]
160
+
161
+ epochs = range(1, len(acc) + 1)
162
+
163
+
164
+ print("-" * 10)
165
+ print(epochs)
166
+ print(history)
167
+ print("-" * 10)
168
+
169
+
170
+ plt.plot(epochs, acc, label="acc", ls="-", marker="o")
171
+ plt.plot(epochs, val_acc, label="val_acc", ls="-", marker="x")
172
+ plt.ylabel("accuracy")
173
+ plt.xlabel("epoch")
174
+ plt.legend(loc="best")
175
+ ```