DL Practical File
DL Practical File
Output –
Task 1 –
Basics of Deep Learning approach with required methods and
definition.
Program –
import numpy as np
import pandas as pd
df = pd.read_csv('/content/mushroom_cleaned.csv')
df.head()
output = df.duplicated().sum()
df.shape
# y = df['class']: Extracts the target variable (or label) 'class' from the
#DataFrame, which is the variable you want to predict.
#X = df.drop(columns = ['class']): Separates the features (independent
#variables) by dropping the 'class' column from the DataFrame. X
contains #the input data for the model
1. Neural Networks:
Loss Function:
o Purpose: Measures how well the model's predictions match the actual data.
o Examples: Mean Squared Error (MSE) for regression, Cross-Entropy Loss for
classification.
Optimization Algorithms:
o Purpose: Minimize the loss function by adjusting the weights of the network.
o Methods:
Gradient Descent: Updates weights in the direction of the negative
gradient of the loss function.
Variants: Stochastic Gradient Descent (SGD), Adam (Adaptive
Moment Estimation).
Backpropagation:
o Purpose: Algorithm used to calculate the gradient of the loss function with
respect to each weight by chain rule, crucial for updating weights in neural
networks.
4. Regularization Techniques:
Definition: Parameters set before the training process that affect the model's learning
process.
Examples: Learning rate, batch size, number of epochs, number of hidden layers,
activation functions.
6. Evaluation Metrics:
7. Transfer Learning:
Summary
Output –
Task 2 –
Program to create a basic ANN with printing summary of model
architecture of the model.
Program –
#Continuing from previous program
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
model = Sequential()
model.add(Dense(30,activation='relu',input_dim=9))
model.add(Dense(15,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.summary()
EXPLANATION: -
tensorflow: A popular open-source platform for machine learning and
deep learning.
keras: A high-level neural networks API running on top of TensorFlow. It
simplifies the process of building and training neural networks.
Sequential: A linear stack of layers in Keras, allowing you to build a
neural network layer by layer in a straightforward manner.
Dense: A fully connected layer where each neuron in the layer is connected
to every neuron in the previous layer. This is one of the most common layers
used in neural networks.
Task 3 –
Output –
Task – 3
Download Mushroom Dataset from Kaggle and create a Deep Neural
Network to check whether the mushroom is edible or not.
Program -
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
model = Sequential()
model.add(Dense(64, activation='relu', input_dim=8))
model.add(Dense(36, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='Adam')
model.fit(X_train_scaled, y_train, epochs=50, verbose=0)
y_log = model.predict(X_test_scaled)
y_pred = np.where(y_log>0.5,1,0)
from sklearn.metrics import accuracy_score
acc = accuracy_score(y_test, y_pred)
print(f"The accuracy is: {acc: 0.3F}")
Task 4 –
Output –
Task – 4
Write a program to develop the ANN model, then print the Accuracy
plot of the PIMA dataset downloaded from Kaggle.
Program -
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score,
f1_score, recall_score
import tkinter as tk
from tkinter import filedialog
def get_dataset():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(title="Select Dataset CSV",
filetypes=[("CSV files", "*.csv")])
if file_path:
data = pd.read_csv(file_path)
return data
else:
raise FileNotFoundError("No file selected!")
data = get_dataset()
X = data.drop(columns=['Outcome'])
y = data['Outcome']
X_train, X_temp, y_train, y_temp = train_test_split(X, y,
test_size=0.4, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp,
test_size=0.5, random_state=42)
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu',
input_shape=(X_train.shape[1],)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
y_pred = model.predict(X_test).flatten()
y_pred_binary = (y_pred > 0.5).astype(int)
# To Plot Graph
plt.figure(figsize=(12,6))
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy', fontsize=14,
fontweight='bold')
plt.xlabel('Epochs', fontsize=14, fontweight='bold')
plt.ylabel('Accuracy', fontsize=14, fontweight='bold')
plt.legend()
plt.grid(True)
plt.show()
Task 5 –
Output –
Task – 5
Write a program to develop the ANN model, then print the Accuracy
plot of the Mushroom dataset downloaded from Kaggle.
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score,
f1_score, recall_score
import tkinter as tk
from tkinter import filedialog
def get_dataset():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(title="Select Dataset CSV",
filetypes=[("CSV files", "*.csv")])
if file_path:
data = pd.read_csv(file_path)
return data
else:
raise FileNotFoundError("No file selected!")
data = get_dataset()
X = data.drop(columns=['Outcome'])
y = data['Outcome']
X_train, X_temp, y_train, y_temp = train_test_split(X, y,
test_size=0.4, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp,
test_size=0.5, random_state=42)
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu',
input_shape=(X_train.shape[1],)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(X_train, y_train, validation_data=(X_val, y_val),
epochs=10, verbose=0)
y_pred = model.predict(X_test).flatten()
y_pred_binary = (y_pred > 0.5).astype(int)
accuracy = accuracy_score(y_test, y_pred_binary)
precision = precision_score(y_test, y_pred_binary)
recall = recall_score(y_test, y_pred_binary)
f1 = f1_score(y_test, y_pred_binary)
print(f'Accuracy: {accuracy: .4f}')
print(f'Precision: {precision: .4f}')
print(f'Recall: {recall: .4f}')
print(f'F1: {f1: .4f}')
# To Plot Graph
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy', fontsize=14,
fontweight='bold')
plt.xlabel('Epochs', fontsize=14, fontweight='bold')
plt.ylabel('Accuracy', fontsize=14, fontweight='bold')
plt.legend()
plt.grid(True)
plt.show()
Task 6 –
Output –
1.
2.
3.
Task 6 –
Write a program to develop optimized ANN model, then print the
Accuracy plot of the Mushroom dataset downloaded from Kaggle.
Program –
import pandas as pd
import numpy as np
df = pd.read_csv('mushroom_cleaned.csv')
#REMOVING DUPLICATES
df1 = df.drop_duplicates()
#LABEL ENCODING
label_encoder = LabelEncoder()
for column in df1.columns:
df1.loc[:, column] = label_encoder.fit_transform(df1[column])
#STANDARD SCALING
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
sc.fit(x_train)
x_train = sc.transform(x_train)
x_test = sc.transform(x_test)
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout
model = Sequential()
model.add(Dense(128,activation='relu',input_dim= x_train.shape[1]))
model.add(Dropout(0.3))
model.summary() #1
#COMPILING MODEL
from tensorflow.keras.optimizers import Adam
optimizer = Adam(learning_rate=0.001)
model.compile(loss='binary_crossentropy', optimizer=optimizer,
metrics=['accuracy'])
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
#3
Task 7 –
Output –
1.
2.
Task 7 –
Write a Program to develop a ANN model, then print the accuracy
plot of TITANIC dataset.
Program –
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
df["Age"].fillna(df["Age"].median(), inplace=True)
df["Embarked"].fillna(df["Embarked"].mode()[0], inplace=True)
# Standardize features
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
# Build model
model = Sequential([
Dense(64, activation='relu', input_dim=x_train.shape[1]),
Dropout(0.3),
Dense(32, activation='relu'),
Dense(y.shape[1], activation='softmax')
])
# Compile model
optimizer = Adam(learning_rate=0.001)
model.compile(loss='categorical_crossentropy',
optimizer=optimizer, metrics=['accuracy'])
# Train model
history = model.fit(x_train, y_train, epochs=50, batch_size=32,
validation_data=(x_test, y_test), verbose=1)
# Evaluate model
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test Accuracy: {test_accuracy:.3f}") #1
# Plot accuracy
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Training and Validation Accuracy')
plt.show() #2
Task 8 –
Output –
1.
2.
3.
Task 8 –
Print the Confusion Matrix and Loss Plot of PIMA dataset and write
the value of the matrix such as precision, recall, F1 score.
Program –
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score,
recall_score, f1_score, roc_auc_score
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
# Data preprocessing
df[columns] = df[columns].apply(pd.to_numeric, errors='coerce')
df.dropna(inplace=True)
# Standardize features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y,
test_size=0.2, random_state=42)
# Build model
model = Sequential([
Dense(16, input_dim=8, activation='relu'),
Dropout(0.3),
Dense(12, activation='relu'),
Dense(8, activation='relu'),
Dense(1, activation='sigmoid')
])
# Compile model
optimizer = Adam(learning_rate=0.001)
model.compile(loss='binary_crossentropy', optimizer=optimizer,
metrics=['accuracy'])
# Train model
history = model.fit(X_train, y_train, epochs=150, batch_size=32,
validation_split=0.2, verbose=0, callbacks=[early_stopping])
# Predictions
y_pred = (model.predict(X_test) > 0.5).astype("int32")
# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()
# Plot Loss
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Loss Plot')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
# Evaluation Metrics
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred)
print(f"Precision: {precision:0.3F}")
print(f"Recall: {recall:0.3F}")
print(f"F1-Score: {f1:0.3F}")
print(f"ROC-AUC: {roc_auc:0.3F}")
Task 9 –
Output –
1.
Task 9 –
Print the accuracy of ANN model using PIMA dataset with dataset
with different values of Optimizers.
List of Optimizers: Adam, RMSprop, SGD, Adadelta, Adagrad,
Nadam.
Program –
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam, SGD, RMSprop,
Adadelta, Adagrad, Nadam
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42, stratify=y)
results = {}
for name, optimizer in optimizers.items():
accuracy, history = create_and_train_model(optimizer)
results[name] = accuracy
print(f"Optimizer: {name}, Test Accuracy: {accuracy:.3f}")
Task 10 –
Output –
Task 10 –
Object detection with pre – trained RetinaNet with Kerras.
Program –
pip install keras-retinanet opencv-python
import numpy as np
import cv2
import matplotlib.pyplot as plt
from keras_retinanet import models
from keras_retinanet.utils.image import preprocess_image,
resize_image
from keras_retinanet.utils.visualization import draw_box,
draw_caption
from keras_retinanet.utils.colors import label_color
# Load image
image_path = r"K:\market.jpg" # Path to the image
image = cv2.imread(image_path)
# Run detection
boxes, scores, labels =
model.predict_on_batch(np.expand_dims(image, axis=0))
# Correct for image scale
boxes /= scale
# Visualize detections
for box, score, label in zip(boxes[0], scores[0], labels[0]):
if score < 0.5: # Adjust threshold as needed
continue
Task 11 –
Output –
Task 11 –
NEURAL RECOMMENDATION SYSTEM.
Program –
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Embedding, Flatten, Input, Dot
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
def build_model(self):
# Input layers for user, positive item, and negative item
user_input = Input(shape=(1,), name="user_input")
pos_item_input = Input(shape=(1,),
name="positive_item_input")
neg_item_input = Input(shape=(1,),
name="negative_item_input")
# User embedding
user_embedded = Flatten()(user_embedding(user_input))
# Simulate data
num_users = 1000
num_items = 1000
embedding_dim = 32
Task 12 –
Output –
Task 12 -
Implementation of Backpropagation in neural network using numpy.
Program -
import numpy as np
import matplotlib.pyplot as plt
def sigmoid_derivative(x):
return x * (1 - x)
def plot_loss(self):
plt.plot(self.loss_history)
plt.xlabel("Epochs")
plt.ylabel("Mean Squared Error Loss")
plt.title("Loss Over Epochs")
plt.show()
# Example usage
if __name__ == "__main__": # Corrected __name__ check
# XOR dataset
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
Task 13 –
Output –
Task 13 –
Implementation of Neural Recommender Systems with Implicit
Feedback and the Triplet Loss.
Program –
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Embedding, Flatten, Input,
Concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
def build_model(self):
# Input layers for user, positive item, and negative item
user_input = Input(shape=(1,), name="user_input")
pos_item_input = Input(shape=(1,),
name="positive_item_input")
neg_item_input = Input(shape=(1,),
name="negative_item_input")
# User embedding
user_embedded = Flatten()(user_embedding(user_input))
# Simulate data
num_users = 1000
num_items = 1000
embedding_dim = 32
Task 14 –
Output –
Task 14 –
Write a program to print the accuracy of CNN using max pooling
technique.
Program –
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D,
Flatten, Dense
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt
Task 15 –
Output –
Task 15 –
Write a program to print the accuracy of CNN using average pooling
technique.
Program –
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, AveragePooling2D,
Flatten, Dense
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt
Task 16 –
Output –
Task 16 –
Write a program to print the accuracy of CNN using global pooling
technique.
Program –
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,
GlobalAveragePooling2D, Dense
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt