DL LAB MANUAL Mugesh
DL LAB MANUAL Mugesh
NO: 1
ACCURACY OF VARIOUS ACTIVATION FUNCTIONS
DATE:
AIM :
To test the accuracies of various activation functions.
ALGORITHM :
PROGRAM :
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Dropout
import numpy as np
import matplotlib.pyplot as plt
# Load and preprocess data
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Define the model with changed activation function
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='tanh'), # Changed from 'relu' to 'tanh'
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
OUTPUT :
RESULT :
Thus the accuracies of various activation functions have been studied successfully.
EXP.NO: 2
MLP NEUTRAL NETWORK TO SOLVE THE XOR PROBLEM
DATE:
AIM :
To solve the given XOR problem using Multilayer Perceptron Neural Network.
ALGORITHM :
PROGRAM :
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
X = np.array([[0, 0], [0, 0], [1, 1], [1, 0]])
y = np.array([[1], [1], [1], [1]])
model = Sequential()
model.add(Dense(8, input_dim=2, activation='relu')) # Increased neurons from 6 to 8
model.add(Dense(1, activation='tanh')) # Changed activation from sigmoid to tanh
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=1000, verbose=0)
loss, accuracy = model.evaluate(X, y)
print(f'Accuracy: {accuracy * 100:.2f}%')
predictions = model.predict(X)
predictions_int = [round(pred[0]) for pred in predictions]
print('Predictions:', predictions_int)
OUTPUT :
RESULT :
AIM :
To build a Artificial Neural Network to recognize characters and digits from images.
ALGORITHM :
Import Libraries: Use Keras and NumPy.
Load Data: Load MNIST dataset (28x28 images).
Preprocess Data: Normalize inputs and one-hot encode outputs.
Build Model:
1. Add Flatten, Dense (150 neurons, tanh), and Dense (100 neurons,
ReLu) layers.
Compile: Use Adam optimizer and categorical crossentropy loss.
Train Model: Train for 10 epochs with validation.
Evaluate Model: Test accuracy on the test set.
Predict: Predict classes for test data and display results.
PROGRAM :
# Import necessary libraries
import numpy as np
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt
from google.colab import files
from PIL import Image, ImageOps
# Make a prediction
prediction = model.predict(img_array)
predicted_label = np.argmax(prediction)
print(f"Predicted Digit: {predicted_label}")
OUTPUT :
RESULT :
Thus, an Artificial Neural Network to recognize characters and digits from images.
AIM :
ALGORITHM :
Import Libraries – Load NumPy, Matplotlib, OpenCV, and Keras modules.
Load Data – Load the Fashion-MNIST dataset (grayscale 28×28 images).
Preprocess Data
1 Resize images to 32×32.
2 Convert grayscale to RGB (3 channels).
3 Normalize pixel values to [0,1].
Build Autoencoder Model
Encoder: Apply Conv2D, MaxPooling2D layers for feature extraction.
Decoder: Use Conv2D, UpSampling2D layers to reconstruct images.
Compile & Train Model
Use Adam optimizer and Binary Crossentropy loss.
Train for 5 epochs with batch size 256.
Evaluate & Visualize Results
Predict reconstructed images on test data.
Plot original vs. reconstructed images for comparison.
PROGRAM :
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Model
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.datasets import fashion_mnist
import cv2
# Load Fashion-MNIST dataset
(X_train, _), (X_test, _) = fashion_mnist.load_data()
# Resize images from (28x28) to (32x32) and convert grayscale to 3-channel RGB-like
format
X_train = np.array([cv2.cvtColor(cv2.resize(img, (32, 32)), cv2.COLOR_GRAY2RGB) for
img in X_train])
X_test = np.array([cv2.cvtColor(cv2.resize(img, (32, 32)), cv2.COLOR_GRAY2RGB) for
img in X_test])
# Normalize pixel values
X_train = X_train.astype('float32') / 255.0
X_test = X_test.astype('float32') / 255.0
# Encoder
input_img = Input(shape=(32, 32, 3))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(16, (3, 3), activation='tanh', padding='same')(x) # Changed activation
encoded = MaxPooling2D((2, 2), padding='same')(x)
# Decoder
x = Conv2D(16, (3, 3), activation='tanh', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x) # Output to 3
channels
# Reconstructed images
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i])
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
OUTPUT:
RESULT :
Thus, a program using autoencoders to analyze images for image reconstruction
tasks was build successfully.
EXP.NO: 5
CONVOLUTIONAL NEURAL NETWORK (CNN) FOR SPEECH
DATE: RECOGNITION
AIM :
To build Convolutional Neural Network for Speech Recognition task.
ALGORITHM :
Import Libraries: Load TensorFlow, NumPy, and required Keras modules.
Generate Data: Create random dummy data with shape (num_samples,
input_length, 1).
Preprocess Data: One-hot encode labels and split data into training & testing sets.
Build CNN Model:
Add Conv1D layers with tanh and LeakyReLU activation.
Use MaxPooling1D, BatchNormalization, Flatten, Dense, and Dropout layers.
Compile Model: Use Adam optimizer and categorical cross-entropy loss.
Train Model: Train for 7 epochs with a batch size of 32.
Evaluate Model: Compute accuracy on test data.
Make Predictions: Predict and display first 10 classes.
PROGRAM :
import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import LeakyReLU
num_samples = 1000
input_length = 16000
num_classes = 10
X, y = generate_dummy_data(num_samples, input_length, num_classes)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
input_shape = (input_length, 1)
model = build_cnn_model(input_shape=input_shape, num_classes=num_classes)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
RESULT :