-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn.py
More file actions
29 lines (25 loc) · 976 Bytes
/
cnn.py
File metadata and controls
29 lines (25 loc) · 976 Bytes
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
#import the libraries
import tensorflow as tf
from keras import layers, models
from keras.datasets import mnist
#load Dataset
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train ,x_test = x_train / 255.0, x_test / 255.0 #Normalize
x_train = x_train.reshape(-1,28,28,1)
x_test = x_test.reshape(-1, 28, 28, 1)
model = models.Sequential([
layers.Conv2D(32,(3,3), activation = 'relu', input_shape=(28 ,28 ,1)),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3), activation = 'relu'),
layers.MaxPooling2D((2,2)),
layers.Flatten(),
layers.Dense(64, activation = 'relu'),
layers.Dense(10, activation = 'softmax')
])
#compile the model
model.compile(optimizer='adam', loss ='sparse_categorical_crossentropy', metrics = ['accuracy'])
#train the model
model.fit(x_train, y_train, epochs=5, batch_size = 64, validation_split = 0.2)
#evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test Accuracy: ", test_acc)