質問編集履歴

2

修正

2019/07/17 23:05

投稿

wrkig
wrkig

score0

test CHANGED
File without changes
test CHANGED
@@ -191,3 +191,159 @@
191
191
 
192
192
 
193
193
  何かわかることがございましたら、ご教示いただければ幸いです。
194
+
195
+
196
+
197
+
198
+
199
+ ---
200
+
201
+
202
+
203
+ **追記**
204
+
205
+
206
+
207
+ `plt.plot`の前に、`print`文を挿入してみたのですが、出力は上と変わらずでした。どうやら`model.fit`の行で処理が終わっているようです。
208
+
209
+ さらに、別セルで`print(history)`で確認したのですが、"__NameError: name 'history' is not defined__" となって`model.fit`が`history`に代入されていませんでした。
210
+
211
+ ```Python
212
+
213
+ import numpy as np
214
+
215
+ import matplotlib.pyplot as plt
216
+
217
+ from keras.datasets import mnist
218
+
219
+ from keras.layers import Activation, Dense, Dropout
220
+
221
+ from keras.models import Sequential, load_model
222
+
223
+ from keras.utils.np_utils import to_categorical
224
+
225
+ from keras import optimizers
226
+
227
+ from keras import models
228
+
229
+ from keras import layers
230
+
231
+ import tensorflow
232
+
233
+ import tensorboard
234
+
235
+ from livelossplot import PlotLossesKeras
236
+
237
+
238
+
239
+ %matplotlib inline
240
+
241
+
242
+
243
+ (X_train, y_train), (X_test, y_test) = mnist.load_data()
244
+
245
+
246
+
247
+ X_train = X_train.reshape(X_train.shape[0], 784)[:6000]
248
+
249
+ X_test = X_test.reshape(X_test.shape[0], 784)[:1000]
250
+
251
+ y_train = to_categorical(y_train)[:6000]
252
+
253
+ y_test = to_categorical(y_test)[:1000]
254
+
255
+
256
+
257
+ model = Sequential()
258
+
259
+ model.add(Dense(256, input_dim=784))
260
+
261
+ model.add(Activation("sigmoid"))
262
+
263
+ model.add(Dense(128))
264
+
265
+ model.add(Activation("sigmoid"))
266
+
267
+
268
+
269
+ model.add(Dropout(rate=0.5))
270
+
271
+
272
+
273
+ model.add(Dense(10))
274
+
275
+ model.add(Activation("softmax"))
276
+
277
+
278
+
279
+ sgd = optimizers.SGD(lr=0.1)
280
+
281
+
282
+
283
+ model.compile(optimizer=sgd, loss="categorical_crossentropy", metrics=["accuracy"])
284
+
285
+
286
+
287
+ # callbacks = [PlotLossesKeras()]
288
+
289
+
290
+
291
+ history = model.fit(
292
+
293
+ X_train,
294
+
295
+ y_train,
296
+
297
+ batch_size=32,
298
+
299
+ epochs=5,
300
+
301
+ verbose=1,
302
+
303
+ validation_data=(X_test, y_test),
304
+
305
+ # callbacks=[PlotLossesKeras()]
306
+
307
+ )
308
+
309
+
310
+
311
+ acc = history.history["acc"]
312
+
313
+ val_acc = history.history["val_acc"]
314
+
315
+ loss = history.history["loss"]
316
+
317
+ val_loss = history.history["val_loss"]
318
+
319
+
320
+
321
+ epochs = range(1, len(acc) + 1)
322
+
323
+
324
+
325
+
326
+
327
+ print("-" * 10)
328
+
329
+ print(epochs)
330
+
331
+ print(history)
332
+
333
+ print("-" * 10)
334
+
335
+
336
+
337
+
338
+
339
+ plt.plot(epochs, acc, label="acc", ls="-", marker="o")
340
+
341
+ plt.plot(epochs, val_acc, label="val_acc", ls="-", marker="x")
342
+
343
+ plt.ylabel("accuracy")
344
+
345
+ plt.xlabel("epoch")
346
+
347
+ plt.legend(loc="best")
348
+
349
+ ```

1

追記

2019/07/17 22:49

投稿

wrkig
wrkig

score0

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- 環境は macOS version:10.14.5、python3.7でjupyter labを使っています。
5
+ 環境は macOS version:10.14.5でjupyter labを使っています。
6
6
 
7
7
 
8
8
 
@@ -191,163 +191,3 @@
191
191
 
192
192
 
193
193
  何かわかることがございましたら、ご教示いただければ幸いです。
194
-
195
-
196
-
197
-
198
-
199
- ---
200
-
201
-
202
-
203
- **追記**
204
-
205
-
206
-
207
- `plt.plot([1, 5], [1, 10])`を書いてみたのですが、グラフが表示されませんでした。
208
-
209
- pythonやkerasのバージョンが原因で表示されないことはあるのでしょうか?
210
-
211
-
212
-
213
- ```Python
214
-
215
- import numpy as np
216
-
217
- import matplotlib.pyplot as plt
218
-
219
- from keras.datasets import mnist
220
-
221
- from keras.layers import Activation, Dense, Dropout
222
-
223
- from keras.models import Sequential, load_model
224
-
225
- from keras.utils.np_utils import to_categorical
226
-
227
- from keras import optimizers
228
-
229
- from keras import models
230
-
231
- from keras import layers
232
-
233
- import tensorflow
234
-
235
- import tensorboard
236
-
237
- from livelossplot import PlotLossesKeras
238
-
239
-
240
-
241
- %matplotlib inline
242
-
243
-
244
-
245
- (X_train, y_train), (X_test, y_test) = mnist.load_data()
246
-
247
-
248
-
249
- X_train = X_train.reshape(X_train.shape[0], 784)[:6000]
250
-
251
- X_test = X_test.reshape(X_test.shape[0], 784)[:1000]
252
-
253
- y_train = to_categorical(y_train)[:6000]
254
-
255
- y_test = to_categorical(y_test)[:1000]
256
-
257
-
258
-
259
- model = Sequential()
260
-
261
- model.add(Dense(256, input_dim=784))
262
-
263
- model.add(Activation("sigmoid"))
264
-
265
- model.add(Dense(128))
266
-
267
- model.add(Activation("sigmoid"))
268
-
269
-
270
-
271
- model.add(Dropout(rate=0.5))
272
-
273
-
274
-
275
- model.add(Dense(10))
276
-
277
- model.add(Activation("softmax"))
278
-
279
-
280
-
281
- sgd = optimizers.SGD(lr=0.1)
282
-
283
-
284
-
285
- model.compile(optimizer=sgd, loss="categorical_crossentropy", metrics=["accuracy"])
286
-
287
-
288
-
289
- # callbacks = [PlotLossesKeras()]
290
-
291
-
292
-
293
- history = model.fit(
294
-
295
- X_train,
296
-
297
- y_train,
298
-
299
- batch_size=32,
300
-
301
- epochs=5,
302
-
303
- verbose=1,
304
-
305
- validation_data=(X_test, y_test),
306
-
307
- # callbacks=[PlotLossesKeras()]
308
-
309
- )
310
-
311
-
312
-
313
- acc = history.history["acc"]
314
-
315
- val_acc = history.history["val_acc"]
316
-
317
- loss = history.history["loss"]
318
-
319
- val_loss = history.history["val_loss"]
320
-
321
-
322
-
323
- epochs = range(1, len(acc) + 1)
324
-
325
-
326
-
327
-
328
-
329
- print("-" * 20)
330
-
331
- print(epochs)
332
-
333
- print(history)
334
-
335
- print("-" * 20)
336
-
337
-
338
-
339
-
340
-
341
- plt.plot([1, 5], [1, 10]) # 追記
342
-
343
- plt.plot(epochs, acc, label="acc", ls="-", marker="o")
344
-
345
- plt.plot(epochs, val_acc, label="val_acc", ls="-", marker="x")
346
-
347
- plt.ylabel("accuracy")
348
-
349
- plt.xlabel("epoch")
350
-
351
- plt.legend(loc="best")
352
-
353
- ```