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

回答編集履歴

2

修正

2020/05/08 05:29

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -5,7 +5,7 @@
5
5
  ## 追記
6
6
 
7
7
  そういう仕様ですね。
8
- fit() で学習した場合はサンプル数の進捗、fit_generator() で学習した場合は1エポックあたりの反復回数になります。
8
+ 表示される進捗は fit() で学習した場合はサンプル数、fit_generator() で学習した場合は1エポックあたりの反復回数になります。
9
9
 
10
10
  ```python
11
11
  import numpy as np

1

修正

2020/05/08 05:29

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -1,3 +1,61 @@
1
1
  そこに表示されている312というのはサンプル数でなく、そのエポックの反復回数です。
2
2
  各エポックの反復回数は学習データの全サンプル数 / バッチサイズです。
3
- 今回の場合、 40000//128=312
3
+ 今回の場合、 40000//128=312
4
+
5
+ ## 追記
6
+
7
+ そういう仕様ですね。
8
+ fit() で学習した場合はサンプル数の進捗、fit_generator() で学習した場合は1エポックあたりの反復回数になります。
9
+
10
+ ```python
11
+ import numpy as np
12
+ from tensorflow.keras.datasets import mnist
13
+ from tensorflow.keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D
14
+ from tensorflow.keras.models import Sequential
15
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
16
+ from tensorflow.keras.utils import to_categorical
17
+
18
+ (x_train, y_train), (x_test, y_test) = mnist.load_data()
19
+
20
+ x_train = np.reshape(x_train, [-1, 28, 28, 1])
21
+ y_train = to_categorical(y_train)
22
+ y_test = to_categorical(y_test)
23
+
24
+ # we train our network using float data
25
+ x_train = x_train.astype("float32") / 255
26
+ x_test = x_test.astype("float32") / 255
27
+
28
+ # network parameters
29
+ batch_size = 128
30
+ hidden_units = 256
31
+ data_augmentation = False
32
+ epochs = 20
33
+ max_batches = len(x_train) / batch_size
34
+
35
+ model = Sequential()
36
+ model.add(Conv2D(32, kernel_size=(3, 3), activation="relu", input_shape=(28, 28, 1)))
37
+ model.add(Conv2D(64, (3, 3), activation="relu"))
38
+ model.add(MaxPooling2D(pool_size=(2, 2)))
39
+ model.add(Dropout(0.25))
40
+ model.add(Flatten())
41
+ model.add(Dense(128, activation="relu"))
42
+ model.add(Dropout(0.5))
43
+ model.add(Dense(10, activation="softmax"))
44
+ model.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=["accuracy"])
45
+
46
+ if not data_augmentation:
47
+ print("Not using data augmentation.")
48
+ model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size)
49
+ else:
50
+ print("Using real-time data augmentation.")
51
+ datagen = ImageDataGenerator(
52
+ featurewise_center=True,
53
+ featurewise_std_normalization=True,
54
+ rotation_range=20,
55
+ width_shift_range=0.2,
56
+ height_shift_range=0.2,
57
+ horizontal_flip=False,
58
+ )
59
+ datagen.fit(x_train)
60
+ model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size))
61
+ ```