-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel subclass.py
More file actions
43 lines (36 loc) · 1.3 KB
/
model subclass.py
File metadata and controls
43 lines (36 loc) · 1.3 KB
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
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import mnist
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1).astype("float32") / 255.0
x_test = x_test.reshape(-1, 28, 28, 1).astype("float32") / 255.0
class CNNBlock(layers.Layer):
def __init__(self, out_channels, kernel_size=3):
super(CNNBlock, self).__init__()
self.conv = layers.Conv2D(out_channels, kernel_size, padding='same')
self.bn = layers.BatchNormalization()
def call(self, input_tensor, training=False):
x = self.conv(input_tensor)
print(x.shape)
x = self.bn(x, training=training)
x = tf.nn.relu(x)
return x
model = keras.Sequential(
[
CNNBlock(32),
CNNBlock(64),
CNNBlock(128),
layers.Flatten(),
layers.Dense(10)
]
)
model.compile(
optimizer=keras.optimizers.Adam(),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'],
)
model.fit(x_train, y_train, epochs=10, batch_size=64, verbose=1)
model.evaluate(x_test, y_test, batch_size=64, verbose=2)