Ann 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

24/05/2023, 17:37 Assignment No.

9 FINAL - Jupyter Notebook

In [1]:

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np

2023-05-17 11:47:28.854513: I tensorflow/core/util/port.cc:110] one


DNN custom operations are on. You may see slightly different numeri
cal results due to floating-point round-off errors from different c
omputation orders. To turn them off, set the environment variable `
TF_ENABLE_ONEDNN_OPTS=0`.
2023-05-17 11:47:28.855506: I tensorflow/tsl/cuda/cudart_stub.cc:2
8] Could not find cuda drivers on your machine, GPU will not be use
d.
2023-05-17 11:47:28.873775: E tensorflow/compiler/xla/stream_execut
or/cuda/cuda_dnn.cc:7704] Unable to register cuDNN factory: Attempt
ing to register factory for plugin cuDNN when one has already been
registered
2023-05-17 11:47:28.873794: E tensorflow/compiler/xla/stream_execut
or/cuda/cuda_fft.cc:609] Unable to register cuFFT factory: Attempti
ng to register factory for plugin cuFFT when one has already been r
egistered
2023-05-17 11:47:28.873798: E tensorflow/compiler/xla/stream_execut
or/cuda/cuda_blas.cc:1520] Unable to register cuBLAS factory: Attem
pting to register factory for plugin cuBLAS when one has already be
en registered
2023-05-17 11:47:28.877004: I tensorflow/tsl/cuda/cudart_stub.cc:2
8] Could not find cuda drivers on your machine, GPU will not be use
d.
2023-05-17 11:47:28.877479: I tensorflow/core/platform/cpu_feature_
guard.cc:182] This TensorFlow binary is optimized to use available
CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other o
perations, rebuild TensorFlow with the appropriate compiler flags.
2023-05-17 11:47:29.267976: W tensorflow/compiler/tf2tensorrt/util
s/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

In [2]:

(X_train, y_train), (X_test,y_test) = datasets.cifar10.load_data()


X_train.shape

Out[2]:

(50000, 32, 32, 3)

In [3]:

X_test.shape

Out[3]:

(10000, 32, 32, 3)

localhost:8888/notebooks/Untitled Folder 1/Assignment No. 9 FINAL.ipynb 1/8


24/05/2023, 17:37 Assignment No. 9 FINAL - Jupyter Notebook

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]:

array([6, 9, 9, 4, 1], dtype=uint8)

In [7]:

y_test = y_test.reshape(-1,)

In [8]:

classes = ["airplane","automobile","bird","cat","deer","dog","frog","horse","ship

In [9]:

def plot_sample(X, y, index):


plt.figure(figsize = (15,2))
plt.imshow(X[index])
plt.xlabel(classes[y[index]])

In [10]:

plot_sample(X_train, y_train, 0)

localhost:8888/notebooks/Untitled Folder 1/Assignment No. 9 FINAL.ipynb 2/8


24/05/2023, 17:37 Assignment No. 9 FINAL - Jupyter Notebook

In [11]:

plot_sample(X_train, y_train, 1)

In [12]:

X_train = X_train / 255.0


X_test = X_test / 255.0

localhost:8888/notebooks/Untitled Folder 1/Assignment No. 9 FINAL.ipynb 3/8


24/05/2023, 17:37 Assignment No. 9 FINAL - Jupyter Notebook

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'])

ann.fit(X_train, y_train, epochs=5)

2023-05-17 11:47:30.552107: I tensorflow/compiler/xla/stream_execut


or/cuda/cuda_gpu_executor.cc:995] successful NUMA node read from Sy
sFS had negative value (-1), but there must be at least one NUMA no
de, so returning NUMA node zero. See more at https://github.com/tor
valds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-
L355 (https://github.com/torvalds/linux/blob/v6.0/Documentation/AB
I/testing/sysfs-bus-pci#L344-L355)
2023-05-17 11:47:30.552319: W tensorflow/core/common_runtime/gpu/gp
u_device.cc:1960] Cannot dlopen some GPU libraries. Please make sur
e the missing libraries mentioned above are installed properly if y
ou would like to use GPU. Follow the guide at https://www.tensorflo
w.org/install/gpu (https://www.tensorflow.org/install/gpu) for how
to download and setup the required libraries for your platform.
Skipping registering GPU devices...

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>

localhost:8888/notebooks/Untitled Folder 1/Assignment No. 9 FINAL.ipynb 4/8


24/05/2023, 17:37 Assignment No. 9 FINAL - Jupyter Notebook

In [14]:

from sklearn.metrics import confusion_matrix , classification_report


import numpy as np
y_pred = ann.predict(X_test)
y_pred_classes = [np.argmax(element) for element in y_pred]

print("Classification Report: \n", classification_report(y_test, y_pred_classes))

313/313 [==============================] - 3s 8ms/step


Classification Report:
precision recall f1-score support

0 0.64 0.43 0.52 1000


1 0.72 0.48 0.57 1000
2 0.39 0.33 0.35 1000
3 0.37 0.24 0.29 1000
4 0.48 0.30 0.37 1000
5 0.41 0.38 0.40 1000
6 0.49 0.61 0.54 1000
7 0.35 0.78 0.48 1000
8 0.57 0.69 0.62 1000
9 0.58 0.56 0.57 1000

accuracy 0.48 10000


macro avg 0.50 0.48 0.47 10000
weighted avg 0.50 0.48 0.47 10000

In [15]:

cnn = models.Sequential([
layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=
layers.MaxPooling2D((2, 2)),

layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),


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'])

localhost:8888/notebooks/Untitled Folder 1/Assignment No. 9 FINAL.ipynb 5/8


24/05/2023, 17:37 Assignment No. 9 FINAL - Jupyter Notebook

In [17]:

cnn.fit(X_train, y_train, epochs=10)

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)

313/313 [==============================] - 2s 5ms/step - loss: 0.98


19 - accuracy: 0.6866

Out[18]:

[0.9819189310073853, 0.6866000294685364]

localhost:8888/notebooks/Untitled Folder 1/Assignment No. 9 FINAL.ipynb 6/8


24/05/2023, 17:37 Assignment No. 9 FINAL - Jupyter Notebook

In [19]:

y_pred = cnn.predict(X_test)
y_pred[:5]

313/313 [==============================] - 1s 4ms/step

Out[19]:

array([[1.5296830e-02, 3.7161630e-04, 2.0790633e-03, 6.7601961e-01,


6.7103736e-04, 2.6613161e-01, 5.2719600e-03, 2.7515048e-03,
3.1405263e-02, 1.5254221e-06],
[5.3905547e-02, 3.9434269e-02, 5.0401150e-07, 9.8367352e-08,
1.2282582e-09, 9.1655368e-09, 6.4237095e-11, 3.0244050e-07,
9.0665001e-01, 9.1841266e-06],
[4.5537379e-01, 4.8484314e-02, 2.0938353e-03, 1.4523463e-02,
8.7027416e-05, 1.2265679e-03, 6.1702876e-06, 5.0438374e-02,
4.2089793e-01, 6.8685529e-03],
[9.8998147e-01, 5.0581153e-03, 1.3736077e-04, 1.1132373e-03,
4.8387710e-06, 1.9321926e-05, 4.4951407e-06, 7.3398351e-06,
3.6501633e-03, 2.3631919e-05],
[7.7832448e-07, 1.1846597e-05, 1.6902879e-02, 6.1331887e-02,
8.4704083e-01, 5.6822794e-03, 6.8861082e-02, 3.2818556e-05,
1.3540218e-04, 1.2205140e-07]], dtype=float32)

In [20]:

y_classes = [np.argmax(element) for element in y_pred]


y_classes[:5]

Out[20]:

[3, 8, 0, 0, 4]

In [21]:

y_test[:5]

Out[21]:

array([3, 8, 8, 0, 6], dtype=uint8)

In [22]:

plot_sample(X_test, y_test,3)

localhost:8888/notebooks/Untitled Folder 1/Assignment No. 9 FINAL.ipynb 7/8


24/05/2023, 17:37 Assignment No. 9 FINAL - Jupyter Notebook

In [23]:

classes[y_classes[3]]

Out[23]:

'airplane'

In [ ]:

localhost:8888/notebooks/Untitled Folder 1/Assignment No. 9 FINAL.ipynb 8/8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy