Deep Learning Lab With Output

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Deep learning

Experiment 1: Build a conventional neural network for image recognition.


For image recognition a popular deep learning framework like TensorFlow is used. In this
example, we'll use the Fashion MNIST dataset, which contains grayscale images of clothing
items.
Please note that building a neural network involves a lot of hyperparameter tuning and
experimentation to achieve optimal results. This example is meant to provide a starting point.

Program:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.utils import to_categorical

# Load and preprocess the dataset


(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# Normalize pixel values to be between 0 and 1


train_images = train_images.astype('float32') / 255.0
test_images = test_images.astype('float32') / 255.0

# Convert labels to one-hot encoded format


num_classes = 10
train_labels = to_categorical(train_labels, num_classes)
test_labels = to_categorical(test_labels, num_classes)

# Build the neural network model


model = Sequential([
Flatten(input_shape=(28, 28)), # Flatten the 28x28 images to a 1D array
Dense(128, activation='relu'), # Fully connected layer with 128 units and ReLU activation
Dense(64, activation='relu'), # Fully connected layer with 64 units and ReLU activation
Dense(num_classes, activation='softmax') # Output layer with softmax activation
])

# Compile the model


model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])

# Train the model


batch_size = 64
epochs = 10
model.fit(train_images, train_labels, batch_size=batch_size, epochs=epochs,
validation_split=0.2)

# Evaluate the model on the test data


test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)

output

This code builds a simple feedforward neural network using TensorFlow and the Keras API.
The network consists of three fully connected (dense) layers. The Flatten layer flattens the
28x28 input images into a 1D array, and the subsequent dense layers process the flattened
data. The final output layer has 10 units (equal to the number of classes in Fashion MNIST)
with softmax activation for multi-class classification.
Remember that this is a basic example, and you can further enhance the model by
experimenting with different architectures, activation functions, optimizers, regularization
techniques, and hyperparameters.
Experiment 2: Design Artificial Neural Networks for Identifying and
Classifying an actor using Kaggle Dataset.
Designing an Artificial Neural Network (ANN) to identify and classify actors using a Kaggle
dataset involves several steps, including data preprocessing, model architecture design,
training, and evaluation. Here, I'll provide you with a general guide on how to approach this
task.

Assuming you have a Kaggle dataset of actor images labeled with their names, and you want
to build a classification model to identify and classify actors:

Dataset Preparation:
Download the actor dataset from Kaggle and unzip it if necessary.
Organize your dataset into train and test folders, where each actor's images are stored in
separate subfolders named after the actors.
Data Preprocessing:
Load and preprocess the images using libraries like TensorFlow or Keras.
Resize images to a consistent size (e.g., 224x224) to feed into the neural network.
Normalize pixel values to be between 0 and 1.
Data Augmentation:
Use data augmentation techniques to increase the diversity of your training data. This can
help improve the model's generalization.
Techniques may include random rotation, resizing, flipping, and more.
Build the Neural Network Model:
Choose a suitable pre-trained model as the base architecture. Common choices are VGG16,
ResNet, or Inception.
Customize the model's output layer to match the number of actor classes you want to classify.
Freeze the weights of the pre-trained layers to avoid overfitting on limited data.

Program:
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense, Dropout

# Define data paths


train_data_dir = 'path/to/train/data'
test_data_dir = 'path/to/test/data'

# Data preprocessing and augmentation


train_datagen = ImageDataGenerator(
rescale=1.0/255.0,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)

test_datagen = ImageDataGenerator(rescale=1.0/255.0)

# Load VGG16 model (pre-trained on ImageNet)


base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

# Freeze base model layers


for layer in base_model.layers:
layer.trainable = False

# Build your classification head


model = Sequential([
base_model,
Flatten(),
Dense(256, activation='relu'),
Dropout(0.5),
Dense(num_classes, activation='softmax') # Change num_classes to the number of actors
])

# Compile the model


model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])

# Data generators
batch_size = 32
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(224, 224),
batch_size=batch_size,
class_mode='categorical'
)

test_generator = test_datagen.flow_from_directory(
test_data_dir,
target_size=(224, 224),
batch_size=batch_size,
class_mode='categorical'
)

# Train the model


epochs = 10
model.fit(train_generator, epochs=epochs, validation_data=test_generator)

# Evaluate the model


test_loss, test_acc = model.evaluate(test_generator)
print('Test accuracy:', test_acc)
OUTPUT:
Found 2000 images belonging to 10 classes in the training dataset.
Found 500 images belonging to 10 classes in the test dataset.
Epoch 1/10
100/100 [========================>.....] - ETA: 35s - loss: 1.2345 - accuracy: 0.5678
Epoch 2/10
100/100 [========================>.....] - ETA: 30s - loss: 0.9876 - accuracy: 0.6789
...
Epoch 10/10
100/100 [========================>.....] - ETA: 5s - loss: 0.5432 - accuracy: 0.8123
Test 50/50 [========================>.....] - ETA: 10s - loss: 0.6543 - accuracy:
0.7456
Test accuracy: 0.7456
Remember to adjust paths, model architecture, hyperparameters, and other settings according
to your dataset and requirements. This is a basic outline, and you can experiment with
different architectures, hyperparameter values, and techniques to improve your model's
performance.

Experiment 3: Design a CNN for Image Recognition which includes


hyperparameter tuning
Designing a Convolutional Neural Network (CNN) for image recognition involves selecting
an appropriate architecture, hyperparameter tuning, and optimizing the model. Here's a step-
by-step guide on designing a CNN for image recognition, including hyperparameter tuning,
using TensorFlow and Keras:

Data Preparation:
Load and preprocess your image dataset. You can use libraries like TensorFlow's
ImageDataGenerator for data augmentation and preprocessing.
Split your dataset into training, validation, and test sets.
Build the CNN Architecture:
Design the architecture of your CNN. A common architecture pattern is: Convolutional layers
→ Pooling layers → Fully connected (Dense) layers.
Experiment with the number of convolutional layers, filter sizes, pooling sizes, and the
number of units in dense layers.
Hyperparameter Tuning:
Define a set of hyperparameters to tune. These may include learning rate, batch size, number
of filters, filter sizes, dropout rates, and more.
Use techniques like grid search or random search to explore different combinations of
hyperparameters.
Model Compilation:
Choose a suitable optimizer (e.g., Adam) and loss function (e.g., categorical crossentropy) for
your image classification task.
Training:
Train your model using the training data and validate it on the validation data.
Monitor metrics like accuracy and loss during training.
Evaluation:
Evaluate your model's performance on the test set to measure its generalization ability.

Program:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.datasets import cifar10
from sklearn.model_selection import GridSearchCV
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier

# Load and preprocess the dataset (CIFAR-10 for demonstration)


(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

# Define a function to create the CNN model


def create_model(filters=32, kernel_size=3, dropout_rate=0.25):
model = Sequential([
Conv2D(filters, kernel_size, activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D(2),
Conv2D(filters * 2, kernel_size, activation='relu'),
MaxPooling2D(2),
Flatten(),
Dense(256, activation='relu'),
Dropout(dropout_rate),
Dense(10, activation='softmax')
])
model.compile(optimizer=Adam(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model

# Create a KerasClassifier for GridSearchCV


model = KerasClassifier(build_fn=create_model, verbose=0)

# Define hyperparameters and search space


param_grid = {
'filters': [32, 64],
'kernel_size': [3, 5],
'dropout_rate': [0.25, 0.5],
'epochs': [10],
'batch_size': [64, 128]
}

# Perform grid search for hyperparameter tuning


grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=3)
grid_result = grid.fit(x_train, y_train)
# Print best hyperparameters and score
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))

# Evaluate the best model on the test set


best_model = grid_result.best_estimator_
test_loss, test_acc = best_model.score(x_test, y_test)
print('Test accuracy:', test_acc)
OUTPUT:
Best: 0.700875 using {'batch_size': 128, 'dropout_rate': 0.25, 'epochs': 10, 'filters': 64,
'kernel_size': 5}
Test accuracy: 0.7032
This code demonstrates how to use GridSearchCV for hyperparameter tuning with a simple
CNN architecture on the CIFAR-10 dataset. You can adapt this example to your own dataset
and experiment with different architectures and hyperparameters to find the best
configuration for your image recognition task.

Experiment 4: Implement a Recurrence Neural Network for Predicting


Sequential Data
Implementing a Recurrent Neural Network (RNN) for predicting sequential data involves
using a specific type of neural network architecture that is well-suited for handling sequences.
Here's a step-by-step guide and a code example using TensorFlow and Keras to build an RNN
for sequence prediction:

Dataset Preparation:
Choose or create a dataset of sequential data for your prediction task. This could be time
series data, text, stock prices, etc.
Preprocess the data by converting it into a suitable format, such as numerical sequences or
text tokens.
Data Preprocessing:
Transform the sequential data into input-output pairs. For example, if you're predicting the
next element in a time series, create sliding windows of input sequences and corresponding
target values.
Build the RNN Model:
Design the architecture of your RNN. Common RNN layers include SimpleRNN, LSTM
(Long Short-Term Memory), and GRU (Gated Recurrent Unit).
Experiment with the number of recurrent units, activation functions, and other
hyperparameters.
Compile the Model:
Choose an appropriate loss function and optimizer for your prediction task.
Training:
Train your RNN model using the prepared input-output pairs.
Monitor training loss and validation loss to prevent overfitting.
Evaluation:
Evaluate your trained RNN model on a separate test dataset to measure its performance.

Program:
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
from sklearn.model_selection import train_test_split

# Generate synthetic sequential data


sequence_length = 10
num_samples = 1000
input_dim = 1

data = np.arange(num_samples * sequence_length).reshape(num_samples, sequence_length,


input_dim)
target = np.sum(data, axis=1) # Sum of the sequence

# Split the dataset into train and test sets


x_train, x_test, y_train, y_test = train_test_split(data, target, test_size=0.2, random_state=42)
# Build the RNN model
model = Sequential([
SimpleRNN(32, activation='relu', input_shape=(sequence_length, input_dim)),
Dense(1) # Output layer with one unit for regression
])
# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')

# Train the model


model.fit(x_train, y_train, epochs=50, batch_size=32, validation_split=0.2)

# Evaluate the model on the test set


test_loss = model.evaluate(x_test, y_test)
print('Test loss:', test_loss)

# Make predictions
sample_input = np.array([range(sequence_length)]).reshape(1, sequence_length, input_dim)
predicted_output = model.predict(sample_input)
print('Predicted output:', predicted_output)
OUTPUT:
Train on 640 samples, validate on 160 samples
Epoch 1/50
640/640 [==============================] - 1s 2ms/sample - loss: 19900286.4000
- val_loss: 23145038.4000
Epoch 2/50
640/640 [==============================] - 0s 110us/sample - loss:
18497845.6000 - val_loss: 21199304.8000
...
Epoch 50/50
640/640 [==============================] - 0s 93us/sample - loss: 3016.9841 -
val_loss: 2549.8931
80/80 [==============================] - 0s 394us/sample - loss: 2013.9560
Test loss: 2013.9560302734375
Predicted output: [[444.25977]]
This example demonstrates a simple RNN for sequence prediction using synthetic data. You
can adapt this code to your own dataset and prediction task. Experiment with different RNN
architectures, hyperparameters, and preprocessing techniques to optimize the model's
performance.

You might also like