Ann 2
Ann 2
Ann 2
In [1]:
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np
In [2]:
Out[2]:
In [3]:
X_test.shape
Out[3]:
In [4]:
y_train.shape
Out[4]:
(50000, 1)
In [5]:
y_train[:5]
Out[5]:
array([[6],
[9],
[9],
[4],
[1]], dtype=uint8)
In [6]:
y_train = y_train.reshape(-1,)
y_train[:5]
Out[6]:
In [7]:
y_test = y_test.reshape(-1,)
In [8]:
classes = ["airplane","automobile","bird","cat","deer","dog","frog","horse","ship
In [9]:
In [10]:
plot_sample(X_train, y_train, 0)
In [11]:
plot_sample(X_train, y_train, 1)
In [12]:
In [13]:
ann = models.Sequential([
layers.Flatten(input_shape=(32,32,3)),
layers.Dense(3000, activation='relu'),
layers.Dense(1000, activation='relu'),
layers.Dense(10, activation='softmax')
])
ann.compile(optimizer='SGD',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Epoch 1/5
1563/1563 [==============================] - 35s 22ms/step - loss:
1.8191 - accuracy: 0.3525
Epoch 2/5
1563/1563 [==============================] - 36s 23ms/step - loss:
1.6282 - accuracy: 0.4256
Epoch 3/5
1563/1563 [==============================] - 36s 23ms/step - loss:
1.5428 - accuracy: 0.4542
Epoch 4/5
1563/1563 [==============================] - 36s 23ms/step - loss:
1.4840 - accuracy: 0.4775
Epoch 5/5
1563/1563 [==============================] - 35s 23ms/step - loss:
1.4327 - accuracy: 0.4975
Out[13]:
<keras.src.callbacks.History at 0x7fd814637040>
In [14]:
In [15]:
cnn = models.Sequential([
layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
In [16]:
cnn.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
In [17]:
Epoch 1/10
1563/1563 [==============================] - 19s 12ms/step - loss:
1.4756 - accuracy: 0.4655
Epoch 2/10
1563/1563 [==============================] - 19s 12ms/step - loss:
1.1272 - accuracy: 0.6052
Epoch 3/10
1563/1563 [==============================] - 19s 12ms/step - loss:
0.9880 - accuracy: 0.6554
Epoch 4/10
1563/1563 [==============================] - 19s 12ms/step - loss:
0.8955 - accuracy: 0.6899
Epoch 5/10
1563/1563 [==============================] - 19s 12ms/step - loss:
0.8287 - accuracy: 0.7119
Epoch 6/10
1563/1563 [==============================] - 18s 12ms/step - loss:
0.7684 - accuracy: 0.7345
Epoch 7/10
1563/1563 [==============================] - 19s 12ms/step - loss:
0.7228 - accuracy: 0.7503
Epoch 8/10
1563/1563 [==============================] - 18s 11ms/step - loss:
0.6735 - accuracy: 0.7650
Epoch 9/10
1563/1563 [==============================] - 19s 12ms/step - loss:
0.6355 - accuracy: 0.7797
Epoch 10/10
1563/1563 [==============================] - 19s 12ms/step - loss:
0.5936 - accuracy: 0.7939
Out[17]:
<keras.src.callbacks.History at 0x7fd71df65be0>
In [18]:
cnn.evaluate(X_test,y_test)
Out[18]:
[0.9819189310073853, 0.6866000294685364]
In [19]:
y_pred = cnn.predict(X_test)
y_pred[:5]
Out[19]:
In [20]:
Out[20]:
[3, 8, 0, 0, 4]
In [21]:
y_test[:5]
Out[21]:
In [22]:
plot_sample(X_test, y_test,3)
In [23]:
classes[y_classes[3]]
Out[23]:
'airplane'
In [ ]: