NAMAN GUPTA 00524302022
Q1. Write a program for creating a perceptron.
Code
!pip install tensorflow
import tensorflow as tf
import numpy as np
X_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
y_train = np.array([[0], [0], [0], [1]], dtype=np.float32)
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, activation='sigmoid', input_shape=(2,))
])
model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=100, verbose=1)
print("\nTesting the model:")
predictions = model.predict(X_train)
for i, x in enumerate(X_train):
print(f"Input: {x}, Predicted Output: {round(predictions[i][0])}")
Output
1
NAMAN GUPTA 00524302022
Q2. Write a program to implement multi-layer perceptron using TensorFlow. Apply multi-layer
perceptron (MLP) on the Iris dataset.
Code
!pip install tensorflow
import tensorflow as tf
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
import numpy as np
X, y = load_iris(return_X_y=True)
y = OneHotEncoder(sparse_output=False).fit_transform(y.reshape(-1, 1))
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu', input_shape=(4,)),
tf.keras.layers.Dense(8, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=50, batch_size=5, verbose=1)
test_loss, test_acc = model.evaluate(X_test, y_test)
print(f"\nTest Accuracy: {test_acc:.4f}")
predictions = model.predict(X_test)
print("\nSample Predictions:")
for i in range(5):
print(f"Actual: {np.argmax(y_test[i])}, Predicted: {np.argmax(predictions[i])}")
2
NAMAN GUPTA 00524302022
Output
3
NAMAN GUPTA 00524302022
Q3. (a) Write a program to implement a Convolution Neural Network (CNN) in Keras. Perform
predictions using the trained Convolution Neural Network (CNN).
Code
import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=64, validation_split=0.1)
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"\nTest Accuracy: {test_acc:.4f}")
predictions = model.predict(x_test[:5])
print("\nSample Predictions:")
for i in range(5):
print(f"Predicted class: {np.argmax(predictions[i])}, True class: {np.argmax(y_test[i])}")
4
NAMAN GUPTA 00524302022
Output
5
NAMAN GUPTA 00524302022
Q3. (b) Write a program to build an Image Classifier with CIFAR-10 Data.
Code
import tensorflow as tf
from tensorflow.keras import layers, models
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test), batch_size=64)
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f"Test accuracy: {test_acc:.2f}")
Output
6
NAMAN GUPTA 00524302022
Q4. (a) Write a program to perform face detection using CNN.
Code
!pip install opencv-python
import cv2
from google.colab.patches import cv2_imshow
from google.colab import files
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
uploaded = files.upload()
video_path = list(uploaded.keys())[0]
video_cap = cv2.VideoCapture(video_path)
while video_cap.isOpened():
ret, frame = video_cap.read()
if not ret:
print("Video ended or cannot read frame.")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2_imshow(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_cap.release()
cv2.destroyAllWindows()
Output
7
NAMAN GUPTA 00524302022
Q4. (b) Write a program to demonstrate hyperparameter tuning in CNN.
Code
!pip install keras-tuner
!pip install tensorflow
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import keras_tuner as kt
import numpy as np
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
def build_model(hp):
model = keras.Sequential()
model.add(layers.Conv2D(
filters=hp.Int('conv1_filters', min_value=32, max_value=128, step=32),
kernel_size=hp.Choice('conv1_kernel', values=[3, 5]),
activation='relu', input_shape=(32, 32, 3)
))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Conv2D(
filters=hp.Int('conv2_filters', min_value=64, max_value=256, step=64),
kernel_size=hp.Choice('conv2_kernel', values=[3, 5]),
activation='relu'
))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(
units=hp.Int('dense_units', min_value=64, max_value=256, step=64),
activation='relu'
))
model.add(layers.Dense(10, activation='softmax'))
8
NAMAN GUPTA 00524302022
model.compile(
optimizer=keras.optimizers.Adam(
learning_rate=hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])
),
loss='sparse_categorical_crossentropy', # No need to one-hot encode in this case
metrics=['accuracy']
return model
tuner = kt.RandomSearch(
build_model,
objective='val_accuracy',
max_trials=5, # Number of different hyperparameter combinations to try
executions_per_trial=1, # Number of times to train each model
directory='my_tuner_dir',
project_name='cnn_tuning'
tuner.search(x_train, y_train, epochs=10, validation_split=0.2)
best_hps = tuner.get_best_hyperparameters(num_trials=1)[0]
print(f"""
Best hyperparameters found:
Conv1 Filters: {best_hps.get('conv1_filters')}
Conv1 Kernel Size: {best_hps.get('conv1_kernel')}
Conv2 Filters: {best_hps.get('conv2_filters')}
Conv2 Kernel Size: {best_hps.get('conv2_kernel')}
Dense Units: {best_hps.get('dense_units')}
Learning Rate: {best_hps.get('learning_rate')}
""")
best_model = tuner.hypermodel.build(best_hps)
best_model.fit(x_train, y_train, epochs=10, validation_split=0.2)
test_loss, test_acc = best_model.evaluate(x_test, y_test)
print(f"Test accuracy: {test_acc:.4f}")
9
NAMAN GUPTA 00524302022
Output
10
NAMAN GUPTA 00524302022
Q4. (c)Predicting Bike-Sharing Patterns Build and train neural networks from scratch to predict the
number of bike share users on a given day.
Code
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import tensorflow as tf
from tensorflow.keras import layers, models
import zipfile
import urllib.request
import os
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/00275/Bike-Sharing-Dataset.zip"
zip_path = "Bike-Sharing-Dataset.zip"
if not os.path.exists(zip_path):
urllib.request.urlretrieve(url, zip_path)
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall("bike_data")
df = pd.read_csv("bike_data/day.csv")
X = df[['season', 'yr', 'mnth', 'holiday', 'weekday', 'workingday', 'weathersit', 'temp', 'atemp', 'hum',
'windspeed']]
y = df['cnt']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
model = models.Sequential([
layers.Dense(64, activation='relu', input_shape=(X_train.shape[1],)),
layers.Dense(32, activation='relu'),
layers.Dense(1) # Output layer for regression
])
11
NAMAN GUPTA 00524302022
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
history = model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test), batch_size=32)
test_loss, test_mae = model.evaluate(X_test, y_test)
print(f"Test MAE: {test_mae}")
y_pred = model.predict(X_test)
plt.scatter(y_test, y_pred)
plt.xlabel('Actual Rentals')
plt.ylabel('Predicted Rentals')
plt.title('Actual vs Predicted Rentals')
plt.show()
Output
12
NAMAN GUPTA 00524302022
Q5. Write a program to build auto-encoder in Keras.
Code
!pip install tensorflow matplotlib
import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
(x_train, _), (x_test, _) = tf.keras.datasets.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), 28, 28, 1))
x_test = x_test.reshape((len(x_test), 28, 28, 1))
input_img = tf.keras.Input(shape=(28, 28, 1))
encoded = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
encoded = layers.MaxPooling2D((2, 2), padding='same')(encoded)
encoded = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
encoded = layers.MaxPooling2D((2, 2), padding='same')(encoded)
decoded = layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
decoded = layers.UpSampling2D((2, 2))(decoded)
decoded = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(decoded) # Fixed padding
decoded = layers.UpSampling2D((2, 2))(decoded)
decoded = layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(decoded)
autoencoder = models.Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(x_train, x_train,
epochs=10,
batch_size=128,
shuffle=True,
validation_data=(x_test, x_test))
decoded_imgs = autoencoder.predict(x_test)
n = 10
plt.figure(figsize=(20, 4))
13
NAMAN GUPTA 00524302022
for i in range(n):
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test[i].reshape(28, 28), cmap='gray')
plt.axis('off')
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(28, 28), cmap='gray')
plt.axis('off')
plt.show()
Output
14
NAMAN GUPTA 00524302022
Q6. Write a program to implement basic reinforcement learning algorithm to teach a bot to reach
its destination.
Code
import numpy as np
import random
grid_size = 5
start = (0, 0)
goal = (4, 4)
actions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
Q = np.zeros((grid_size, grid_size, len(actions)))
alpha = 0.1 # Learning rate
gamma = 0.9 # Discount factor
epsilon = 0.1 # Exploration factor
episodes = 1000 # Number of episodes
def reward(state):
if state == goal:
return 100 # Reward for reaching the goal
return -1 # Negative reward for each step (penalty)
def choose_action(state):
if random.uniform(0, 1) < epsilon:
return random.choice(range(len(actions))) # Explore
else:
return np.argmax(Q[state[0], state[1]]) # Exploit
for episode in range(episodes):
state = start
done = False
while not done:
action_idx = choose_action(state)
action = actions[action_idx]
next_state = (state[0] + action[0], state[1] + action[1])
next_state = (max(0, min(next_state[0], grid_size - 1)),
15
NAMAN GUPTA 00524302022
max(0, min(next_state[1], grid_size - 1)))
r = reward(next_state)
Q[state[0], state[1], action_idx] = Q[state[0], state[1], action_idx] + \
alpha * (r + gamma * np.max(Q[next_state[0], next_state[1]]) - Q[state[0], state[1],
action_idx])
state = next_state
if state == goal:
done = True
state = start
path = [state]
while state != goal:
action_idx = np.argmax(Q[state[0], state[1]]) # Choose the best action (exploit)
action = actions[action_idx]
state = (state[0] + action[0], state[1] + action[1])
state = (max(0, min(state[0], grid_size - 1)),
max(0, min(state[1], grid_size - 1)))
path.append(state)
print(f"Path to goal: {path}")
Output
16
NAMAN GUPTA 00524302022
Q7. (a) Write a program to implement a Recurrent Neural Network
Code
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
import matplotlib.pyplot as plt
def generate_data(seq_length=100):
x = np.linspace(0, seq_length, seq_length)
y = np.sin(x)
return y
def create_dataset(data, time_step=10):
X, y = [], []
for i in range(len(data) - time_step):
X.append(data[i:(i + time_step)])
y.append(data[i + time_step])
return np.array(X), np.array(y)
data = generate_data(100)
X, y = create_dataset(data, time_step=10)
X = X.reshape((X.shape[0], X.shape[1], 1))
train_size = int(len(X) * 0.8)
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
model = Sequential()
model.add(SimpleRNN(10, activation='relu', input_shape=(X_train.shape[1], 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X_train, y_train, epochs=20, batch_size=16, verbose=1)
predictions = model.predict(X_test)
plt.plot(y_test, label='True Data')
plt.plot(predictions, label='Predicted Data')
17
NAMAN GUPTA 00524302022
plt.legend()
plt.show()
Output
18
NAMAN GUPTA 00524302022
Q7. (b) Write a program to implement LSTM and perform time series analysis using LSTM.
Code
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
time = np.linspace(0, 100, 1000)
data = np.sin(time) + np.random.normal(0, 0.1, 1000)
def create_dataset(data, time_step=50):
X, y = [], []
for i in range(len(data) - time_step - 1):
X.append(data[i:(i + time_step)])
y.append(data[i + time_step])
return np.array(X), np.array(y)
time_step = 50
X, y = create_dataset(data)
X = X.reshape((X.shape[0], X.shape[1], 1))
train_size = int(len(X) * 0.8)
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
model = Sequential([
LSTM(50, input_shape=(X_train.shape[1], 1)),
Dense(1)
])
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X_train, y_train, epochs=10, batch_size=32)
predictions = model.predict(X_test)
plt.plot(y_test, label='True Data')
plt.plot(predictions, label='Predictions')
plt.legend()
plt.show()
19
NAMAN GUPTA 00524302022
Output
20
NAMAN GUPTA 00524302022
Q8. (a) Write a program to perform object detection using Deep Learning
Code
!pip install ultralytics
from ultralytics import YOLO
from google.colab import files
from glob import glob
uploaded = files.upload()
model = YOLO('yolov8n.pt') # You can change the model to yolov8s.pt or yolov8m.pt
if len(uploaded) == 1 and any(file.lower().endswith(('.png', '.jpg', '.jpeg')) for file in uploaded.keys()):
image_path = list(uploaded.keys())[0]
results = model(image_path)
results[0].show()
results[0].save(filename='detected.jpg')
files.download('detected.jpg')
elif len(uploaded) == 1 and any(file.lower().endswith(('.mp4', '.avi', '.mov')) for file in
uploaded.keys()):
video_path = list(uploaded.keys())[0]
model.predict(source=video_path, save=True)
output_files = glob('runs/detect/*/*.mp4')
if output_files:
output_path = output_files[0] # Get the first .mp4 file
files.download(output_path)
else:
print("No .mp4 files found in the output directory.")
else:
print("Please upload a valid image or video.")
21
NAMAN GUPTA 00524302022
Output
22
NAMAN GUPTA 00524302022
Q8. (b) Dog-Breed Classifier Design and train a convolutional neural network to analyze images of
dogs and correctly identify their breeds. Use transfer learning and well-known architectures to
improve this model.
Code
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
import cv2
import tensorflow as tf
from tensorflow import keras
from keras import layers
from functools import partial
import warnings
warnings.filterwarnings('ignore')
AUTO = tf.data.experimental.AUTOTUNE
from zipfile import ZipFile
data_path = '/content/drive/MyDrive/dog-breed-identification.zip' # Make sure to adjust the path if
necessary
with ZipFile(data_path, 'r') as zip:
zip.extractall()
print('The dataset has been extracted.')
df = pd.read_csv('labels.csv')
print(df.head())
print(df.shape)
print(f"Number of unique breeds: {df['breed'].nunique()}")
plt.figure(figsize=(10, 5))
df['breed'].value_counts().plot.bar()
plt.axis('off')
plt.show()
23
NAMAN GUPTA 00524302022
df['filepath'] = 'train/' + df['id'] + '.jpg'
print(df.head())
plt.subplots(figsize=(10, 10))
for i in range(12):
plt.subplot(4, 3, i + 1)
k = np.random.randint(0, len(df))
img = cv2.imread(df.loc[k, 'filepath'])
plt.imshow(img)
plt.title(df.loc[k, 'breed'])
plt.axis('off')
plt.show()
le = LabelEncoder()
df['breed'] = le.fit_transform(df['breed'])
print(df.head())
features = df['filepath']
target = df['breed']
X_train, X_val, Y_train, Y_val = train_test_split(features, target, test_size=0.15, random_state=10)
print(f"Training set size: {X_train.shape}, Validation set size: {X_val.shape}")
import albumentations as A
transforms_train = A.Compose([
A.VerticalFlip(p=0.2),
A.HorizontalFlip(p=0.7),
A.CoarseDropout(p=0.5),
A.RandomGamma(p=0.5),
A.RandomBrightnessContrast(p=1)
])
img = cv2.imread('train/00792e341f3c6eb33663e415d0715370.jpg')
plt.imshow(img)
plt.show()
augments = [A.VerticalFlip(p=1), A.HorizontalFlip(p=1), A.CoarseDropout(p=1), A.CLAHE(p=1)]
24
NAMAN GUPTA 00524302022
plt.subplots(figsize=(10, 10))
for i, aug in enumerate(augments):
plt.subplot(2, 2, i + 1)
aug_img = aug(image=img)['image']
plt.imshow(aug_img)
plt.show()
def aug_fn(img):
aug_data = transforms_train(image=img)
aug_img = aug_data['image']
return aug_img
@tf.function
def process_data(img, label):
aug_img = tf.numpy_function(aug_fn, [img], Tout=tf.float32)
return img, label
def decode_image(filepath, label=None):
img = tf.io.read_file(filepath)
img = tf.image.decode_jpeg(img)
img = tf.image.resize(img, [128, 128])
img = tf.cast(img, tf.float32) / 255.0
if label is None:
return img
return img, tf.one_hot(indices=label, depth=120, dtype=tf.float32)
train_ds = (
tf.data.Dataset
.from_tensor_slices((X_train, Y_train))
.map(decode_image, num_parallel_calls=AUTO)
.map(partial(process_data), num_parallel_calls=AUTO)
.batch(32)
.prefetch(AUTO)
25
NAMAN GUPTA 00524302022
val_ds = (
tf.data.Dataset
.from_tensor_slices((X_val, Y_val))
.map(decode_image, num_parallel_calls=AUTO)
.batch(32)
.prefetch(AUTO)
for img, label in train_ds.take(1):
print(img.shape, label.shape)
from tensorflow.keras.applications.inception_v3 import InceptionV3
pre_trained_model = InceptionV3(input_shape=(128, 128, 3), weights='imagenet',
include_top=False)
for layer in pre_trained_model.layers:
layer.trainable = False
last_layer = pre_trained_model.get_layer('mixed7')
print('Last layer output shape:', last_layer.output.shape)
last_output = last_layer.output
x = layers.Flatten()(last_output)
x = layers.Dense(256, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.Dense(256, activation='relu')(x)
x = layers.Dropout(0.3)(x)
x = layers.BatchNormalization()(x)
output = layers.Dense(120, activation='softmax')(x)
model = keras.Model(pre_trained_model.input, output)
model.compile(
optimizer='adam',
loss=keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.AUC()]
26
NAMAN GUPTA 00524302022
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
if logs.get('val_auc') is not None and logs.get('val_auc') > 0.99:
print('\nValidation AUC has reached above 99%. Stopping training.')
self.model.stop_training = True
es = EarlyStopping(patience=3, monitor='val_auc', restore_best_weights=True, mode='max')
lr = ReduceLROnPlateau(monitor='val_loss', patience=2, factor=0.5, verbose=1)
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=50,
verbose=1,
callbacks=[es, lr, myCallback()]
history_df = pd.DataFrame(history.history)
history_df.loc[:, ['loss', 'val_loss']].plot()
history_df.loc[:, ['auc', 'val_auc']].plot()
plt.show()
Output
27
NAMAN GUPTA 00524302022
Q9. (a) Write a program to demonstrate different activation functions.
Code
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 100)
def sigmoid(x): return 1 / (1 + np.exp(-x))
def tanh(x): return np.tanh(x)
def relu(x): return np.maximum(0, x)
def leaky_relu(x, alpha=0.1): return np.where(x > 0, x, alpha * x)
def softmax(x): return np.exp(x) / np.sum(np.exp(x), axis=0)
def swish(x): return x * sigmoid(x)
activation_functions = {
"Sigmoid": sigmoid(x),
"Tanh": tanh(x),
"ReLU": relu(x),
"Leaky ReLU": leaky_relu(x),
"Softmax": softmax(x),
"Swish": swish(x),
plt.figure(figsize=(10, 8))
for i, (name, y) in enumerate(activation_functions.items()):
plt.subplot(2, 3, i + 1)
plt.plot(x, y)
plt.title(name)
plt.axhline(0, color="black", linewidth=0.5, linestyle="--")
plt.axvline(0, color="black", linewidth=0.5, linestyle="--")
plt.tight_layout()
plt.show()
28
NAMAN GUPTA 00524302022
Output
29
NAMAN GUPTA 00524302022
Q9. (b) Write a program in TensorFlow to demonstrate different Loss functions.
Code
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
y_true = np.array([0.0, 1.0, 1.0, 0.0, 1.0])
y_pred = np.array([0.1, 0.9, 0.8, 0.2, 0.6])
loss_functions = {
"Mean Squared Error": tf.keras.losses.MeanSquaredError(),
"Mean Absolute Error": tf.keras.losses.MeanAbsoluteError(),
"Binary Crossentropy": tf.keras.losses.BinaryCrossentropy(),
"Categorical Crossentropy": tf.keras.losses.CategoricalCrossentropy(from_logits=True)
plt.figure(figsize=(12, 8))
for i, (name, loss_fn) in enumerate(loss_functions.items()):
loss_value = loss_fn(y_true, y_pred).numpy()
plt.subplot(2, 2, i+1)
plt.plot([0, 1], [loss_value, loss_value], label=f'{name}: {loss_value:.2f}', color='blue')
plt.title(name)
plt.xlabel('Index')
plt.ylabel('Loss')
plt.legend()
plt.tight_layout()
plt.show()
30
NAMAN GUPTA 00524302022
Output
31
NAMAN GUPTA 00524302022
Q10. Write a program to build an Artificial Neural Network by implementing the Back propagation
algorithm and test the same using appropriate data sets.
Code
import numpy as np
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
data = {
'feature1': [0.1, 0.2, 0.3, 0.4, 0.5],
'feature2': [0.5, 0.4, 0.3, 0.2, 0.1],
'label': [0, 0, 1, 1, 1] # Binary labels
df = pd.DataFrame(data)
X = df[['feature1', 'feature2']].values # Input features
y = df['label'].values # Output labels
model = Sequential()
model.add(Dense(8, input_dim=2, activation='relu')) # 2 input features, 8 neurons in hidden layer
model.add(Dense(1, activation='sigmoid')) # Output layer for binary classification
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=100, batch_size=1, verbose=1)
test_data = np.array([[0.2, 0.4]]) # New input data
prediction = model.predict(test_data)
predicted_label = (prediction > 0.5).astype(int) # Convert probability to binary
print(f"Predicted label: {predicted_label[0][0]}")
32
NAMAN GUPTA 00524302022
Output
33