-
Notifications
You must be signed in to change notification settings - Fork 194
/
mixed-precision_training.py
63 lines (44 loc) · 1.44 KB
/
mixed-precision_training.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Youtube Link => https://youtu.be/Kvy65eGF2Bs
import tensorflow as tf
from tensorflow import keras
def mp_model():
model = keras.Sequential(
[
keras.layers.Flatten(input_shape=(32, 32, 3)),
keras.layers.Dense(3000, activation="relu"),
keras.layers.Dense(1000, activation="relu"),
keras.layers.Dense(
10,
),
keras.layers.Activation("sigmoid", dtype="float32"),
]
)
model.compile(
optimizer="SGD", loss="categorical_crossentropy", metrics=["accuracy"]
)
return model
# Dense(units) => units: Positive integer, dimensionality of the output space.
# Note Thus, we had to overwrite the policy for the last layer to
# float32. We will se why in a moment.
tf.keras.mixed_precision.set_global_policy("mixed_floats16")
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()
multi_class_cifar_10 = [
"airplane",
"automobile",
"bird",
"cat",
"deer",
"dog",
"frog",
"horse",
"ship",
"truck",
]
X_train_scaled = X_train / 255
X_test_scaled = X_test / 255
y_train_categorical = keras.utils.to_categorical(y_train, num_classes=10, dtype="float")
y_test_categorical = keras.utils.to_categorical(y_test, num_classes=10, dtype=10)
with tf.device("/GPU:0"):
model = mp_model()
model.fit(X_test_scaled, y_test_categorical)
model.evaluate(X_test_scaled, y_test_categorical)