Deep Learning Exp
Deep Learning Exp
regression using:
A.) Single variable
B.) Multiple variables Program
OUTPUT –
OUTPUT –
Input: “Hello, how are you today”
Output: Voice saying Hello
Program:
A) CNN
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Load and preprocess the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Reshape the input data to 4D tensor (batch_size, height, width, channels)
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
# Define the CNN model
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
Flatten(),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=5, batch_size=64, validation_data=(x_test, y_test))
# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test)
b) RNN
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense
# Load and preprocess the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
# Define the RNN model
model = Sequential([
SimpleRNN(128, input_shape=(x_train.shape[1:]), activation='relu', return_sequences=True),
SimpleRNN(128, activation='relu'),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=5, batch_size=64, validation_data=(x_test, y_test))
# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test)
DEEP LEARNING & ITS APPLICATIONS LAB
print(f'Test accuracy: {test_acc}')
Program:
A) LSTM
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Embedding, LSTM, Dropout,
TimeDistributed, concatenate
from tensorflow.keras.applications import InceptionV3
from tensorflow.keras.applications.inception_v3 import preprocess_input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing import image as keras_image
import os
# Placeholder function for loading data, replace with your actual data loading code
def load_your_data_function():
image_dir = 'D:\LSTM\images' # Update with the path to your image directory
captions_file = 'D:\LSTM\captions.txt' # Update with the path to your captions file
with open(captions_file, 'r') as file:
captions = file.readlines()
captions = [caption.strip() for caption in captions]
image_paths = [os.path.join(image_dir, filename) for filename in os.listdir(image_dir)]
return image_paths, captions
image_paths, captions = load_your_data_function()
tokenizer = Tokenizer()
tokenizer.fit_on_texts(captions)
vocab_size = len(tokenizer.word_index) + 1
Program:
A) Auto encoder using MNIST handwritten digits
import matplotlib.pyplot as plt
import numpy as np
import pandas as np
import tensorflow as tf
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers, losses
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Model
(x_train, _), (x_test, _) = mnist.load_data()
x_train, x_val = x_train[:-10000], x_train[-10000:]
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255
x_val = x_val.astype('float32')/255
print(x_train.shape)
print(x_test.shape)
print(x_val.shape)
# n = 10
# plt.figure(figsize=(20,4))
# for i in range(n):
# # display original
# ax = plt.subplot(2,n,i+1)
# plt.imshow(x_test[i])
# plt.title("original")
# plt.gray()
# ax.get_xaxis().set_visible(False)
# ax.get_yaxis().set_visible(False)
latent_dim = 64