Professional Documents
Culture Documents
NNDL Lab Record
NNDL Lab Record
BACHELOR OF ENGINEERING
2023 - 2024
SIXTH SEMESTER
Name :
University Reg.No :
Semester :
Branch :
Year :
AIM:
ALGORITHM:
Step1:Import the necessary libraries.
Step2: Load the mnist dataset.
Step3:.Preprocess the data.
Step4:Define the model.
Step5:Hyperparameters to perform.
Step6:Perform grid search for hyperparameter tuning
PROGRAM:
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.add(layers.Flatten())
model.add(layers.Dropout(dropout_rate))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer,
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
# Hyperparameters to tune
learning_rates = [0.001, 0.01, 0.1]
dropout_rates = [0.25, 0.5]
# Print results
print(f"Learning Rate: {lr}, Dropout Rate: {dr}")
print(f"Train Accuracy: {history.history['accuracy'][-1]}, Test Accuracy:
{history.history['val_accuracy'][-1]}")
print("-" * 50)
RESULT:
Thus the implementation of deep learning model by fine tuning hyperparameters was excecuted
successfully.
Ex No:7
IMPLEMENT THE TRANSFER LEARNING CONCEPT IN IMAGE
Date: CLASSIFICATION
AIM:
To write implementation of transfer learning concept in image classification.
ALGORITHM:
Step1:Import the necessary libraries.
Step2: Load CIFAR-10 dataset.
Step3:.Preprocess the data.
Step4:One-hot encode the labels.
Step5:Load pretrain model.
Step6:Freeze the convolutional base.
Step7:Build the new model.
Step8:Train and evaluate the model.
PROGRAM:
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.utils import to_categorical
# Load CIFAR-10 dataset
(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()
# Preprocess the data
train_images = preprocess_input(train_images)
test_images = preprocess_input(test_images)
# One-hot encode the labels
train_labels = to_categorical(train_labels, num_classes=10)
test_labels = to_categorical(test_labels, num_classes=10)
# Load pre-trained MobileNetV2 model without the top (classification) layers
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(32, 32, 3))
# Freeze the convolutional base
base_model.trainable = False
# Build a new model on top of the pre-trained base
model = models.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dense(128, activation='relu'),
layers.Dropout(0.5),
layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Train the model
history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))
# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')
# Fine-tune the model (optional)
# model.trainable = True
# model.compile(optimizer=tf.keras.optimizers.Adam(1e-5),
# loss='categorical_crossentropy',
# metrics=['accuracy'])
# history_fine = model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels
))
RESULT:
Thus the implementation of transfer learning concepts in image classification.
8
Step1:Set hyperparameters.
Step2: Load the dataset.
Step3:.Pad sequences to make them same length.
Step4:Define the model
Step5:Compile the model.
Step6: Train and evaluate the model.
9(a)
Step1:Set hyperparameters.
Step2: Define the autoencoder model.
Step3:.Compile the autoencoder.
Step4:Train the autoencoder using random data.
Step5:Test the autoencoder on new data.
Step6: Train and evaluate the model.
9(b)
Step1:Set hyperparameters.
Step2: Define the autoencoder model.
Step3:.Compile the autoencoder.
Step4:Train the autoencoder using random data.
Step5:Test the autoencoder on new data.
Step6: Train and evaluate the model.
10