NNDL Lab Manual
NNDL Lab Manual
LAB RECORD
Bonafide Certificate
INDEX
S.N PROGRAM P.NO MARK SIGN
O
1 Implement simple matrix operations in
tensorflow
2 Implement a perceptron in tensorflow/keras
environment
3 Implement a feedforward network in
tensorflow/keras
4 Implement a regression model in keras
Output
print(test_acc)
0.7163000106811523
RESULT
Thus, a simple CNN has achieved a test accuracy of over 70% using CNN model image
classification.
# Necessary imports
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
import numpy as np
from sklearn.datasets import make_classification
X, y = make_classification(
n_samples=1000, n_features=20, n_informative=10, n_classes=2, random_state=42)
Output:
Tuned Logistic Regression Parameters: {'C': 0.006105402296585327}
Best score is 0.853
RESULT
The logistic regression parameter is fine tuned and its best score is identified.
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i][0]])
plt.show()
# Use pretrained model
base_model = MobileNetV2(input_shape=(32, 32, 3), include_top=False,
weights='imagenet')
base_model.trainable = False # Freeze the base model
# Upsample to 14x14
layers.Conv2DTranspose(128, kernel_size=4, strides=2, padding='same',
activation='relu'),
layers.BatchNormalization(),
# Upsample to 28x28
layers.Conv2DTranspose(128, kernel_size=4, strides=2, padding='same',
activation='relu'),
layers.BatchNormalization(),
# Output layer with the shape of the target image, 1 channel for grayscale
layers.Conv2D(1, kernel_size=7, activation='sigmoid', padding='same')
])
return model
def build_discriminator_cnn():
model = models.Sequential([
# Input layer with the shape of the target image
layers.Conv2D(64, kernel_size=3, strides=2, input_shape=(28, 28, 1), padding='same',
activation='relu'),
# Downsample to 14x14
layers.Conv2D(128, kernel_size=3, strides=2, padding='same', activation='relu'),
layers.BatchNormalization(),
# Set the Discriminator's weights to non-trainable (important when we train the combined
GAN model)
discriminator_cnn.trainable = False
epochs = 10000
batch_size = 64
#################################
# 2. Train the Generator (via GAN)
#################################
# Train the generator (note that we want the Discriminator to mistake images as real)
noise = np.random.normal(0, 1, (batch_size, 100))
valid_labels = np.ones((batch_size, 1))
g_loss = gan_cnn.train_on_batch(noise, valid_labels)
RESULT
Image generation is executed using Generative Adversarial Model (GAN).