0% found this document useful (0 votes)
22 views

Lab Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lab Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

INDEX

S.No. Particulars Page No.

1 Syllabus

2 Task Plan

3 Implementation of a Multiclass classification model for grey-scale image

4 Implementation of a Convolutional Neural Network (CNN) classification


model

5
Construct a Convnet model for image classification

6 Application of Transfer Learning to build an image classification model

7 Implement a simple Generative Adversarial Network (GAN

8 Implementation of word embedding using RNN

9 Implementation of Auto encoders

10 Implementation of prediction using LSTM networks

11 Implementation of object detection and localization

12 Apply optimization techniques to improve model performance

13
TASK 1

IMPLEMENTATION OF MULTICLASS CLASSIFICATION FOR GRAY SCALE IMAGES

Aim:

To use tensorflow for implementation of multiclass classification for gray scale image.

Procedure:

Step 1: import the required libraries

Step 2: load & preprocess the dataset

Step 3: define the model architecture

Step 4: compile the model

Step 5: train the model

Step 6: evaluate the model using test data

Step 7: using visualization techniques for determining the performance metrics

Program
import tensorflow as tf
from tensorflow.keras import datasets
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras import models, layers
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10)) model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 8))
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
plt.savefig("output_report.png")

OUTPUT:
Epoch 1/10
1563/1563 [==============================] - 80s 50ms/step - loss: 1.5129 - accuracy:
0.4485 - val_loss: 1.2795 - val_accuracy: 0.5465
Epoch 2/10
1563/1563 [==============================] - 76s 49ms/step - loss: 1.1397 - accuracy:
0.5965 - val_loss: 1.0586 - val_accuracy: 0.6189
Epoch 3/10
1563/1563 [==============================] - 75s 48ms/step - loss: 0.9859 - accuracy:
0.6548 - val_loss: 0.9609 - val_accuracy: 0.6641
Epoch 4/10
1563/1563 [==============================] - 78s 50ms/step - loss: 0.8883 - accuracy:
0.6898 - val_loss: 0.9351 - val_accuracy: 0.6731
Epoch 5/10
1563/1563 [==============================] - 76s 49ms/step - loss: 0.8145 - accuracy:
0.7147 - val_loss: 0.8953 - val_accuracy: 0.6889
Epoch 6/10
1563/1563 [==============================] - 76s 49ms/step - loss: 0.7576 - accuracy:
0.7353 - val_loss: 0.8864 - val_accuracy: 0.6949
Epoch 7/10
1563/1563 [==============================] - 76s 49ms/step - loss: 0.7055 - accuracy:
0.7533 - val_loss: 0.8584 - val_accuracy: 0.7112
Epoch 8/10
1563/1563 [==============================] - 75s 48ms/step - loss: 0.6600 - accuracy:
0.7691 - val_loss: 0.9043 - val_accuracy: 0.6992
Epoch 9/10
1563/1563 [==============================] - 75s 48ms/step - loss: 0.6200 - accuracy:
0.7819 - val_loss: 0.8517 - val_accuracy: 0.7128
Epoch 10/10
1563/1563 [==============================] - 73s 47ms/step - loss: 0.5821 - accuracy:
0.7948 - val_loss: 0.9044 - val_accuracy: 0.7027
[]

Result:
Thus the Implementation of multiclass classification for gray scale image is successfully
completed.

TASK 2

IMPLEMENTATION OF A CONVOLUTIONAL NEURAL NETWORK CLASSIFICATION


MODEL USING TENSORFLOW

Aim:

To implement convolutional neural network using tensor flow


Procedure:

Step 1: Import the required libraries

Step 2: Load the preprocess dataset

Step 3: Define the model architecture

Step 4: Compile & train the model

Step 5: Evaluate the model using test data

Step 6: Using visualization techniques for determining the performance metrics


Program:
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers
import Dropout from tensorflow.keras.layers import Dense
(X_train,y_train) , (X_test,y_test)=mnist.load_data()
X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], X_train.shape[2], 1)) X_test =
X_test.reshape((X_test.shape[0],X_test.shape[1],X_test.shape[2],1)) print(X_train.shape)
print(X_test.shape) X_train=X_train/255 X_test=X_test/255 model=Sequential()
model.add(Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)))
model.add(MaxPool2D(2,2))
model.add(Flatten())
model.add(Dense(100,activation='relu'))
model.add(Dense(10,activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(X_train,y_train,epochs=10)
model.evaluate(X_test,y_test)

OUTPUT:
Epoch 1/10
1875/1875 [==============================] - 43s 23ms/step - loss: 0.1620 - accuracy:
0.9506
Epoch 2/10
1875/1875 [==============================] - 43s 23ms/step - loss: 0.0565 - accuracy:
0.9823
Epoch 3/10
1875/1875 [==============================] - 41s 22ms/step - loss: 0.0374 - accuracy:
0.9883
Epoch 4/10
1875/1875 [==============================] - 41s 22ms/step - loss: 0.0249 - accuracy:
0.9923
Epoch 5/10
1875/1875 [==============================] - 41s 22ms/step - loss: 0.0169 - accuracy:
0.9947
Epoch 6/10
1875/1875 [==============================] - 41s 22ms/step - loss: 0.0133 - accuracy:
0.9957
Epoch 7/10
1875/1875 [==============================] - 42s 22ms/step - loss: 0.0090 - accuracy:
0.9972
Epoch 8/10
1875/1875 [==============================] - 42s 23ms/step - loss: 0.0074 - accuracy:
0.9977
Epoch 9/10
1875/1875 [==============================] - 40s 21ms/step - loss: 0.0055 - accuracy:
0.9982
Epoch 10/10
1875/1875 [==============================] - 40s 21ms/step - loss: 0.0052 - accuracy:
0.9983 [ ]
Result:
Thus the Implementing convolutional neural network using tensor flow is successfully
completed.
TASK 3

ALEXNET FROM SCRATCH IN TENSORFLOW . PY

Aim:

To create alexnet from scratch in tensorflow.py

Procedure:

Step 1: Import the necessary libraries

Step 2: Define the alexnet architecture as a function

Step 3: Define the input shape & number of classes

Step 4: Create an instance of alexnet

Step 5: Print the model summary


Program :
import tensorflow_datasets as tfds
(train_dataset, test_dataset), info = tfds.load(
'cats_vs_dogs',
split = ('train[:80%]', 'train[80%:]'),
with_info = True,
as_supervised=True)
len(train_dataset), len(test_dataset)
for X, y in train_dataset:
print(X.shape, y.numpy())
image_1 = X.numpy()
break
import matplotlib.pyplot as plt
plt.imshow(image_1)
import tensorflow as tf
def normalize_img(image, label):
return (tf.cast(image, tf.float32) / 255.0, label)
def resize(image, label):
return (tf.image.resize(image, (224, 224)), label)
train_dataset = train_dataset.map(resize, num_parallel_calls=tf.data.AUTOTUNE)
train_dataset = train_dataset.map(normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
SHUFFLE_VAL = len(train_dataset) // 1000 # Divide by big value on free Colab
BATCH_SIZE = 4 # Use small batch size on free Colab
train_dataset = train_dataset.shuffle(SHUFFLE_VAL)
train_dataset = train_dataset.batch(BATCH_SIZE)
train_dataset = train_dataset.prefetch(tf.data.AUTOTUNE)
test_dataset = test_dataset.map(resize, num_parallel_calls=tf.data.AUTOTUNE)
test_dataset = test_dataset.map(normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
test_dataset = test_dataset.batch(BATCH_SIZE)
test_dataset = test_dataset.prefetch(tf.data.AUTOTUNE)
for (img, label) in train_dataset:
print(img.numpy().shape, label.numpy()

break
from tensorflow.keras import layers
from tensorflow.keras.models import Model
def AlexNet():
inp = layers.Input((224, 224, 3))
x = layers.Conv2D(96, 11, 4, activation='relu')(inp)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(3, 2)(x)
x = layers.Conv2D(256, 5, 1, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.MaxPooling2D(3, 2)(x)
x = layers.Conv2D(384, 3, 1, activation='relu')(x)
x = layers.Conv2D(384, 3, 1, activation='relu')(x)
x = layers.Conv2D(256, 3, 1, activation='relu')(x)
x = layers.MaxPooling2D(3, 2)(x)
x = layers.Flatten()(x)
x = layers.Dense(4096, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(4096, activation='relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(1, activation='sigmoid')(x)
model = Model(inputs=inp, outputs=x)
return model
model = AlexNet()
model.summary()
tf.keras.utils.plot_model(
model,
to_file='model.png',
show_shapes=True,
show_dtype=False,
show_layer_names=False,
show_layer_activations=True,
dpi=100

for (img, label) in train_dataset:


print(model(img).numpy().shape, label.numpy())
break
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import BinaryCrossentropy
model.compile(loss=BinaryCrossentropy(),
optimizer=Adam(learning_rate=0.001), metrics=['accuracy'])
from tensorflow.keras.callbacks import EarlyStopping
es = EarlyStopping(patience=5,
monitor='loss')
model.fit(train_dataset, epochs=10, validation_data=test_dataset,
callbacks=[es])
OUTPUT:
Epoch 1/10
4653/4653 [==============================] - 2197s 472ms/step - loss: 0.7012 - accuracy:
0.5031 - val_loss: 0.6931 - val_accuracy: 0.5099
Epoch 2/10
4653/4653 [==============================] - 2128s 457ms/step - loss: 0.6934 - accuracy:
0.4988 - val_loss: 0.6933 - val_accuracy: 0.4901
Epoch 3/10
4653/4653 [==============================] - 2112s 454ms/step - loss: 0.7023 - accuracy:
0.5008 - val_loss: 0.6936 - val_accuracy: 0.5099
Epoch 4/10
4653/4653 [==============================] - 2148s 462ms/step - loss: 0.6939 - accuracy:
0.5039 - val_loss: 0.6930 - val_accuracy: 0.5099
Epoch 5/10
4653/4653 [==============================] - 2141s 460ms/step - loss: 0.6934 - accuracy:
0.5008 - val_loss: 0.6932 - val_accuracy: 0.4901
Epoch 6/10

4653/4653
[==============================] -
2146s 461ms/step - loss: 0.6935 - accuracy:

0.5002 - val_loss: 0.6933 - val_accuracy: 0.4901


Epoch 7/10
4653/4653 [==============================] - 2169s 466ms/step - loss: 0.6933 - accuracy:
0.5021 - val_loss: 0.6933 - val_accuracy: 0.4901
Epoch 8/10
4653/4653 [==============================] - 2143s 461ms/step - loss: 0.6933 - accuracy:
0.5001 - val_loss: 0.6934 - val_accuracy: 0.4901
Epoch 9/10
4653/4653 [==============================] - 2133s 458ms/step - loss: 0.6932 - accuracy:
0.5030 - val_loss: 0.6933 - val_accuracy: 0.4901
Epoch 10/10
4653/4653 [==============================] - 2126s 457ms/step - loss: 0.6932 - accuracy:
0.5029 - val_loss: 0.6933 - val_accuracy: 0.4901
Result:
Thus, the Creating alexnet from scratch in tensorflow is successfully completed.

TASK 4

VGG16 PRETRAINED MODEL

Aim:

To create vgg16 pretrained model from transfer learning.

Procedure:

Step 1: Import the necessary libraries.

Step 2: Define different layers using keras

Step 3: Define the path of dataset

Step 4: Display output of dataset in a given path

Step 5: Define function as vgg16( )

Step 6: Print model summary

Program:
#pytefrom google.colab import drive
#drive.mount('/content/drive')
from google.colab import drive
drive.mount('/content/drive')
import cv2
import numpy as np
import os
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense,Flatten,Conv2D,Activation,Dropout
from keras import backend as K
import keras
from keras.models import Sequential, Model
from keras.models import load_model
#from keras.optimizers import SGD
from keras.callbacks import EarlyStopping,ModelCheckpoint
from keras.layers import MaxPool2D
from google.colab.patches import cv2_imshow
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
#K.tensorflow_backend._get_available_gpus()
import keras.backend as K
train_path="/content/drive/MyDrive/archive/train"
test_path="/content/drive/MyDrive/archive/test"
class_names=os.listdir(train_path)
class_names_test=os.listdir(test_path)
print(class_names)
print(class_names_test)
#Sample datasets images
image_criminal=cv2.imread("/content/drive/MyDrive/archive/test/ALTAMIRA
YELLOWTHROAT/1.jpg")
cv2_imshow(image_criminal)
image_normal=cv2.imread("/content/drive/MyDrive/archive/test/AMERICAN AVOCET/1.jpg")

cv2_imshow(image_normal)
image_Suspicious=cv2.imread("/content/drive/MyDrive/archive/test/AMERICAN BITTERN/2.jpg")
cv2_imshow(image_Suspicious)
train_datagen =
ImageDataGenerator(zoom_range=0.15,width_shift_range=0.2,height_shift_range=0.2,shear_range=0
.15)
test_datagen = ImageDataGenerator()
def VGG16():
model = Sequential()
model.add(Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,3),padding="same",
activation="relu"))
model.add(Conv2D(filters=64,kernel_size=(3,3),padding="same", activation="relu"))

model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2),name='vgg16'))
model.add(Flatten(name='flatten'))
model.add(Dense(256, activation='relu', name='fc1'))
model.add(Dense(128, activation='relu', name='fc2'))
model.add(Dense(3, activation='softmax', name='output'))
return model

model=VGG16()
model.summary()
Vgg16 = Model(inputs=model.input, outputs=model.get_layer('vgg16').output)
Vgg16.load_weights("/content/drive/MyDrive/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5"
)

for layer in Vgg16.layers:


layer.trainable = False
!apt vggmodel
import tensorflow as tf
from tensorflow.keras.applications import VGG16
vggmodel = VGG16(weights='imagenet', include_top=True)
# Freeze the first 19 layers (up to the 19th index)
for layer in vggmodel.layers[:19]:
layer.trainable = False
# Print the summary to check the layer status
vggmodel.summary()
for layers in (vggmodel.layers)[:19]:
print(layers)
layers.trainable = False
for layer in model.layers:
print(layer, layer.trainable)
!pip install matplotlib-venn
!apt-get -qq install -y libfluidsynth1
#https://pypi.python.org/pypi/libarchive
!apt-get -qq install -y libarchive-dev && pip install -U libarchive
import libarchive
#https://pypi.python.org/pypi/pydot
!apt-get -qq install -y graphviz && pip install pydot
import pydot
#from keras.optimizers import SGD
from tensorflow.keras.optimizers import Adam, SGD, RMSprop
pip install Optimizers
opt = SGD(lr=1e-4, momentum=0.9)
model.compile(loss="multi_crossentropy", optimizer=opt,metrics=["accuracy"])
es=EarlyStopping(monitor='val_accuracy', mode='max', verbose=1, patience=20)
mc = ModelCheckpoint('/content/drive/MyDrive/RealTime/model.h5', monitor='val_accuracy',
mode='max', save_best_only=True)
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
H=
model.fit_generator(train_generator,validation_data=test_generator,epochs=10,verbose=1,callbacks=[
mc,es])
model.load_weights("/content/drive/MyDrive/model.h5")
model.evaluate_generator(test_generataor)
model_json = model.to_json()
with open("/content/drive/MyDrive/model.json","w") as json_file:
json_file.write(model_json)
from keras.model import model_from_json
def predict_(image_path):
#Load the Model from Json File
json_file = open('/content/drive/MyDrive/model.json', 'r')
model_json_c = json_file.read()
json_file.close()
model_c = model_from_json(model_json_c)
#Load the weights
model_c.load_weights("/content/drive/MyDrive/model.h5")
#Compile the model

opt = SGD(lr=1e-4, momentum=0.9)


model_c.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"])
#load the image you want to classify
image = cv2.imread(image_path)
image = cv2.resize(image, (224,224))
cv2_imshow(image)
#predict the image
preds = model_c.predict(np.expand_dims(image, axis=0))[0]
print(preds)

'''

if(preds==0):
print("Predicted Label:AMERICAN KESTREL")
if(preds==1):
print("Predicted Label:AMERICAN GOLDFINCH")
else:
print("Predicted Label: AMERICAN FLAMINGO")
'''
['AMERICAN KESTREL', 'AMERICAN GOLDFINCH', 'AMERICAN FLAMINGO']
#Perform Classification
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
predict_("/content/drive/MyDrive/vggTL/data/test/ALTAMIRA YELLOWTHROAT/4.jpg")
predict_("/content/drive/MyDrive/vggTL/data/test/AMERICAN AVOCET/5.jpg")
predict_("/content/drive/MyDrive/vggTL/data/test/AMERICAN BITTERN/4.jpg")
test_path="/content/drive/MyDrive/test"
class_names_test=os.listdir(test_path)
for path in class_names_test :
files=os.listdir(test_path+ "/"+path )
for f in files:
print(f)
predict_(test_path+ "/"+path + "/" + f)
import numpy as np
from sklearn.metrics import confusion_matrix,classification_report
batch_size = 32
num_of_test_samples = 600
predictions = model.predict_generator(test_generator, num_of_test_samples // batch_size+1)

y_pred = np.argmax(predictions, axis=1)

true_classes = test_generator.classes
class_labels = list(test_generator.class_indices.keys())
print(class_labels)
print(confusion_matrix(test_generator.classes, y_pred))
report = classification_report(true_classes, y_pred, target_names=class_labels)
print(report)

OUTPUT:
Epoch 1/10
50/50 [==============================] - 1000s 20s/step - loss: 6.0878 - accuracy: 0.7510 -
val_loss: 1.2246 - val_accuracy: 0.9150
Epoch 2/10
50/50 [==============================] - 975s 20s/step - loss: 1.2780 - accuracy: 0.8771 -
val_loss: 1.0288 - val_accuracy: 0.9183
Epoch 3/10
50/50 [==============================] - 970s 19s/step - loss: 0.6588 - accuracy: 0.9134 -
val_loss: 1.0429 - val_accuracy: 0.9150
Epoch 4/10
50/50 [==============================] - 970s 19s/step - loss: 0.4398 - accuracy: 0.9255 -
val_loss: 1.0595 - val_accuracy: 0.9100
Epoch 5/10
50/50 [==============================] - 970s 20s/step - loss: 0.4480 - accuracy: 0.9344 -
val_loss: 1.0376 - val_accuracy: 0.9017
Epoch 6/10
50/50 [==============================] - 970s 19s/step - loss: 0.5306 - accuracy: 0.9229 -
val_loss: 1.0845 - val_accuracy: 0.9133
Epoch 7/10
50/50 [==============================] - 968s 19s/step - loss: 0.4033 - accuracy: 0.9446 -
val_loss: 0.7878 - val_accuracy: 0.9200
Epoch 8/10
50/50 [==============================] - 968s 19s/step - loss: 0.3513 - accuracy: 0.9414 -
val_loss: 1.0129 - val_accuracy: 0.9233
Epoch 9/10
50/50 [==============================] - 968s 19s/step - loss: 0.3326 - accuracy: 0.9510 -
val_loss: 0.7167 - val_accuracy: 0.9250
Epoch 10/10
50/50 [==============================] - 968s 19s/step - loss: 0.2232 - accuracy: 0.9541 -
val_loss: 0.7805 - val_accuracy: 0.9283
Result:
Thus Creating vgg-16 pretrained model from transfer learning is successfully completed.

TASK 5

GENERATIVE ADVERSIAL NETWORK

Aim:

To implement a simple generative adversial network (GAM)

Procedure:

Step 1: Import all pandas

Step 2: Load & preprocess the mnist dataset

Step 3: Build generator model & discriminator model

Step 4: Build GAN & compile models

Step 5: Train the loop & save the generated images for classification

Step 6: Visualize it
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import layers
# Load and preprocess the MNIST dataset
(train_images, _), (_, _) = tf.keras.datasets.mnist.load_data()
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')
train_images = (train_images - 127.5) / 127.5
# Generator model def
build_generator():
model = tf.keras.Sequential()
model.add(layers.Dense(128, input_shape=(100,), use_bias=False))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU(alpha=0.01))
model.add(layers.Dense(784, activation='tanh'))
model.add(layers.Reshape((28, 28, 1)))
return model
# Discriminator model def
build_discriminator():
model = tf.keras.Sequential()
model.add(layers.Flatten(input_shape=(28, 28, 1)))
model.add(layers.Dense(128))
model.add(layers.LeakyReLU(alpha=0.01))
model.add(layers.Dense(1, activation='sigmoid'))
return model
# Build GAN
def build_gan(generator, discriminator):
discriminator.trainable = False
model = tf.keras.Sequential([generator, discriminator])return
model

generator = build_generator()
discriminator = build_discriminator()
gan = build_gan(generator, discriminator)#
Compile models
discriminator.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(0.0002, 0.5))
gan.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(0.0002, 0.5)) #
Training loop
epochs = 10
batch_size = 64
for epoch in range(epochs):
idx = np.random.randint(0, train_images.shape[0], batch_size)real_images
= train_images[idx]
noise = np.random.normal(0, 1, (batch_size, 100))
generated_images = generator.predict(noise)
real_labels = np.ones((batch_size, 1))
fake_labels = np.zeros((batch_size, 1))
d_loss_real = discriminator.train_on_batch(real_images, real_labels) d_loss_fake
= discriminator.train_on_batch(generated_images, fake_labels) d_loss = 0.5 *
np.add(d_loss_real, d_loss_fake)
noise = np.random.normal(0, 1, (batch_size, 100))
fake_labels = np.ones((batch_size, 1))
g_loss = gan.train_on_batch(noise, fake_labels) if
epoch % 100 == 0:
print("Epoch {}, D Loss: {}, G Loss: {}".format(epoch, d_loss, g_loss))#
Save generated images for visualization
if epoch % 1000 == 0:
generated_images = generator.predict(np.random.normal(0, 1, (16, 100)))
generated_images = 0.5 * generated_images + 0.5 # Rescale images plt.figure(figsize=(4,
4))
for i in range(generated_images.shape[0]): plt.subplot(4,
4, i + 1) plt.imshow(generated_images[i, :, :, 0],
cmap='gray')

plt.axis('off')
plt.show()

OUTPUT:
2/2 [==============================] - 0s 6ms/step

Epoch 0, D Loss: 0.6883715391159058, G Loss: 0.7373790740966797


1/1 [==============================] - 0s 120ms/step

2/2 [==============================] - 0s 5ms/step

2/2 [==============================] - 0s 5ms/step

2/2 [==============================] - 0s 7ms/step

2/2 [==============================] - 0s 5ms/step

2/2 [==============================] - 0s 5ms/step


2/2 [==============================] - 0s 8ms/step

2/2 [==============================] - 0s 6ms/step

2/2 [==============================] - 0s 5ms/step

2/2 [==============================] - 0s 5ms/step

Result
Thus the Implementing a simple generative adversial network is successfully completed.

Task 6

RECURRENT NEURAL NETWORKS

AIM:

To create and perform a recurrent neural networks

PROCEDURE:

Step1: Import all necessary libraries

Step2: Import keras model for processing

Step3: Define Corpus and Tokenize

Step4: Convert text to Sequences with same length

Step5: Prepare input and target data and build RNN model

Step6: print model Summary


Program:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
# Sample corpus

corpus = [
'I enjoy playing football',
'Football is my favorite sport',
'I watch football matches regularly'
]# Tokenize the corpus
tokenizer = Tokenizer()
tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1
# Convert text to sequences
sequences = tokenizer.texts_to_sequences(corpus)
# Pad sequences to have the same length
max_sequence_length = max([len(seq) for seq in sequences])
padded_sequences = pad_sequences(sequences, maxlen=max_sequence_length, padding='post')
# Prepare input and target data
input_data = padded_sequences[:, :-1]
target_data = padded_sequences[:, -1]
# Build the RNN model
embedding_dim = 10
model = Sequential()
model.add(Embedding(total_words, embedding_dim, input_length=max_sequence_length-1))
model.add(LSTM(50))
model.add(Dense(total_words, activation='softmax'))
model.summary()
# Compile the model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
model.fit(input_data, target_data, epochs=100, verbose=1)
# Get the word embeddings
word_embeddings = model.layers[0].get_weights()[0]
# Print word embeddings
for word, index in
tokenizer.word_index.items():
embedding_vector = word_embeddings[index]
print(f"{word}: {embedding_vector}")
OUTPUT:
Epoch 1/100
1/1 [==============================] - 2s 2s/step - loss: 2.4875 - accuracy: 0.0000e+00
Epoch 2/100
1/1 [==============================] - 0s 17ms/step - loss: 2.4826 - accuracy: 0.0000e+00
Epoch 3/100
1/1 [==============================] - 0s 13ms/step - loss: 2.4776 - accuracy: 0.0000e+00
Epoch 4/100
1/1 [==============================] - 0s 13ms/step - loss: 2.4727 - accuracy: 0.3333
Epoch 95/100
1/1 [==============================] - 0s 16ms/step - loss: 0.7661 - accuracy: 1.0000
Epoch 96/100
1/1 [==============================] - 0s 16ms/step - loss: 0.7504 - accuracy: 1.0000
Epoch 97/100
1/1 [==============================] - 0s 16ms/step - loss: 0.7344 - accuracy: 1.0000
Epoch 98/100
1/1 [==============================] - 0s 20ms/step - loss: 0.7179 - accuracy: 1.0000
Epoch 99/100
1/1 [==============================] - 0s 16ms/step - loss: 0.7010 - accuracy: 1.0000
Epoch 100/100
1/1 [==============================] - 0s 15ms/step - loss: 0.6838 - accuracy: 1.0000

Result:
Thus, the Creating & performing recurrent neural network is sucessfully completed.
TASK 7

AUTO ENCODES

AIM:

To create a auto encodes by using keras model.

PROCEDURE:

Step1: Import all libraries from keras

Step 2: Initialize encoding dimension

Step3: Give input image & encode

Step4: Load the dataset from google drive

Step5: Train the dataset

Step6: Summarize the model

Program:
# -*- coding: utf-8 -*-
"""Autoencoder
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1J678VYoIlJT7kfn9SbQNdBdnSTdS06_I

"""

from keras.models import Model


from keras.layers import Input, Dense
from keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt
encoding_dim = 64
input_img = Input(shape=(784,))
encoded = Dense(encoding_dim, activation='relu')(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)
autoencoder = Model(input_img, decoded)
encoder = Model(input_img, encoded)
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = Model(encoded_input,decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta',loss='binary_crossentropy')
(x_train, _),(x_test, _) = mnist.load_data()

x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255
x_train = x_train.reshape((len(x_train), (np.prod(x_train.shape[1:]))))
x_test = x_test.reshape((len(x_test), (np.prod(x_test.shape[1:]))))
print(f'x_train shape = {x_train.shape}')
print(f'x_test shape = {x_test.shape}')
autoencoder.fit(x_train, x_train, epochs=5, batch_size=32, shuffle=True, validation_data=(x_test,
x_test))
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)

n = 10
plt.figure(figsize=(20,4))
for i in range(n):
ax = plt.subplot(2, n, i+1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax = plt.subplot(2, n, i+1+n)
plt.imshow(decoded_imgs[i].reshape(28,28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
from google.colab import drive
drive.mount('/content/drive')

OUTPUT:

Result:
Thus Creating a autoencodes by using keras modelis sucessfully completed.

Task 8

LONG SHORT-TERM MEMORY

AIM:

To create a long short term memory(LSTM) classification model.

PROCEDURE:

Step1: Import all libraries from keras

Step2: Import the dataframe with a path

Step3: locate the dataset in drive

Step4: Train the dataset


Step5: Summarize the model.

Program:
# -*- coding: utf-8 -*-
"""LSTM.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19SKdhO8GsH-bNjjdExu1AFMfA5MjNZ93
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from google.colab import drive
drive.mount('/content/drive')
df =
pd.read_csv('/content/drive/MyDrive/monthlyMilkProduction.csv',index_col='Date',parse_dates=True
)

df.index.freq='MS'
df.head()
df.plot(figsize=(10,6))
from statsmodels.tsa.seasonal import seasonal_decompose
results = seasonal_decompose(df['Production'])
results.plot()
len(df)
train = df.iloc[:156]
test = df.iloc[156:]
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
df.head(),df.tail()
scaler.fit(train)
scaled_train = scaler.transform(train)
scaled_test = scaler.transform(test)
scaled_train[:10]

from keras.preprocessing.sequence import TimeseriesGenerator


n_input = 3
n_features = 1
generator = TimeseriesGenerator(scaled_train, scaled_train, length=n_input, batch_size=1)
print(len(scaled_train),len(generator))
from numpy.random.mtrand import f
x,y = generator[0]
print(f'Given the Array: \n{x.flatten}')
print(f'Predict this y: \n {y}')
x.shape
n_input = 12
generator = TimeseriesGenerator(scaled_train, scaled_train, length=n_input, batch_size=1)
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
model =Sequential()
model.add(LSTM(100, activation='relu', input_shape=(n_input, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
model.summary()
model.fit(generator, epochs=50)
loss_per_epoch = model.history.history['loss']
plt.plot(range(len(loss_per_epoch)),loss_per_epoch)
last_train_batch = scaled_train[-12:]
last_train_batch = last_train_batch.reshape(1, n_input, n_features)
model.predict(last_train_batch)
scaled_test[0]
test_predictions = []
first_eval_batch = scaled_train[-n_input:]
current_batch = first_eval_batch.reshape((1, n_input, n_features))
for i in range(len(test)):
current_pred = model.predict(current_batch)[0]
test_predictions.append(current_pred)
current_batch = np.append(current_batch[:,1:,:],[[current_pred]],axis=1)
test_predictions
test.head()
true_predictions = scaler.inverse_transform(test_predictions)
test['Predictions'] = true_predictions
test.plot(figsize=(10,6))
from sklearn.metrics import mean_squared_error
from math import sqrt
rmse = sqrt(mean_squared_error(test['Production'],test['Predictions']))
print(rmse)

OUTPUT:
Epoch 1/50

144/144 [==============================] - 3s 8ms/step - loss: 0.0439

Epoch 2/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0251

Epoch 3/50

144/144 [==============================] - 1s 7ms/step - loss: 0.0194

Epoch 4/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0113

Epoch 5/50

144/144 [==============================] - 2s 11ms/step - loss: 0.0073

Epoch 6/50

144/144 [==============================] - 2s 13ms/step - loss: 0.0047

Epoch 7/50

144/144 [==============================] - 1s 9ms/step - loss: 0.0038

Epoch 8/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0045

Epoch 9/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0073

Epoch 10/50

144/144 [==============================] - 1s 9ms/step - loss: 0.0042

Epoch 11/50
144/144 [==============================] - 1s 9ms/step - loss: 0.0042

Epoch 12/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0040

Epoch 13/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0037

Epoch 14/50

144/144 [==============================] - 2s 11ms/step - loss: 0.0042

Epoch 15/50

144/144 [==============================] - 2s 13ms/step - loss: 0.0042

Epoch 16/50

144/144 [==============================] - 2s 14ms/step - loss: 0.0037

Epoch 17/50

144/144 [==============================] - 2s 12ms/step - loss: 0.0038

Epoch 18/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0033

Epoch 19/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0040

Epoch 20/50

144/144 [==============================] - 1s 9ms/step - loss: 0.0045

Epoch 21/50

144/144 [==============================] - 1s 9ms/step - loss: 0.0033

Epoch 22/50

144/144 [==============================] - 1s 9ms/step - loss: 0.0034

Epoch 23/50

144/144 [==============================] - 2s 13ms/step - loss: 0.0030

Epoch 24/50

144/144 [==============================] - 2s 12ms/step - loss: 0.0032

Epoch 25/50

144/144 [==============================] - 1s 9ms/step - loss: 0.0029

Epoch 26/50
144/144 [==============================] - 1s 7ms/step - loss: 0.0052

Epoch 27/50

144/144 [==============================] - 1s 7ms/step - loss: 0.0034

Epoch 28/50

144/144 [==============================] - 1s 9ms/step - loss: 0.0025

Epoch 29/50

144/144 [==============================] - 1s 7ms/step - loss: 0.0034

Epoch 30/50

144/144 [==============================] - 1s 7ms/step - loss: 0.0034

Epoch 31/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0025

Epoch 32/50

144/144 [==============================] - 2s 13ms/step - loss: 0.0027

Epoch 33/50

144/144 [==============================] - 2s 11ms/step - loss: 0.0028

Epoch 34/50

144/144 [==============================] - 1s 9ms/step - loss: 0.0025

Epoch 35/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0031

Epoch 36/50

144/144 [==============================] - 1s 10ms/step - loss: 0.0025

Epoch 37/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0023

Epoch 38/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0028

Epoch 39/50

144/144 [==============================] - 2s 11ms/step - loss: 0.0031

Epoch 40/50

144/144 [==============================] - 2s 13ms/step - loss: 0.0029

Epoch 41/50
144/144 [==============================] - 1s 9ms/step - loss: 0.0027

Epoch 42/50

144/144 [==============================] - 1s 9ms/step - loss: 0.0029

Epoch 43/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0029

Epoch 44/50

144/144 [==============================] - 1s 7ms/step - loss: 0.0028

Epoch 45/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0026

Epoch 46/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0024

Epoch 47/50

144/144 [==============================] - 2s 11ms/step - loss: 0.0023

Epoch 48/50

144/144 [==============================] - 2s 13ms/step - loss: 0.0019

Epoch 49/50

144/144 [==============================] - 1s 9ms/step - loss: 0.0022

Epoch 50/50

144/144 [==============================] - 1s 8ms/step - loss: 0.0021

1/1 [==============================] - 0s 241ms/step


array([0.67548077])
Result:
Thus the Creating a long short term classification model is successfully completed.

Task 9

CLASSIFICATION WITH LOCALIZATION

AIM:

To create & perform classification with localization

PROCEDURE:

Step1: Downloading the detection dataset

Step2: perform data aquisition with image library

Step3: Preprocess the dataset

Step4: Visualize the data with it annotations

Step5: Split the data into train the model

Step6: Compile & train the model

Step7: Plot model loss & accuracy curves


Program:
!pip install ultralytics
import numpy as np
import os

import cv2
import time
from ultralytics import YOLO
# define some parameters
CONFIDENCE = 0.5
font_scale = 1
thickness = 1
# loading the YOLOv8 model with the default weight file
model = YOLO("yolov8n.pt")
# loading all the class labels (objects)
labels = open("/content/coco.names").read().strip().split("\n")
# generating colors for each object for later plotting
colors = np.random.randint(0, 255, size=(len(labels), 3), dtype="uint8")
path_name = "/content/dog.jpg"
image = cv2.imread(path_name)
file_name = os.path.basename(path_name) # "dog.jpg"
filename, ext = file_name.split(".") # "dog", "jpg"
# measure how much it took in seconds
start = time.perf_counter()
# run inference on the image
# see: https://docs.ultralytics.com/modes/predict/#arguments for full list of arguments
results = model.predict(image, conf=CONFIDENCE)[0]
time_took = time.perf_counter() - start
print(f"Time took: {time_took:.2f}s")
print(results.boxes.data)
# loop over the detections
for data in results.boxes.data.tolist():
# get the bounding box coordinates, confidence, and class id
xmin, ymin, xmax, ymax, confidence, class_id = data
# converting the coordinates and the class id to integers
xmin = int(xmin)
ymin = int(ymin)
xmax = int(xmax)
ymax = int(ymax)
class_id = int(class_id)
# draw a bounding box rectangle and label on the image
for class_id in class_ids:
color = [int(c) for c in colors[class_id]]
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color=color, thickness=thickness)
text = f"{labels[class_id]}: {confidence:.2f}"
# calculate text width & height to draw the transparent boxes as background of the text
(text_width, text_height) = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX,
fontScale=font_scale, thickness=thickness)[0]
text_offset_x = xmin
text_offset_y = ymin - 5
box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y -
text_height))
overlay = image.copy()
cv2.rectangle(overlay, box_coords[0], box_coords[1], color=color, thickness=cv2.FILLED)
# add opacity (transparency to the box)
image = cv2.addWeighted(overlay, 0.6, image, 0.4, 0)
# now put the text (label: confidence %)
cv2.putText(image, text, (xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX,
fontScale=font_scale, color=(0, 0, 0), thickness=thickness)
import cv2
from google.colab.patches import cv2_imshow
# display output image
cv2_imshow(image)
cv2.waitKey(0)
# save output image to disk
cv2.imwrite(filename + "_yolo8." + ext, image)
OUTPUT:

Result:
Thus, the Creating & performing classification with localization is successfully completed.

TASK 10

OPTIMIZERS

AIM:

To create & perform optimization by using tensorflow

PROCEDURE:

Step 1: Import all libraries

Step2: Define points on x-axis & y-axis

Step3: Create a mesh of point of whih to evaluate the function

Step4: Generate 20 levels on a log scale

Step5: Add a marker to show the minimum

Step6: Change fontsize & plot f(r)

Step7: Plot all lines & vectors

Step 8: Reset to initial fontsize


Step9: Execute

Step 10: Summarize the model


# -*- coding: utf-8 -*-

"""Untitled10.ipynb

Automatically generated by Colaboratory.

Original file is located at

https://colab.research.google.com/drive/1w7xl7q4u8bQ9dOo7vh98b3iB9POgt7Gn

"""

# Commented out IPython magic to ensure Python compatibility.

# Commented out IPython magic to ensure Python compatibility.

!pip install tensorflow-gpu==2.0.0-beta0 > /dev/null 2>&1

import tensorflow as tf

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

from IPython import display

# %matplotlib inline

display.clear_output()

print("TensorFlow executing eagerly: {}".format(tf.executing_eagerly()))

import tensorflow as tf

import numpy as np

import matplotlib.pyplot as plt

def rosenbrock_banana(x, y, a=1., b=20.):

return tf.math.pow(a - x, 2.) + b * tf.math.pow(y - tf.math.pow(x, 2.), 2.)


def gen_2d_loss_surface(loss_func, n_x=100, n_y=100, min_x=-2., max_x=2., min_y=-0.2,
max_y=1.3):

x_vals = np.linspace(min_x, max_x, n_x)

y_vals = np.linspace(min_y, max_y, n_y)

X, Y = np.meshgrid(x_vals, y_vals)

Z = loss_func(X, Y)

return X, Y, Z

# Usage example:
X, Y, Z = gen_2d_loss_surface(rosenbrock_banana)

plt.contourf(X, Y, Z, levels=100, cmap='jet')

plt.colorbar()

plt.xlabel('x')

plt.ylabel('y')

plt.title('Rosenbrock Banana Loss Surface')

plt.show()

# create a mesh of points at which to evaluate our function

X, Y = np.meshgrid(np.linspace(min_x, max_x, n_x),

np.linspace(min_y, max_y, n_y))

# evaluate the func at all of the points

Z = loss_func(X, Y).numpy()

return X, Y, Z

def make_contour_plot(X, Y, Z, levels=None):

if levels == None:

# generate 20 levels on a log scale

levels = np.insert(np.logspace(0, 2.6, 20, True, base=10), 0, 0)

fig = plt.figure(figsize=(9.84, 3))

ax = fig.gca()

ax.contour(X, Y, Z, levels, alpha=0.5)

ax.contourf(X, Y, Z, levels, alpha=0.2)

ax.set_xlabel('x')

ax.set_ylabel('y')
return fig, ax

def make_surface_plot(X, Y, Z, elevation=0, azimuth_angle=0, levels=None):

if levels == None:

# generate 20 levels on a log scale

levels = np.insert(np.logspace(0, 2.6, 20, True, base=10), 0, 0)

fig = plt.figure(figsize=(10,6))

ax = fig.gca(projection='3d')

ax.view_init(elevation, azimuth_angle)
ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.2)

ax.contour(X, Y, Z, levels, cmap='viridis', alpha=0.5)

ax.set_xlabel('x')

ax.set_ylabel('y')

return fig, ax

X, Y, Z = gen_2d_loss_surface(rosenbrock_banana)

fig, ax = make_contour_plot(X, Y, Z)

# add a marker to show the minimum

ax.plot(1, 1, 'r*', ms=30, label='minimum')

ax.legend()

fig.show()

def f(x):

return -np.cos(x)
def tangent_f(x):

return np.sin(x)

def df(x, x_0):

return tangent_f(x_0) * (x - x_0) + f(x_0)

def perpindicular_unit_f(x_0):

slope_f = tangent_f(x_0)

y_0 = f(x_0)

x_1 = slope_f / np.sqrt(2) + x_0

y_1 = -x_1 / slope_f + y_0 + x_0 / slope_f

return [[x_0, x_1], [y_0, y_1]]

def interactive_gradient_visual(x_0):

# change the fontsize for better visibility

init_size = plt.rcParams["font.size"] # store initial font size

plt.rcParams.update({'font.size': 22}) # update the size

plt.figure(figsize=(12, 8))

x = np.linspace(-np.pi, 2 * np.pi)

f_x = f(x)

# plot f(x)
plt.plot(x, f_x, label=r"$f(x)$", color="green")

# add a point showing where x_0 falls on f(x)

plt.plot(x_0, f(x_0), marker="o", color="black")

# plot the tangent line to f(x) at x_0

plt.plot(x, df(x, x_0), linestyle="--", color="cornflowerblue", label=r"$df(x)$")

# plot the normal vector to the tangent

perp_unit_vector = perpindicular_unit_f(x_0)

plt.plot(perp_unit_vector[0], perp_unit_vector[1], color="dimgray")

# drop a vertical line from x_0

plt.plot([x_0, x_0], [f(x_0), -3.1], color="silver")

# Usage example:

min_x, max_x = -3.1, 6.2

min_y, max_y = -3.1, 3.1


X, Y, Z = gen_2d_loss_surface(rosenbrock_banana, n_x=100, n_y=100, min_x=min_x, max_x=max_x,
min_y=min_y, max_y=max_y)

plt.contourf(X, Y, Z, levels=100, cmap='jet')

# Define the perp_unit_vector (this is just an example; you need to calculate it based on your specific
scenario)

perp_unit_vector = [[1.5, 2.5], [1.5, 1.5]]

[[x_0, x_1], [y_0, y_1]] = perp_unit_vector

dx = x_1 - x_0

dy = 0 # y_1 - y_1

arrow = plt.arrow(

x_0, y_1, dx, dy,


color="red", label=r"$f'(x_0)$",

lw=3, head_width=np.abs(x_1 - x_0)/10, length_includes_head=True

plt.plot([x_0, x_1], [y_1, y_1], color="red", label=r"$f'(x_0)$")

plt.legend(loc="upper left")

plt.xlim(min_x, max_x)

plt.ylim(min_y, max_y)

plt.xlabel(r"$x_0$")
plt.show()

# Add the following line to store and reset the initial font size

init_size = plt.rcParams['font.size']

plt.show()

# Reset to the initial font size (you may skip this line if you don't want to reset it)

plt.rcParams.update({'font.size': init_size})

interactive_gradient_visual(x_0)

def f(x):

return -np.cos(x)

def tangent_f(x):

return np.sin(x)

def df(x, x_0):

return tangent_f(x_0) * (x - x_0) + f(x_0)

def perpindicular_unit_f(x_0):

slope_f = tangent_f(x_0)

y_0 = f(x_0)

x_1 = slope_f / np.sqrt(2) + x_0

y_1 = -x_1 / slope_f + y_0 + x_0 / slope_f

return [[x_0, x_1], [y_0, y_1]]

def noisy_df(x, x_0, noisy_x_0):

return tangent_f(noisy_x_0) * (x - x_0) + f(x_0)

def noisy_perpindicular_unit_f(x_0, noisy_x_0):

slope_f = tangent_f(noisy_x_0)

y_0 = f(x_0)

x_1 = slope_f / np.sqrt(2) + x_0

y_1 = -x_1 / slope_f + y_0 + x_0 / slope_f

return [[x_0, x_1], [y_0, y_1]]

def interactive_noisy_gradient_visual(x_0, noisy_x_0):

# change the fontsize for better visibility

init_size = plt.rcParams["font.size"] # store initial font size

plt.rcParams.update({'font.size': 22}) # update the size


plt.figure(figsize=(12, 8))

x = np.linspace(-np.pi, 2 * np.pi)

f_x = f(x)

y_0 = f(x_0)

# plot f(x)

plt.plot(x, f_x, label=r"$f(x)$", color="green")

# add a point showing where x_0 falls on f(x)

plt.plot(x_0, f(x_0), marker="o", color="black")

# plot the tangent line to f(x) at x_0

plt.plot(x, df(x, x_0), linestyle="--", color="cornflowerblue", label=r"$df(x)$")

# drop a vertical line from x_0

plt.plot([x_0, x_0], [f(x_0), -3.1], color="silver")

# plot the noisy tangent line to f(x) at x_0

plt.plot(x, noisy_df(x, x_0, noisy_x_0), linestyle="--", color="red", label=r"$\widetilde{df}(x)$")

# plot the normal vector to the tangent

[[x_0, x_1], [y_0, y_1]] = perpindicular_unit_f(x_0)

plt.plot([x_0, x_1], [y_0, y_1], color="dimgray")

# Plot the noisy positive direction of the change vector

dx = x_1 - x_0

dy = 0

arrow = plt.arrow(

x_0, y_1, dx, dy,

color="red", label=r"$f'(x_0)$",

lw=3, head_width=np.abs(x_1 - x_0)/10, length_includes_head=True

plt.plot([x_0, x_1], [y_1, y_1], color="red", label=r"$\widetilde{f'}(x_0)$")

plt.legend(loc="upper left")

plt.xlim(min_x, max_x)

plt.ylim(min_y, max_y)

plt.xlabel(r"$x_0$")

plt.show()
# reset to initial font size

plt.rcParams.update({'font.size': init_size})

def interactive_batch_size_visual(x_0, batch_size):

np.random.seed(0)

noisy_x_0 = x_0 + np.mean(np.random.normal(loc=0, scale=0.5, size=batch_size))

interactive_noisy_gradient_visual(x_0, noisy_x_0)

x_0 = 2.7

batch_size = 20

interactive_batch_size_visual(x_0, batch_size)

def SGD_update(params, grads, states, hyper_params):

# hyper-param typical values: learning_rate=0.01

# SGD doesn't have any state, however, the algorithms

# we will look at later do!

for param, grad in zip(params, grads):

param.assign_sub(hyper_params['lr'] * grad)

def optimize_banana(update_func, params, states, hyper_params):

# plot the loss surface, minimum value and starting point

X, Y, Z = gen_2d_loss_surface(rosenbrock_banana)

fig, ax = make_contour_plot(X, Y, Z)

ax.plot(1, 1, 'r*', ms=30, label='minimum')

ax.plot(start_x, start_y, 'b*', ms=20, label='start')

for epoch in range(epochs):

with tf.GradientTape() as tape:

# we are trying to minimize the output of the func

loss = rosenbrock_banana(x, y)

# calculate the gradients of the loss with respect to the params

grads = tape.gradient(loss, params)

# save the old x and y values for the plot

old_x = params[0].numpy()

old_y = params[1].numpy()
# update the parameters using SGD
update_func(params, grads, states, hyper_params)

# plot the change in x and y for each update step

ax.annotate('', xy=(x.numpy(), y.numpy()),

xytext=(old_x, old_y),

arrowprops={'arrowstyle': '->', 'color': 'k', 'lw': 1},

va='center', ha='center')

ax.plot(x.numpy(), y.numpy(), 'g*', ms=20, label='end')

ax.legend()

fig.show()

start_x = -1

start_y = 0.73334

learning_rate = 0.015

epochs = 150

x = tf.Variable(start_x, dtype='float32')

y = tf.Variable(start_y, dtype='float32')

params = [x, y]

states = []

hyper_params = {"lr": learning_rate}

optimize_banana(SGD_update, params, states, hyper_params)

OUTPUT:
Result:
Thus the Creating & performing ptimization by using tensorflow successfully completed.
Task 10
Implement simple facts using python

Aim: To implement simple facts and verify using python

Algorithm:
Step:1Define a list of facts containing the statements to be verified.
Step:2 Create a function named verify_fact that takes a fact as input and returns a boolean value
indicating whether the fact is true or false.
Step:3 In the verify_fact function:
a. Remove the trailing period from the fact using the rstrip function.
b. Check the fact against the known conditions to determine its truth value. You can use
conditional statements (if, elif, else) for this.
 If the fact matches a known condition, return True to indicate that the fact is true.
 If the fact does not match any known condition, return False to indicate that the fact is
false.
Step:4 Iterate over each fact in the list of facts:
a. Call the verify_fact function for each fact.
b. Print the fact and the corresponding "Yes" or "No" based on its truth value.

Program:
# Define a list of facts
facts = [
"john_is_cold.", # john is cold
"raining.", # it is raining
"john_Forgot_His_Raincoat.", # john forgot his raincoat
"fred_lost_his_car_keys.", # fred lost his car keys
"peter_footballer." # peter plays football
]

# Function to check if a fact is true


def verify_fact(fact):
# Remove the trailing period
fact = fact.rstrip(".")
# Perform some logic to verify the fact
if fact == "john_Forgot_His_Raincoat":
return True
elif fact == "raining":
return True
elif fact == "foggy":
return True
elif fact == "Cloudy":
return False # Assume it's not cloudy
else:
return False

# Verify each fact


for fact in facts:
if verify_fact(fact):
print(f"{fact} - Yes")
else:
print(f"{fact} - No")

Output:
john_is_cold. - No
raining. - Yes
john_Forgot_His_Raincoat. - Yes
fred_lost_his_car_keys. - No
peter_footballer. – No

Result:
Thus the implementation of simple facts using python was successfully executed and output
was verified.

You might also like