-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_basic_model.py
More file actions
54 lines (40 loc) · 1.57 KB
/
Copy pathbuild_basic_model.py
File metadata and controls
54 lines (40 loc) · 1.57 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
44
45
46
47
48
49
50
51
52
53
54
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import datasets
#Data Preprocessing
(train_x, train_y), (test_x, test_y) = datasets.mnist.load_data()
train_x = train_x[..., tf.newaxis]
train_y = tf.keras.utils.to_categorical(train_y, 10)
#Modeling
##Feature Extraction
inputs = layers.Input((28, 28, 1), name="input_layer")
net = layers.Conv2D(32, (3,3), padding="SAME")(inputs)
net = layers.Activation('relu')(net)
net = layers.Conv2D(32, (3, 3), padding = "SAME")(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D((2, 2))(net)
net = layers.Dropout(0.25)(net)
net = layers.Conv2D(64, (3,3), padding="SAME")(net)
net = layers.Activation('relu')(net)
net = layers.Conv2D(64, (3, 3), padding = "SAME")(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D((2, 2))(net)
net = layers.Dropout(0.25)(net) # (None, 7, 7, 64)
## Classification
net = layers.Flatten()(net) # (None, 3136(노드, 픽셀)) 3136 = 7 * 7 * 64
net = layers.Dense(512)(net)
net = layers.Activation('relu')(net)
net = layers.Dropout(0.5)(net)
net = layers.Dense(10)(net) # Number of classes
net = layers.Activation('softmax')(net)
## Model made
model = tf.keras.Model(inputs=inputs, outputs=net, name='Basic_CNN')
# Optimization Setting
model.compile(loss = tf.keras.losses.categorical_crossentropy,
optimizer = tf.keras.optimizers.Adam(),
metrics = [tf.keras.metrics.Accuracy()])
# Training
model.fit(train_x, train_y,validation_data=(test_x, test_y),
batch_size = 32,
shuffle = True,
epochs = 1)