-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn.py
More file actions
48 lines (40 loc) · 1.46 KB
/
cnn.py
File metadata and controls
48 lines (40 loc) · 1.46 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
'''Importing necessary libraries'''
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
'''Preparing sample data'''
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train = X_train.astype('float32')/255.0
X_train = np.expand_dims(X_train, -1)
X_test = X_test.astype('float32')/255.0
X_test = np.expand_dims(X_test, -1)
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)
'''Defining CNN Model'''
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D((2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(10, activation='softmax')
])
'''Compile Model'''
model.compile(optimizer='adam',
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
'''Train Model'''
train = model.fit(X_train, y_train, epochs=3, batch_size=32)
'''Plotting the results'''
plt.figure(figsize=(12,4))
plt.subplot(1,2,1)
plt.plot(train.history['accuracy'])
plt.title('Accuracy from Mnist dataset using CNN')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.subplot(1,2,2)
plt.plot(train.history['loss'])
plt.title('Loss from Mnist dataset using CNN')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.show()