ADL Exp File
ADL Exp File
SESSION: 2023-24
Aim: Implement multilayer perceptron algorithm for MNIST hand written Digit
Classification.
Theory:
Solution:
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.utils import to_categorical
Aim: Design a neural network for classifying movie reviews (Binary Classification) using
IMDB dataset.
Theory:
● Neural Network: Neural networks are a set of algorithms, modeled loosely after the
human brain, that are designed to recognize patterns. They interpret sensory data through
a kind of machine perception, labeling or clustering raw input.
● Binary Classification: Binary or binomial classification is the task of classifying the
elements of a given set into two groups on the basis of a classification rule.
● IMDB Dataset: The IMDB dataset is a set of 50,000 highly polarized reviews from the
Internet Movie Database. They are split into 25,000 reviews for training and 25,000
reviews for testing, each set consisting of 50% negative and 50% positive reviews.
Solution:
import numpy as np
from tensorflow.keras.datasets import imdb
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Embedding, Flatten
from tensorflow.keras.preprocessing import sequence
Output:
Epoch 8/10
782/782 ━━━━━━━━━━━━━━━━━━━━ 6s 8ms/step - accuracy:
1.0000 - loss: 5.8364e-06 - val_accuracy: 0.8632 - val_loss: 0.8073
Epoch 9/10
782/782 ━━━━━━━━━━━━━━━━━━━━ 6s 8ms/step - accuracy:
1.0000 - loss: 3.8537e-06 - val_accuracy: 0.8640 - val_loss: 0.8306
Epoch 10/10
782/782 ━━━━━━━━━━━━━━━━━━━━ 7s 9ms/step - accuracy:
1.0000 - loss: 2.3734e-06 - val_accuracy: 0.8638 - val_loss: 0.8557
EXPERIMENT 3
Aim: Design a neural network for classifying news wires (Multi class classification) using
Reuters dataset.
Theory:
● Neural Network: Neural networks are a set of algorithms, modeled loosely after the
human brain, that are designed to recognize patterns. They interpret sensory data
through a kind of machine perception, labeling or clustering raw input.
● Multi-class Classification: Multi-class or multinomial classification is the problem of
classifying instances into one of three or more classes.
● Reuters Dataset: The Reuters dataset is a set of short newswires and their topics,
published by Reuters in 1986. It’s a simple, widely used toy dataset for text
classification. There are 46 different topics; some topics are more represented than
others, but each topic has at least 10 examples in the training set.
Solution:
import numpy as np
from tensorflow.keras.datasets import reuters
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Embedding, Flatten
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
Output:
Epoch 8/10
281/281 ━━━━━━━━━━━━━━━━━━━━ 5s 19ms/step - accuracy:
0.9537 - loss: 0.1214 - val_accuracy: 0.7124 - val_loss: 2.1270
Epoch 9/10
281/281 ━━━━━━━━━━━━━━━━━━━━ 5s 18ms/step - accuracy:
0.9576 - loss: 0.1118 - val_accuracy: 0.7039 - val_loss: 2.1985
Epoch 10/10
281/281 ━━━━━━━━━━━━━━━━━━━━ 5s 19ms/step - accuracy:
0.9544 - loss: 0.1278 - val_accuracy: 0.7079 - val_loss: 2.1972
EXPERIMENT 4
Aim: Design a neural network for predicting house price using Boston Housing price
dataset.
Theory:
● Neural Network: Neural networks are a set of algorithms, modeled loosely after the
human brain, that are designed to recognize patterns. They interpret sensory data
through a kind of machine perception, labeling or clustering raw input.
● Boston Housing Price Dataset: The Boston Housing Dataset is a derived from
information collected by the U.S. Census Service concerning housing in the area of
Boston Mass. It was obtained from the StatLib archive and has been used extensively
throughout the literature to benchmark algorithms.
Solution:
import numpy as np
from tensorflow.keras.datasets import boston_housing
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
# Make predictions
predictions = model.predict(x_test_scaled)
Output:
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 1ms/step - loss: 20.9702
Test loss: 24.972816467285156
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step
array([[ 8.239275 ], [15.192989 ], [18.558802 ], [29.45781 ],
[23.854698 ], [12.648457 ], [23.888613 ], [19.909472 ], [17.175808 ],
[16.581171 ], [15.793694 ], [16.692366 ], [15.752364 ], [37.596016 ],
[14.654897 ], [17.384644 ], [22.42454 ], [18.738693 ], [15.301411 ],
24.503408 ], [10.921483 ], [12.033248 ], [17.442163 ], [11.698573 ],
EXPERIMENT 5
Aim: Build a Convolutional Neural Network for MNIST Hand written digit classification.
Theory:
● Convolutional Neural Network (CNN): CNNs are a class of deep learning models,
most commonly applied to analyzing visual imagery. They are designed to
automatically and adaptively learn spatial hierarchies of features from the training
data.
● MNIST Dataset: The MNIST database (Modified National Institute of Standards and
Technology database) is a large database of handwritten digits that is commonly used
for training various image processing systems. The database is also widely used for
training and testing in the field of machine learning.
Solution:
import tensorflow as tf
import matplotlib.pyplot as plt
import seaborn as sn
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import math
import datetime
import platform
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
train.head()
train.info(), train.shape
test.info(), test.shape
X = train.iloc[:, 1:785]
y = train.iloc[:, 0]
tsne_res = tsne.fit_transform(X_tsn)
plt.figure(figsize=(14, 12))
plt.scatter(tsne_res[:,0], tsne_res[:,1], c=y, s=2)
plt.xticks([])
plt.yticks([])
plt.colorbar();
from sklearn.model_selection import train_test_split
X_train, X_validation, y_train, y_validation = train_test_split(X, y,
test_size = 0.2,random_state = 1212)
print('X_train:', X_train.shape)
print('y_train:', y_train.shape)
print('X_validation:', X_validation.shape)
print('y_validation:', y_validation.shape)
x_train_re = X_train.to_numpy().reshape(33600, 28, 28)
y_train_re = y_train.values
x_validation_re = X_validation.to_numpy().reshape(8400, 28, 28)
y_validation_re = y_validation.values
x_test_re = test.to_numpy().reshape(28000, 28, 28)
print('x_train:', x_train_re.shape)
print('y_train:', y_train_re.shape)
print('x_validation:', x_validation_re.shape)
print('y_validation:', y_validation_re.shape)
print('x_test:', x_test_re.shape)
# Save image parameters to the constants that we will use later for
data re-shaping and for model traning.
(_, IMAGE_WIDTH, IMAGE_HEIGHT) = x_train_re.shape
IMAGE_CHANNELS = 1
print('IMAGE_WIDTH:', IMAGE_WIDTH);
print('IMAGE_HEIGHT:', IMAGE_HEIGHT);
print('IMAGE_CHANNELS:', IMAGE_CHANNELS);
pd.DataFrame(x_train_re[0])
plt.imshow(x_train_re[0], cmap=plt.cm.binary)
plt.show()
numbers_to_display = 100
num_cells = math.ceil(math.sqrt(numbers_to_display))
plt.figure(figsize=(20,20))
for i in range(numbers_to_display):
plt.subplot(num_cells, num_cells, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(x_train_re[i], cmap=plt.cm.binary)
plt.xlabel(y_train_re[i])
plt.show()
x_train_with_chanels = x_train_re.reshape(
x_train_re.shape[0],
IMAGE_WIDTH,
IMAGE_HEIGHT,
IMAGE_CHANNELS
)
x_validation_with_chanels = x_validation_re.reshape(
x_validation_re.shape[0],
IMAGE_WIDTH,
IMAGE_HEIGHT,
IMAGE_CHANNELS
)
x_test_with_chanels = x_test_re.reshape(
x_test_re.shape[0],
IMAGE_WIDTH,
IMAGE_HEIGHT,
IMAGE_CHANNELS
)
print('x_train_with_chanels:', x_train_with_chanels.shape)
print('x_validation_with_chanels:', x_validation_with_chanels.shape)
print('x_test_with_chanels:', x_test_with_chanels.shape)
x_train_normalized = x_train_with_chanels / 255
x_validation_normalized = x_validation_with_chanels / 255
x_test_normalized = x_test_with_chanels / 255
# Let's check just one row from the 0th image to see color chanel
values after normalization.
x_train_normalized[0][10]
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Convolution2D(
input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS),
kernel_size=5,
filters=8,
strides=1,
activation=tf.keras.activations.relu,
kernel_initializer=tf.keras.initializers.VarianceScaling()
))
model.add(tf.keras.layers.MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2)
))
model.add(tf.keras.layers.Convolution2D(
kernel_size=5,
filters=16,
strides=1,
activation=tf.keras.activations.relu,
kernel_initializer=tf.keras.initializers.VarianceScaling()
))
model.add(tf.keras.layers.MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2)
))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(
units=128,
activation=tf.keras.activations.relu
));
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Dense(
units=10,
activation=tf.keras.activations.softmax,
kernel_initializer=tf.keras.initializers.VarianceScaling()
))
model.summary()
tf.keras.utils.plot_model(
model,
show_shapes=True,
show_layer_names=True,
)
adam_optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(
optimizer=adam_optimizer,
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy']
)
log_dir=".logs/fit/" +
datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback =
tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
training_history = model.fit(
x_train_normalized,
y_train_re,
epochs=10,
validation_data=(x_validation_normalized, y_validation_re),
callbacks=[tensorboard_callback]
)
plt.imshow(x_validation_normalized[plot_index].reshape((IMAGE_WIDTH,
IMAGE_HEIGHT)), cmap=color_map)
plt.xlabel(predicted_label)
plt.subplots_adjust(hspace=1, wspace=0.5)
plt.show()
confusion_matrix = tf.math.confusion_matrix(y_validation_re,
predictions)
f, ax = plt.subplots(figsize=(9, 7))
sn.heatmap(
confusion_matrix,
annot=True,
linewidths=.5,
fmt="d",
square=True,
ax=ax
)
plt.show()
predictions_one_hot = loaded_model.predict([x_test_normalized])
print('predictions_one_hot:', predictions_one_hot.shape)
pd.DataFrame(predictions_one_hot)
plt.imshow(x_test_normalized[0].reshape((IMAGE_WIDTH, IMAGE_HEIGHT)),
cmap=plt.cm.binary)
plt.show()
test_pred = pd.DataFrame( loaded_model.predict([x_test_normalized]))
test_pred = pd.DataFrame(test_pred.idxmax(axis = 1))
test_pred.index.name = 'ImageId'
test_pred = test_pred.rename(columns = {0: 'Label'}).reset_index()
test_pred['ImageId'] = test_pred['ImageId'] + 1
test_pred.head()
Output:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 24, 24, 8) 208
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 12, 12, 8) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 8, 8, 16) 3216
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 4, 4, 16) 0
_________________________________________________________________
flatten (Flatten) (None, 256) 0
_________________________________________________________________
dense (Dense) (None, 128) 32896
_________________________________________________________________
dropout (Dropout) (None, 128) 0
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 37,610
Trainable params: 37,610
Non-trainable params: 0
_________________________________________________________________
Epoch 7/10
1050/1050 [==============================] - 7s 7ms/step - loss: 0.0332
- accuracy: 0.9891 - val_loss: 0.0505 - val_accuracy: 0.9869
Epoch 8/10
1050/1050 [==============================] - 7s 7ms/step - loss: 0.0272
- accuracy: 0.9911 - val_loss: 0.0467 - val_accuracy: 0.9873
Epoch 9/10
1050/1050 [==============================] - 7s 7ms/step - loss: 0.0246
- accuracy: 0.9920 - val_loss: 0.0493 - val_accuracy: 0.9869
Epoch 10/10
1050/1050 [==============================] - 7s 7ms/step - loss: 0.0218
- accuracy: 0.9925 - val_loss: 0.0462 - val_accuracy: 0.9882
The model has successfully trained
Train loss: 0.009457636624574661
Train accuracy: 0.997083306312561
Validation loss: 0.04620610177516937
Validation accuracy: 0.9882143139839172
EXPERIMENT 6
Aim: Build a convolutional neural network for simple image (dogs and cats)
Classification.
Theory:
● Convolutional Neural Network (CNN): CNNs are a class of deep learning models,
most commonly applied to analyzing visual imagery. They are designed to
automatically and adaptively learn spatial hierarchies of features from the training
data.
● Image Classification: Image classification is the process of predicting the class of an
input image. In this case, the classes are ‘dog’ and ‘cat’.
Solution:
import os
import keras
import tensorflow as tf
import random
import numpy as np
from os import path
import shutil
data_path = 'train.zip'
shutil.move(src_path_cats, dest_path_cats)
shutil.move(src_path_dogs, dest_path_dogs)
shutil.move(src_path_cats, dest_path_cats)
shutil.move(src_path_dogs, dest_path_dogs)
Flatten(),
Dense(256, activation='relu'),
Dense(256, activation='relu'),
Dense(1, activation='sigmoid')
]
)
AUTOTUNE = tf.data.experimental.AUTOTUNE
train_ds = train_ds.prefetch(buffer_size=AUTOTUNE)
test_ds = test_ds.prefetch(buffer_size=AUTOTUNE)
model.summary()
history = model.fit(train_ds, validation_data=test_ds, epochs=30)
plt.plot(history.history["accuracy"])
plt.plot(history.history['val_accuracy'])
plt.plot(history.history['loss'])
plt.title("Model accuracy")
plt.ylabel("Accuracy")
plt.xlabel("era")
plt.legend(["accuracy", "val_accuracy", "loss"])
print(history.history.keys())
plt.show()
print("Training Accuracy: ", model.evaluate(train_ds, verbose=None)[1])
print("Validation Accuracy: ", model.evaluate(test_ds,
verbose=None)[1])
from keras.preprocessing import image
prediction_img_paths = []
prediction_img_paths.extend(path.join(train_cat_dir,
random.choice(train_cats)) for _ in range(4))
prediction_img_paths.extend(path.join(train_dog_dir,
random.choice(train_dogs)) for _ in range(4))
prediction_imgs = []
print(prediction_img_paths)
fig, ax = plt.subplots(2,4,figsize=(20,12))
Model: "sequential"
313/313 ━━━━━━━━━━━━━━━━━━━━ 45s 144ms/step - accuracy:
0.9941 - loss: 0.0168 - val_accuracy: 0.7218 - val_loss: 89.6814
Epoch 29/30
313/313 ━━━━━━━━━━━━━━━━━━━━ 45s 144ms/step - accuracy:
0.9945 - loss: 0.0197 - val_accuracy: 0.7096 - val_loss: 113.2724
Epoch 30/30
313/313 ━━━━━━━━━━━━━━━━━━━━ 45s 142ms/step - accuracy:
0.9965 - loss: 0.0157 - val_accuracy: 0.7038 - val_loss: 255.2161
EXPERIMENT 7
Aim: Use a pre-trained convolutional neural network (VGG-16) for image classification.
Theory:
● Convolutional Neural Network (CNN): CNNs are a class of deep learning models,
most commonly applied to analyzing visual imagery. They are designed to
automatically and adaptively learn spatial hierarchies of features from the training
data.
● VGG-16: VGG-16 is a convolutional neural network model proposed by K. Simonyan
and A. Zisserman from the University of Oxford in the paper “Very Deep
Convolutional Networks for Large-Scale Image Recognition”. The model achieves
92.7% top-5 test accuracy in ImageNet, which is a dataset of over 14 million images
belonging to 1000 classes.
● Image Classification: Image classification is the process of predicting the class of an
input image.
Solution:
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input,
decode_predictions
import numpy as np
# Normalize the input image's pixel values to the range used when
training the neural network
x = preprocess_input(x)
# Run the image through the deep neural network to make a prediction
predictions = model.predict(x)
import PIL
from PIL import Image
img = Image.open('C:\\Users\\shubh\\Downloads\\th.jpeg')
print(img.show())
print("This is an image of:")
Output:
Original Image -
None
Theory:
● One-Hot Encoding: One-hot is a group of bits among which the legal combinations of
values are only those with a single high (1) bit and all the others low (0). In natural
language processing, one-hot encoding is a commonly used method to represent words
or characters as vectors where each word or character is represented as a binary vector
with all zeros except for a single one.
Solution:
import numpy as np
samples = ['The cat sat on the mat.', 'The dog ate my homework.']
# Build an index of all tokens in the data.
token_index = {}
for sample in samples:
# Tokenize the samples via the `split` method.
# In real life, we would also strip punctuation and special
characters from the samples.
for word in sample.split():
if word not in token_index:
# Assign a unique index to each unique word. Note that we
don't attribute index 0 to anything.
token_index[word] = len(token_index) + 1
samples = ['The cat sat on the mat.', 'The dog ate my homework.']
characters = string.printable # All printable ASCII characters.
token_index = dict(zip(characters, range(1, len(characters) + 1)))
max_length = 50
results = np.zeros((len(samples), max_length, max(token_index.values())
+ 1))
for i, sample in enumerate(samples):
for j, character in enumerate(sample[:max_length]):
index = token_index.get(character)
results[i, j, index] = 1.
print(results.shape)
from tensorflow.keras.preprocessing.text import Tokenizer
samples = ['The cat sat on the mat.', 'The dog ate my homework.']
Output:
(2, 10, 11)
[[0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
(2, 50, 101)
Found 9 unique tokens: {'the': 1, 'cat': 2, 'sat': 3, 'on': 4, 'mat':
5, 'dog': 6, 'ate': 7, 'my': 8, 'homework': 9}
(2, 10, 1000)
EXPERIMENT 9
Theory:
● Word Embeddings: Word embeddings are a type of word representation that allows
words with similar meaning to have a similar representation. They are a distributed
representation for text that is perhaps one of the key breakthroughs for the impressive
performance of deep learning methods on challenging natural language processing
problems.
● IMDB Dataset: The IMDB dataset is a set of 50,000 highly polarized reviews from
the Internet Movie Database. They are split into 25,000 reviews for training and
25,000 reviews for testing, each set consisting of 50% negative and 50% positive
reviews.
Solution:
import numpy as np
import itertools
import os.path
import keras
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense, Flatten, Dropout
from keras.layers import Embedding
#from keras.layers.convolutional import Conv1D, MaxPooling1D
from keras.preprocessing import sequence
from sklearn import decomposition
import matplotlib.pyplot as plt
np.random.seed(2018)
vocab_size = 2000
(X_train, y_train), (X_test, y_test) =
imdb.load_data(num_words=vocab_size)
vocab = set(itertools.chain.from_iterable(X_train))
print(len(vocab))
# pad dataset to a maximum review length in words
max_length = 500
X_train = sequence.pad_sequences(X_train, maxlen=max_length)
X_test = sequence.pad_sequences(X_test, maxlen=max_length)
y_train = keras.utils.to_categorical(y_train, 2)
y_test = keras.utils.to_categorical(y_test, 2)
# id to word
word_to_id = keras.datasets.imdb.get_word_index()
word_to_id["<PAD>"] = 0
word_to_id["<START>"] = 1
word_to_id["<UNK>"] = 2
id_to_word = {value:key for key,value in word_to_id.items()}
if os.path.exists("model_weights.h5"):
model.load_weights("model_weights.h5")
else:
# fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test),
epochs=4, batch_size=64, verbose=2)
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
print('Accuracy: %f' % (accuracy*100))
model.save_weights("model_weights.h5")
# plot
weights = model.layers[0].get_weights()[0]
pca = decomposition.PCA(n_components=2)
pca.fit(weights.T)
fig, ax = plt.subplots()
ax.scatter(pca.components_[0], pca.components_[1])
for i in vocab:
word = id_to_word[i]
ax.annotate(word, (pca.components_[0, i],pca.components_[1, i]))
fig.savefig('embedding.png')
plt.show()
Output:
EXPERIMENT 10
Aim: Implement a RNN for IMDB movie review classification problem.
Theory:
● Recurrent Neural Network (RNN): RNNs are a class of artificial neural networks
where connections between nodes form a directed graph along a temporal sequence.
This allows it to exhibit temporal dynamic behavior. Unlike feedforward neural
networks, RNNs can use their internal state (memory) to process sequences of inputs.
● IMDB Dataset: The IMDB dataset is a set of 50,000 highly polarized reviews from
the Internet Movie Database. They are split into 25,000 reviews for training and
25,000 reviews for testing, each set consisting of 50% negative and 50% positive
reviews.
Solution:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
decoded_review = whatItSay()
decoded_review = whatItSay(5)
#Preprocess
num_words = 15000
(X_train,Y_train),(X_test,Y_test) = imdb.load_data(num_words=num_words)
maxlen=130
X_train = pad_sequences(X_train, maxlen=maxlen)
X_test = pad_sequences(X_test, maxlen=maxlen)
print("X train shape: ",X_train.shape)
print(X_train[5])
for i in X_train[0:10]:
print(len(i))
decoded_review = whatItSay(5)
#Construct RNN Model
rnn = Sequential()
rnn.add(Embedding(num_words,32,input_length =len(X_train[0]))) #
num_words=15000
rnn.add(SimpleRNN(16,input_shape = (num_words,maxlen),
return_sequences=False,activation="relu"))
rnn.add(Dense(1)) #flatten
rnn.add(Activation("sigmoid")) #using sigmoid for binary classification
print(rnn.summary())
rnn.compile(loss="binary_crossentropy",optimizer="rmsprop",metrics=["ac
curacy"])
history = rnn.fit(X_train,Y_train,validation_data =
(X_test,Y_test),epochs = 5,batch_size=128,verbose = 1)
score = rnn.evaluate(X_test,Y_test)
print("accuracy:", score[1]*100)
plt.figure()
plt.plot(history.history["accuracy"],label="Train");
plt.plot(history.history["val_accuracy"],label="Test");
plt.title("Accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epochs")
plt.legend()
plt.show();
plt.figure()
plt.plot(history.history["loss"],label="Train");
plt.plot(history.history["val_loss"],label="Test");
plt.title("Loss")
plt.ylabel("Loss")
plt.xlabel("Epochs")
plt.legend()
plt.show()
Output:
EXPERIMENT 11
Aim: Image Classification: Building a deep learning model that can classify images into
different categories, such as animals, cars, or buildings.
Theory:
● Deep Learning: Deep learning is a machine learning technique that teaches computers
to do what comes naturally to humans: learn by example. Deep learning is a key
technology behind driverless cars, enabling them to recognize a stop sign, or to
distinguish a pedestrian from a lamppost.
● Image Classification: Image classification is the process of predicting the class of an
input image. The classes in this case are ‘animals’, ‘cars’, and ‘buildings’.
Solution:
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models, datasets
import matplotlib.pyplot as plt
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])