回答編集履歴
2
修正
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
そういう仕様ですね。
|
14
14
|
|
15
|
-
fit() で学習した場合はサンプル数
|
15
|
+
表示される進捗は fit() で学習した場合はサンプル数、fit_generator() で学習した場合は1エポックあたりの反復回数になります。
|
16
16
|
|
17
17
|
|
18
18
|
|
1
修正
test
CHANGED
@@ -3,3 +3,119 @@
|
|
3
3
|
各エポックの反復回数は学習データの全サンプル数 / バッチサイズです。
|
4
4
|
|
5
5
|
今回の場合、 40000//128=312
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
## 追記
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
そういう仕様ですね。
|
14
|
+
|
15
|
+
fit() で学習した場合はサンプル数の進捗、fit_generator() で学習した場合は1エポックあたりの反復回数になります。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
```python
|
20
|
+
|
21
|
+
import numpy as np
|
22
|
+
|
23
|
+
from tensorflow.keras.datasets import mnist
|
24
|
+
|
25
|
+
from tensorflow.keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D
|
26
|
+
|
27
|
+
from tensorflow.keras.models import Sequential
|
28
|
+
|
29
|
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
30
|
+
|
31
|
+
from tensorflow.keras.utils import to_categorical
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
x_train = np.reshape(x_train, [-1, 28, 28, 1])
|
40
|
+
|
41
|
+
y_train = to_categorical(y_train)
|
42
|
+
|
43
|
+
y_test = to_categorical(y_test)
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
# we train our network using float data
|
48
|
+
|
49
|
+
x_train = x_train.astype("float32") / 255
|
50
|
+
|
51
|
+
x_test = x_test.astype("float32") / 255
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
# network parameters
|
56
|
+
|
57
|
+
batch_size = 128
|
58
|
+
|
59
|
+
hidden_units = 256
|
60
|
+
|
61
|
+
data_augmentation = False
|
62
|
+
|
63
|
+
epochs = 20
|
64
|
+
|
65
|
+
max_batches = len(x_train) / batch_size
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
model = Sequential()
|
70
|
+
|
71
|
+
model.add(Conv2D(32, kernel_size=(3, 3), activation="relu", input_shape=(28, 28, 1)))
|
72
|
+
|
73
|
+
model.add(Conv2D(64, (3, 3), activation="relu"))
|
74
|
+
|
75
|
+
model.add(MaxPooling2D(pool_size=(2, 2)))
|
76
|
+
|
77
|
+
model.add(Dropout(0.25))
|
78
|
+
|
79
|
+
model.add(Flatten())
|
80
|
+
|
81
|
+
model.add(Dense(128, activation="relu"))
|
82
|
+
|
83
|
+
model.add(Dropout(0.5))
|
84
|
+
|
85
|
+
model.add(Dense(10, activation="softmax"))
|
86
|
+
|
87
|
+
model.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=["accuracy"])
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
if not data_augmentation:
|
92
|
+
|
93
|
+
print("Not using data augmentation.")
|
94
|
+
|
95
|
+
model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size)
|
96
|
+
|
97
|
+
else:
|
98
|
+
|
99
|
+
print("Using real-time data augmentation.")
|
100
|
+
|
101
|
+
datagen = ImageDataGenerator(
|
102
|
+
|
103
|
+
featurewise_center=True,
|
104
|
+
|
105
|
+
featurewise_std_normalization=True,
|
106
|
+
|
107
|
+
rotation_range=20,
|
108
|
+
|
109
|
+
width_shift_range=0.2,
|
110
|
+
|
111
|
+
height_shift_range=0.2,
|
112
|
+
|
113
|
+
horizontal_flip=False,
|
114
|
+
|
115
|
+
)
|
116
|
+
|
117
|
+
datagen.fit(x_train)
|
118
|
+
|
119
|
+
model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size))
|
120
|
+
|
121
|
+
```
|