-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn.py
45 lines (36 loc) · 1.3 KB
/
cnn.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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow import keras
from tensorflow.keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPooling2D
# Load MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Normalize input data
x_train = x_train / 255.0
x_test = x_test / 255.0
# One-hot encode labels
y_train_cat = keras.utils.to_categorical(y_train, 10)
y_test_cat = keras.utils.to_categorical(y_test, 10)
# Add channel dimension
x_train = np.expand_dims(x_train, axis=3)
x_test = np.expand_dims(x_test, axis=3)
# Define the model architecture
model = keras.Sequential([
Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2), strides=2),
Conv2D(64, (3, 3), padding='same', activation='relu'),
MaxPooling2D((2, 2), strides=2),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Train the model
history = model.fit(x_train, y_train_cat, batch_size=32, epochs=5, validation_split=0.2)
# Evaluate the model on the test set
model.evaluate(x_test, y_test_cat)