file
file
VISION OF INSTITUTE
MISSION OF INSTITUTE
INDEX
S.No Experiment Name Date Marks Remark Updated Faculty
Laboratory Class Viva Marks Signature
Assessment Participation (5 Marks)
(15 Marks) (5 Marks)
INDEX
S.No Experiment Name Date Marks Remark Updated Faculty
Laboratory Class Viva Marks Signature
Assessment Participation (5 Marks)
VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY
Aim: To explore the basic features of Tensorflow and Keras packages in Python.
Objectives:
Understand the foundational functionalities of TensorFlow and Keras for deep learning.
Explore how to build, compile, and train basic neural network models using Keras.
Theory:
TensorFlow and Keras are powerful libraries in Python that form the backbone of deep learning
model implementation. They provide a comprehensive ecosystem for building, training, and
deploying machine learning models. These libraries are pivotal for both academic and practical
experiments in the field of deep learning.
1. TensorFlow:
TensorFlow, developed by Google, is an open-source machine learning framework designed for high-
performance numerical computations. It enables researchers and developers to efficiently create and
train machine learning models across various platforms. TensorFlow's core strength lies in its ability
to handle tensors, which are multi-dimensional arrays that form the foundation of all computations in
deep learning.
GPU/TPU Acceleration: TensorFlow optimally utilizes hardware resources like GPUs and
TPUs for faster training and inference.
Scalability: Its distributed training capabilities allow it to handle large-scale models and
datasets across multiple devices and servers.
Versatility: TensorFlow supports both low-level control for custom operations and high-level
APIs like Keras for simplicity.
2. Keras:
Keras is a high-level neural network API integrated with TensorFlow. It is designed to be user-
friendly while still providing powerful features for advanced customization. Keras reduces the
complexity of building deep learning models by offering intuitive syntax and ready-to-use
components for common tasks.
Simplified Model Building: Keras offers Sequential and Functional APIs to define models
effortlessly.
3. Core Concepts:
Tensors:
The primary data structure in TensorFlow, tensors represent multi-dimensional arrays that
enable efficient numerical computation. Operations on tensors are highly optimized for both
CPU and GPU execution.
Layers:
Layers are the building blocks of a deep learning model. In Keras, layers perform specific
transformations, such as linear computations, activation functions, or normalization, to
process input data progressively.
Regression:
In regression tasks, the model predicts continuous outputs. TensorFlow and Keras simplify
the creation of regression models by providing built-in loss functions (e.g., Mean Squared
Error) and metrics to evaluate model performance.
Classification:
For classification tasks, the model predicts categorical outputs, such as labels for data points.
TensorFlow and Keras support multi-class and binary classification through layers like
Dense, activation functions like softmax or sigmoid, and loss functions like
categorical_crossentropy.
By exploring TensorFlow and Keras, learners gain hands-on experience with key deep learning
principles. This experiment lays the groundwork for understanding tensor operations, building neural
networks, and solving real-world problems through regression and classification tasks. These tools
bridge the gap between theoretical concepts and practical applications in the realm of deep learning.
# Regression
import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
housing = fetch_california_housing()
X_train_full, X_test, y_train_full, y_test =
train_test_split(housing.data, housing.target, random_state=42)
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full,
y_train_full, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(30, activation='relu',
input_shape=X_train.shape[1:]),
tf.keras.layers.Dense(1)
])
model.compile(loss='mean_squared_error',
optimizer=tf.keras.optimizers.SGD(learning_rate=0.01))
# Classification
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris
y = tf.keras.utils.to_categorical(y, num_classes=3)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu', input_shape=(4,)),
tf.keras.layers.Dense(3, activation='softmax')
])
model.compile(optimizer='sgd',
loss='categorical_crossentropy',
metrics=['accuracy'])
Output:
# Regression
Learning Outcomes:
Understand how to build and train regression and classification models using TensorFlow and
Keras.
Gain insights into key evaluation metrics like Mean Squared Error (MSE) for regression and
Accuracy or F1-Score for classification tasks.
Develop the ability to preprocess data and apply activation functions, optimizers, and loss
functions suitable for different problem domains.
Objectives:
To implement an ANN model in Python for regression and evaluate its performance using
MSE.
To implement an ANN model in Python for classification and assess its accuracy using
performance metrics.
Theory:
Artificial Neural Networks (ANNs) are computational models inspired by the human brain's
structure and functioning. They consist of layers of interconnected nodes (neurons) which
process and transform input data through various layers to produce an output. The power of
ANNs lies in their ability to learn from data, identify patterns, and generalize predictions,
making them highly suitable for various tasks such as regression, classification, and time
series forecasting.
Each neuron in the hidden layers and output layer processes inputs from previous layers and
passes them through an activation function, which introduces non-linearity into the model.
The weights of the connections between neurons are learned during training by using
optimization techniques like gradient descent.
In regression tasks, the objective is to predict a continuous numerical output based on input
features. For example, predicting house prices based on location, size, and other features is a
typical regression problem.
Input Layer: Takes in feature data (e.g., square footage, number of bedrooms).
Hidden Layers: Processes the data through neurons.
To train an ANN for regression, the model adjusts its weights to minimize the difference
between the predicted output and the actual target value using a loss function like Mean
Squared Error (MSE). The training process involves backpropagation, where errors are
propagated backward through the network, and weights are updated to reduce the loss.
1. Data Preprocessing: Normalization and scaling of data are crucial for ensuring
efficient learning.
2. Model Construction: A neural network model is constructed with appropriate layers
and activation functions.
3. Training the Model: The model is trained using a dataset, adjusting weights based
on the error at each step.
4. Evaluation: The model’s performance is evaluated using metrics like MSE or Root
Mean Squared Error (RMSE).
Python libraries like Keras and TensorFlow are often used to implement ANNs for
regression, as they provide simple APIs for defining and training neural networks.
In classification tasks, the goal is to categorize data into distinct classes or categories. For
example, classifying images as either "cat" or "dog" or predicting whether a customer will
buy a product based on their behavior are typical classification problems.
The structure of an ANN for classification is similar to that of regression, but with key
differences in the output layer:
Input Layer: Receives features of the data (e.g., image pixels, demographic
features).
Hidden Layers: Processes the data through neurons.
Output Layer: Uses a softmax or sigmoid activation function to output class
probabilities. In binary classification, the output layer consists of a single neuron with
a sigmoid activation function. For multi-class classification, a softmax activation
function is used.
For classification tasks, the model is trained using a loss function like Cross-Entropy Loss,
which measures the difference between predicted probabilities and actual class labels. During
training, the model learns to adjust its weights to reduce this loss.
1. Overfitting: ANN models, especially deep networks with many layers, can easily
overfit the training data, leading to poor generalization. Regularization techniques
such as dropout, L2 regularization, or early stopping can help mitigate overfitting.
2. Data Quality: ANN models are sensitive to the quality and quantity of data. Proper
data preprocessing, including handling missing values, scaling, and feature
engineering, is crucial.
3. Model Tuning: The performance of an ANN heavily depends on hyperparameters
like the number of hidden layers, neurons, learning rate, and batch size. Grid search
or random search methods are often employed to optimize these hyperparameters.
4. Computational Complexity: Training deep neural networks can be computationally
expensive, requiring powerful hardware (e.g., GPUs) and careful resource
management.
Several Python libraries make it easy to implement ANN models for both regression and
classification:
1. TensorFlow/Keras: These are the most popular frameworks for building and training
neural networks. Keras provides a high-level API that is user-friendly, while
TensorFlow provides more flexibility and control for advanced users.
2. PyTorch: Another powerful deep learning framework that offers flexibility and
dynamic computation graphs, making it suitable for research and development.
3. Scikit-learn: Although primarily used for traditional machine learning algorithms,
Scikit-learn provides simple neural network implementations (MLPRegressor and
MLPClassifier) that are useful for smaller-scale tasks.
Conclusion
ANNs are versatile tools for both regression and classification tasks in machine learning. By
leveraging their ability to model complex relationships and learn from data, they can achieve
high accuracy in predicting continuous values and categorizing data into distinct classes.
Python, with its rich ecosystem of libraries like Keras and TensorFlow, provides a powerful
environment for implementing, training, and evaluating ANN models. However, successful
deployment requires careful attention to data quality, model tuning, and computational
resources to avoid common pitfalls like overfitting and high computational costs.
#Regression
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
housing = fetch_california_housing()
df = pd.DataFrame(housing.data, columns=housing.feature_names)
df['target'] = housing.target
df.head()
df=df.dropna()
X.isnull()
y.isnull()
sns.pairplot(df)
df.corr()
corrmat = df.corr()
top_corr_features = corrmat.index
plt.figure(figsize=(20,20))
g=sns.heatmap(df[top_corr_features].corr(),annot=True,cmap="RdYlGn")
corrmat.index
X.head()
print(model.feature_importances_)
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LeakyReLU,PReLU,ELU
from keras.layers import Dropout
NN_model = Sequential()
prediction=NN_model.predict(X_test)
y_test
sns.distplot(y_test.values.reshape(-1,1)-prediction)
plt.scatter(y_test,prediction)
# Classification
import numpy as np
import pandas as pd
import tensorflow as tf
iris = load_iris()
X = iris.data
y = iris.target
y = tf.keras.utils.to_categorical(y, num_classes=3)
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units=6,activation="relu"))
ann.add(tf.keras.layers.Dense(units=6,activation="relu"))
ann.add(tf.keras.layers.Dense(units=3,activation="sigmoid"))
ann.compile(optimizer="adam",loss="categorical_crossentropy",metrics=['
accuracy'])
ann.fit(X_train,y_train,batch_size=32,epochs = 100)
#Regression
df.head()
X.isnull()
y.isnull()
df.corr()
corrmat.index
X.head()
print(model.feature_importances_)
plt.show()
NN_model.fit()
y_test
sns.distplot(y_test.values.reshape(-1,1)-prediction)
#Classification
ann.fit()
Learning Outcomes:
Gain the ability to build and train ANN models for both regression and classification tasks in
Python.
Learn to preprocess data and evaluate model performance using key metrics like MSE,
accuracy, and F1-score.
Aim: Implementation of Convolution Neural Network for MRI Data Set in Python.
Objectives:
Theory:
Deep learning is a subset of machine learning that focuses on training artificial neural
networks to learn from large amounts of data. Among deep learning architectures,
Convolutional Neural Networks (CNNs) are particularly effective for analyzing image-
based data, making them suitable for medical imaging tasks such as MRI (Magnetic
Resonance Imaging) analysis.
MRI scans provide detailed internal body structures, helping in medical diagnosis, treatment
planning, and research. However, manually interpreting MRI images is time-consuming and
subject to variability. CNNs help automate and improve MRI analysis by detecting patterns,
classifying abnormalities, and segmenting regions of interest.
CNNs are specialized deep learning models designed for image processing tasks. They use
convolutional layers to extract spatial features from images, making them efficient in
identifying patterns and structures within MRI scans. The fundamental components of a CNN
include:
CNNs have significantly advanced medical imaging applications, enabling automated tumor
detection, brain segmentation, and disease classification from MRI datasets.
MRI datasets contain grayscale images that represent different tissues and structures within
the human body. These datasets are crucial for training CNN models in various medical
applications, such as:
MRI datasets usually require preprocessing steps, including normalization, resizing, and
augmentation, to improve model performance.
Python, with deep learning libraries such as TensorFlow and Keras, provides an efficient
framework for implementing CNNs. The basic steps involved in developing a CNN for MRI
image classification include:
1. Loading the MRI Dataset: Using libraries like Pandas, NumPy, or TensorFlow
datasets.
2. Preprocessing the Data: Resizing images, normalizing pixel values, and
augmenting the dataset.
3. Building the CNN Model: Defining layers such as convolutional, pooling, and fully
connected layers.
4. Compiling and Training the Model: Using an optimizer (SGD, Adam) and loss
function (categorical cross-entropy).
5. Evaluating the Model: Testing the model on unseen MRI images and assessing its
accuracy.
Brain Tumor Detection: CNNs can classify MRI images as normal or containing
tumors with high accuracy.
Alzheimer’s Disease Prediction: Identifying structural changes in brain MRI scans
to diagnose neurodegenerative diseases.
Medical Image Segmentation: Extracting regions of interest, such as tumors or
organs, from MRI scans for precise analysis.
Conclusion
The implementation of a Convolutional Neural Network (CNN) for MRI image classification
is a powerful deep learning application in medical imaging. CNNs efficiently learn spatial
hierarchies of features and can significantly improve disease detection accuracy. With
Python-based frameworks like TensorFlow and Keras, researchers and medical professionals
can develop robust models to analyze MRI scans, contributing to advancements in medical
diagnostics and healthcare.
import pandas as pd
import numpy as np
import cv2
import os
import random
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.image import imread
import tensorflow as tf
import tensorflow_hub as hub
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import warnings
warnings.filterwarnings('ignore')
MAIN_DIR = "/content/brain_tumor_dataset/"
plt.tight_layout()
plt.show()
filepath = MAIN_DIR
images, labels, filenames = [], [], []
for gt in ['yes','no']:
filepath_gt = filepath + gt
for f in os.listdir(filepath_gt):
filename = filepath_gt + '/' + f
if '._' not in filename: # metadata files created by macos
# load image
photo =
tf.keras.utils.load_img(path=filename,target_size=(200, 200))
# store to array
images.append(resized_image)
filenames.append(filename)
if gt=='yes':
label = 1
labels.append(label)
elif gt=='no':
label = 0
labels.append(label)
gtFr2 = pd.concat([trainFr,testFr],axis=0)
gtFr2.set_index(['filename']).to_csv('base_model_train_test.csv')
files_train = []
X_train = []
y_train = []
# store to array
X_train.append(resized_image)
y_train.append(row['gt'])
files_train.append(row['filename'])
files_test = []
X_test = []
y_test = []
# store to array
X_test.append(resized_image)
y_test.append(row['gt'])
files_test.append(row['filename'])
# create model
model = Sequential()
# Plot loss
axes[0].plot(epochs, loss, label="training_loss")
axes[0].plot(epochs, val_loss, label="val_loss")
axes[0].set_title("Loss")
axes[0].set_xlabel("Epochs")
axes[0].legend()
# Plot accuracy
axes[1].plot(epochs, accuracy, label="training_accuracy")
axes[1].plot(epochs, val_accuracy, label="val_accuracy")
axes[1].set_title("Accuracy")
axes[1].set_xlabel("Epochs")
axes[1].legend()
plot_curves(history)
# make predictions
predictions = model.predict(X_test)
# store as probabilities
probabilities = [p[0] for p in predictions]
testFr['pred'] = probabilities
dataPos = testFr[testFr['gt']==1]['pred'].values
dataNeg = testFr[testFr['gt']==0]['pred'].values
dataAll = np.concatenate((dataPos, dataNeg))
lblArr = np.zeros(len(dataAll), dtype=bool)
lblArr[0:len(dataPos)] = True
sens = tpr[cutoffIdx]
print('Sensitivity: {:0.2f}'.format(sens))
spec = 1 - fpr[cutoffIdx]
print('Specificity: {:0.2f}'.format(spec))
model.fit()
# Evaluation Metrics
Learning Outcomes:
Gain the ability to build and train CNN models for classification tasks on images in Python.
Learnt to preprocess and scale image data to train CNN model and evaluate model
performance using key metrics like accuracy, sensitivity and kappa score.
Objectives:
Theory:
Deep learning is a subset of machine learning that enables computers to learn from large
datasets using artificial neural networks. One of the critical challenges in machine learning is
dealing with high-dimensional data, which can lead to increased computational costs and
reduced model performance due to the "curse of dimensionality." Dimensionality reduction
is a technique used to transform high-dimensional data into a lower-dimensional
representation while preserving important features.
An Autoencoder (AE) is a neural network designed to encode input data into a lower-
dimensional space (encoding) and then reconstruct the original input (decoding). It consists
of two main components:
Autoencoders are widely used for dimensionality reduction and feature extraction, especially
in cases where linear methods like PCA (Principal Component Analysis) are insufficient.
Some key applications include:
Autoencoders can be implemented using TensorFlow and Keras in Python. The basic steps
include:
1. Loading the Dataset: Common datasets such as MNIST or CIFAR-10 are used to
train autoencoders.
2. Preprocessing the Data: Normalization and reshaping of input images.
3. Building the Autoencoder Model:
o A simple autoencoder consists of an encoder (Dense layers) followed by a
decoder (Dense layers).
4. Training the Autoencoder: Using an Adam optimizer and Mean Squared Error
(MSE) loss.
5. Evaluating the Performance: Checking the quality of reconstructed images and
analyzing dimensionality reduction.
Conclusion
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 tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, LeakyReLU
from tensorflow.keras.optimizers import RMSprop
from sklearn.manifold import TSNE
# Load Dataset
file_path = "train.csv" # Change if needed
df = pd.read_csv(file_path)
print(df.head())
# EDA
print(df.info())
print(df.describe())
print(df.isnull().sum().sum(), "missing values")
# Normalize Data
scaler = StandardScaler()
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Autoencoder Architecture
encoding_dim = 10 # Dimension reduction size
input_dim = X_train.shape[1]
decoded = Dense(32)(encoded)
decoded = LeakyReLU(alpha=0.1)(decoded)
decoded = Dense(64)(decoded)
decoded = LeakyReLU(alpha=0.1)(decoded)
decoded = Dense(input_dim, activation='linear')(decoded)
# Define Autoencoder
autoencoder = Model(input_layer, decoded)
autoencoder.compile(optimizer=RMSprop(learning_rate=0.0001),
loss='mae')
# Train Autoencoder
autoencoder.fit(X_train, X_train, epochs=50, batch_size=256,
shuffle=True, validation_data=(X_test, X_test))
# Extract Encoder
encoder = Model(input_layer, encoded)
X_train_encoded = encoder.predict(X_train)
X_test_encoded = encoder.predict(X_test)
# t-SNE Visualization
tsne = TSNE(n_components=2, random_state=42)
X_embedded = tsne.fit_transform(X_test_encoded)
plt.scatter(X_embedded[:, 0], X_embedded[:, 1],
c=y_test[:len(X_embedded)], cmap='coolwarm', alpha=0.5)
plt.colorbar()
plt.title("t-SNE Visualization of Encoded Features")
plt.show()
# EDA
# t-SNE Visualization
Learning Outcomes:
Objectives:
Theory:
1. Introduction
Autoencoders are a type of artificial neural network used for unsupervised learning of
efficient codings in data. They are primarily utilized for dimensionality reduction, denoising,
and feature extraction. In this experiment, we apply autoencoders to the MNIST dataset, a
widely used benchmark dataset for handwritten digit recognition. We first implement a basic
autoencoder using fully connected layers and later enhance its performance by employing
convolutional layers.
1. Encoder: This part compresses the input data into a lower-dimensional representation
(latent space).
2. Decoder: This part reconstructs the input data from the compressed representation.
The training objective is to minimize the reconstruction loss between and , usually measured
by Mean Squared Error (MSE) or Binary Cross-Entropy (BCE).
The MNIST dataset consists of 60,000 training images and 10,000 test images of handwritten
digits (0-9), each of size 28x28 pixels. The data is normalized to the range to improve
training efficiency.
After training with Adam optimizer and MSE loss function, the autoencoder is able to
reconstruct the input images reasonably well. However, due to the dense architecture, spatial
relationships in images are not efficiently captured.
The CAE significantly improves the quality of reconstructed images compared to the fully
connected autoencoder. The main advantages include:
5. Conclusion
Autoencoders provide a powerful method for image compression and feature extraction.
While fully connected autoencoders serve as a baseline, convolutional autoencoders
significantly enhance performance by leveraging spatial hierarchies. Future work can include
Variational Autoencoders (VAEs) for more structured latent spaces and application of these
techniques to real-world datasets beyond MNIST.
import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
class_labels = [
"T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"
]
plt.figure(figsize=(10, 6))
for i in range(10):
plt.subplot(2, 5, i+1)
plt.imshow(X_train[i], cmap="gray")
plt.title(class_labels[y_train[i]])
plt.axis("off")
plt.tight_layout()
plt.show()
class_counts = pd.Series(y_train).value_counts().sort_index()
plt.figure(figsize=(8, 5))
sns.barplot(x=class_counts.index, y=class_counts.values,
palette="coolwarm")
plt.xticks(ticks=range(10), labels=class_labels, rotation=45)
plt.xlabel("Class Labels")
plt.ylabel("Count")
plt.title("Distribution of Fashion MNIST Classes")
plt.show()
autoencoder.compile(optimizer='adam', loss='mean_squared_error')
# Convolutional Autoencoder
input_layer = layers.Input(shape=(28, 28, 1))
# Encoder
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')
(input_layer)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
# Decoder
x = layers.Conv2DTranspose(64, (3, 3), activation='relu',
padding='same')(encoded)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2DTranspose(32, (3, 3), activation='relu',
padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
# Define model
autoencoder = models.Model(input_layer, decoded)
plt.figure(figsize=(20, 4))
for i in range(n):
# Original images
plt.subplot(2, n, i + 1)
plt.imshow(X_test[i].reshape(28, 28), cmap='gray')
plt.axis('off')
# Reconstructed images
plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(28, 28), cmap='gray')
plt.axis('off')
plt.show()
# Image count
# Encoded shape:
# Original vs Reconstructed
Learning Outcomes:
Objectives:
Theory:
Deep learning is a subset of machine learning that enables computers to learn from large
datasets using artificial neural networks. One of the critical challenges in machine learning is
dealing with high-dimensional data, which can lead to increased computational costs and
reduced model performance due to the "curse of dimensionality." Dimensionality reduction
is a technique used to transform high-dimensional data into a lower-dimensional
representation while preserving important features.
An Autoencoder (AE) is a neural network designed to encode input data into a lower-
dimensional space (encoding) and then reconstruct the original input (decoding). It consists
of two main components:
Autoencoders are widely used for dimensionality reduction and feature extraction, especially
in cases where linear methods like PCA (Principal Component Analysis) are insufficient.
Some key applications include:
Autoencoders can be implemented using TensorFlow and Keras in Python. The basic steps
include:
6. Loading the Dataset: Common datasets such as MNIST or CIFAR-10 are used to
train autoencoders.
7. Preprocessing the Data: Normalization and reshaping of input images.
8. Building the Autoencoder Model:
o A simple autoencoder consists of an encoder (Dense layers) followed by a
decoder (Dense layers).
9. Training the Autoencoder: Using an Adam optimizer and Mean Squared Error
(MSE) loss.
10. Evaluating the Performance: Checking the quality of reconstructed images and
analyzing dimensionality reduction.
Conclusion
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
import numpy as np
import matplotlib.pyplot as plt
# Encoder
# Decoder
decoded = Dense(784, activation="sigmoid")(encoded)
# Reconstructed images
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(28, 28), cmap="gray")
plt.axis("off")
plt.show()
Learning Outcomes:
Theory:
Introduction:
Stock price prediction is a crucial task in financial markets, where traders and investors aim
to forecast future prices based on historical data. Recurrent Neural Networks (RNNs),
specifically Long Short-Term Memory (LSTM) networks, are widely used for time series
forecasting due to their ability to learn temporal dependencies. In this experiment, we
implement an LSTM-based model for stock price prediction using historical stock data from
Yahoo Finance. The model learns patterns from past prices and predicts future values based
on these observations.
Conclusion:
LSTM networks provide a robust approach for stock price prediction by learning temporal
dependencies in historical data. Despite their effectiveness, challenges such as market
volatility and external influences limit their accuracy. Future improvements can include:
● Incorporating additional technical indicators (e.g., moving averages, RSI).
● Using hybrid models combining LSTMs with attention mechanisms.
● Exploring Transformer-based architectures for enhanced forecasting.
This experiment demonstrates the potential of deep learning in financial market analysis,
paving the way for more sophisticated predictive models.
import numpy as np
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
# Download stock data
def get_stock_data(ticker, start, end):
df = yf.download(ticker, start=start, end=end)
return df[['Close']]
X, y = [], []
for i in range(time_steps, len(data_scaled)):
X.append(data_scaled[i-time_steps:i, 0])
y.append(data_scaled[i, 0])
X, y = np.array(X), np.array(y)
X = np.reshape(X, (X.shape[0], X.shape[1], 1))
return X, y, scaler
# Build LSTM model
def build_lstm_model(input_shape):
model = Sequential([
LSTM(units=50, return_sequences=True, input_shape=input_shape),
Dropout(0.2),
LSTM(units=50, return_sequences=True),
Dropout(0.2),
LSTM(units=50),
Dropout(0.2),
Dense(units=1)
])
model.compile(optimizer='adam', loss='mean_squared_error')
return model
# Main execution
if __name__ == "__main__":
ticker = "AAPL"
start_date = "2020-01-01"
end_date = "2024-01-01"
predictions = model.predict(X_test)
predictions = scaler.inverse_transform(predictions.reshape(-1, 1))
Outputs:
# model training
Learning Outcomes:
● Understand the architecture and working principles of LSTM networks for time-series
forecasting.
● Develop a stock price prediction model using Python and TensorFlow.
● Analyze model performance and visualize predictions effectively.
Theory:
2. Model Construction -
Code:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Flatten, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.applications.vgg16 import preprocess_input
# Final model
model = Model(inputs=base_model.input, outputs=x)
model.compile(optimizer=Adam(), loss='categorical_crossentropy',
metrics=['accuracy'])
print("\nClassification Report:")
print(classification_report(y_true, y_pred_classes))
# Confusion Matrix
cm = confusion_matrix(y_true, y_pred_classes)
plt.figure(figsize=(10,7))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title("Confusion Matrix")
plt.subplot(1,2,2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.title("Model Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.tight_layout()
plt.show()
Output:
# Classification report
# Confusion Matrix
Learning Outcomes:
1. Understood the application of transfer learning using the pre-trained VGG16 model
for efficient image classification.
2. Learned the structure and functionality of VGG16, including its convolutional layers,
pooling layers, and fully connected layers.
3. Gained the ability to evaluate and interpret model performance using classification
metrics and result visualizations.
Objectives:
To implement the Inception V3 model for efficient and accurate image classification.
To evaluate the performance benefits of using a deep, multi-scale convolutional
architecture.
Theory:
2. Model Construction -
Inception V3 can be used in two ways: (a) from scratch with randomly initialized weights, or
(b) by leveraging a pre-trained version on ImageNet and fine-tuning it for a specific dataset.
The top classification layer is modified to match the number of classes in the current task.
The model is compiled using categorical crossentropy for multi-class classification and an
appropriate optimizer like RMSprop or Adam.
Visualization of Results:
Plotting training vs. validation loss and accuracy over epochs gives insight into overfitting or
underfitting. Class activation maps (CAMs) or Grad-CAMs may also be used to visualize
which parts of the image influenced the model's predictions, enhancing interpretability and
trust in the model.
Conclusion
The Inception V3 model is an advanced deep learning architecture that brings significant
improvements to image classification through its scalable and efficient design. It effectively
captures both local and global patterns within images by combining convolutions of different
sizes. Using techniques like factorized convolutions, auxiliary classifiers, and batch
normalization, the model achieves a strong balance between performance and
computational efficiency. Evaluation with metrics and visualizations confirms its ability to
generalize well, making it highly suitable for complex image classification tasks in real-world
scenarios.
import os
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.inception_v3 import InceptionV3,
preprocess_input
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
train_dir = '/content/sports_dataset/sports_dataset/train'
test_dir = '/content/sports_dataset/sports_dataset/test'
train_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
rotation_range=20,
zoom_range=0.2,
horizontal_flip=True
)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(299, 299),
batch_size=32,
class_mode='categorical'
)
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(299, 299),
batch_size=32,
class_mode='categorical',
shuffle=False
)
# Compile model
model.compile(optimizer=Adam(learning_rate=0.0001),
loss='categorical_crossentropy',
metrics=['accuracy'])
# Train model
history = model.fit(
train_generator,
epochs=10,
validation_data=test_generator
)
class_labels = list(test_generator.class_indices.keys())
# Classification report
print("\nClassification Report:")
print(classification_report(y_true, y_pred,
target_names=test_generator.class_indices.keys()))
image_paths_and_labels = []
for class_name in class_labels:
folder_path = os.path.join(test_dir, class_name)
for filename in os.listdir(folder_path):
if filename.endswith(('.jpg', '.jpeg', '.png')):
image_path = os.path.join(folder_path, filename)
image_paths_and_labels.append((image_path, class_name))
num_images_to_display = 9
random_images = random.sample(image_paths_and_labels,
num_images_to_display)
plt.figure(figsize=(12, 12))
for i, (img_path, true_label) in enumerate(random_images):
predicted_class = predict_image(img_path, model, class_labels)
plt.subplot(3, 3, i + 1)
img = image.load_img(img_path, target_size=(299, 299))
plt.imshow(img)
plt.title(f"Pred: {predicted_class}\nTrue: {true_label}")
plt.axis('off')
plt.tight_layout()
plt.show()
plt.subplot(1,2,2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.title('Loss Over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.tight_layout()
plt.show()
Objectives:
Bi-LSTM Layer – Comprises two LSTM layers operating in opposite directions; they
extract both past and future dependencies for deeper context understanding.
Dropout Layer – Randomly drops units to reduce overfitting and improve model
generalization.
Dense Layer – Processes the combined outputs of the Bi-LSTM and projects them
to the desired output dimension.
1. Data Preprocessing-
The dataset is first cleaned (removing stop words, lowercasing, punctuation, etc.) and
tokenized into sequences of integers. These sequences are padded to ensure consistent
input length, enabling batch processing.
2. Model Construction-
A Sequential model is built starting with an Embedding layer. This is followed by a Bi-LSTM
layer that processes each input sequence in both directions. A dropout layer is added for
regularization, followed by a dense layer and a softmax/sigmoid output layer for
classification.
3. Training the Model-
The model is compiled using optimizers like Adam and loss functions such as binary or
categorical crossentropy, depending on the task. During training, the model adjusts its
internal weights using backpropagation through time to minimize the loss.
4. Evaluation and Prediction-
The trained model is tested on a separate validation/test dataset. Predictions are generated
and compared with actual labels to calculate performance metrics. The bidirectional
architecture typically results in more accurate predictions, especially in context-heavy tasks.
Model Evaluation and Visualization
Performance is assessed using standard classification metrics to ensure that the model
generalizes well to unseen data.
Performance Metrics:
Accuracy – Measures the overall percentage of correct predictions.
Precision – Indicates how many of the predicted positives are actually correct.
Recall – Reflects how many actual positives the model correctly identified.
F1-Score – Harmonic mean of precision and recall, useful in cases of class
imbalance.
Visualization of Results
Accuracy and loss curves across epochs are plotted to assess learning behavior and
convergence. Confusion matrices visualize true vs. predicted labels, making it easier to
identify which classes are well-learned and which are misclassified.
Conclusion
The Bi-directional LSTM architecture effectively enhances model understanding of textual
data by capturing both previous and future context, leading to better performance in
classification tasks. Its dual-layer setup allows deeper semantic learning, resulting in
improved accuracy and generalization. Evaluating through metrics and visual tools confirms
the strength of Bi-LSTM over standard unidirectional models in natural language
applications.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('ecommerce_datsaset.csv',usecols=['Text', 'label'])
df['Text'] = df['Text'].astype(str)
label_counts = df['label'].value_counts()
valid_labels = label_counts[label_counts > 1].index
df = df[df['label'].isin(valid_labels)]
label_encoder = LabelEncoder()
df['label_encoded'] = label_encoder.fit_transform(df['label'])
tokenizer = Tokenizer(oov_token='<OOV>')
tokenizer.fit_on_texts(df['Text'])
sequences = tokenizer.texts_to_sequences(df['Text'])
word_index = tokenizer.word_index
max_length = 100
X = pad_sequences(sequences, maxlen=max_length, padding='post')
y = df['label_encoded'].values
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, stratify=y, random_state=42)
# Model
vocab_size = len(word_index) + 1
embedding_dim = 64
model = Sequential([
Embedding(vocab_size, embedding_dim, input_length=max_length),
Bidirectional(LSTM(64, return_sequences=False)),
Dropout(0.5),
Dense(32, activation='relu'),
Dense(len(np.unique(y)), activation='softmax')
])
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=10, batch_size=32,
validation_split=0.2)
# Evaluation
y_pred = np.argmax(model.predict(X_test), axis=1)
plt.subplot(1,2,2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.legend()
plt.title('Loss over epochs')
plt.show()
# Confusion matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8,6))
Learning Outcomes:
1. Understood the working of Bi-directional LSTM and its ability to capture context from
both past and future sequences in text data.
2. Gained practical experience in building and training Bi-LSTM models for text
classification using Python and deep learning frameworks.
3. Learned to evaluate model performance using metrics like accuracy, precision, recall,
and F1-score for reliable classification results.