[go: up one dir, main page]

0% found this document useful (0 votes)
27 views67 pages

NNDL 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views67 pages

NNDL 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Ex No: 1 Implement simple vector addition in TensorFlow

Date:

PROGRAM :

#1.Implement simple vector addition in tensorflow

# importing packages

import tensorflow as tf

# creating a scalar

scalar = tf.constant(7)

scalar
OUTPUT :

tf.Tensor(

[[1 2]

[3 4]], shape=(2, 2), dtype=int32)

the number of dimensions of a matrix is : 2

tf.Tensor(

[[ 3 6]

[ 9 12]], shape=(2, 2), dtype=int32)

tf.Tensor(

[[1 2]

[3 4]], shape=(2, 2), dtype=int32)

tf.Tensor(

[[ 2 8]

[18 32]], shape=(2, 2), dtype=int32)

tf.Tensor(

[[2. 2.]

[2. 2.]], shape=(2, 2), dtype=float64)

tf.Tensor(

[[1 2]

[3 4]], shape=(2, 2), dtype=int32)

tf.Tensor(

[[1 3]

[2 4]], shape=(2, 2), dtype=int32)

dot product of matrices is : tf.Tensor(

[[ 7 10]

[15 22]], shape=(2, 2), dtype=int32)


scalar.ndim

# create a vector

vector = tf.constant([10, 10])

# checking the dimensions of vector

vector.ndim

# creating a matrix

matrix = tf.constant([[1, 2], [3, 4]])

print(matrix)

print('the number of dimensions of a matrix is : ' +str(matrix.ndim))

# creating two tensors

matrix = tf.constant([[1, 2], [3, 4]])

matrix1 = tf.constant([[2, 4], [6, 8]])

# addition of two matrices

print(matrix+matrix1)

# subtraction of two matrices

print(matrix1 - matrix)

# multiplication of two matrices

print(matrix1 * matrix)

# division of two matrices

print(matrix1 / matrix)

# creating a matrix

matrix = tf.constant([[1, 2], [3, 4]])

print(matrix)

# transpose of the matrix

print(tf.transpose(matrix))

# dot product of matrices

print('dot product of matrices is : ' +str(tf.tensordot(matrix, matrix, axes=1)))

RESULT:
OUTPUT:

Epoch 1/10

8/8 [==============================] - 1s 45ms/step - loss: 10.3458 - val_loss: 12.2980

Epoch 2/10

8/8 [==============================] - 0s 8ms/step - loss: 10.0908 - val_loss: 11.9863

Epoch 3/10

8/8 [==============================] - 0s 8ms/step - loss: 9.8364 - val_loss: 11.6757

Epoch 4/10

8/8 [==============================] - 0s 10ms/step - loss: 9.5745 - val_loss: 11.3687

Epoch 5/10

8/8 [==============================] - 0s 10ms/step - loss: 9.3178 - val_loss: 11.0608

Epoch 6/10

8/8 [==============================] - 0s 9ms/step - loss: 9.0594 - val_loss: 10.7527

Epoch 7/10

8/8 [==============================] - 0s 9ms/step - loss: 8.8187 - val_loss: 10.4355

Epoch 8/10

8/8 [==============================] - 0s 13ms/step - loss: 8.5541 - val_loss: 10.1272

Epoch 9/10

8/8 [==============================] - 0s 11ms/step - loss: 8.2961 - val_loss: 9.8209

Epoch 10/10

8/8 [==============================] - 0s 12ms/step - loss: 8.0424 - val_loss: 9.5135

1/1 [==============================] - 0s 142ms/step

Mean Squared Error: 8.532234615916241


Ex No: 2 Implement a regression model in Keras
Date:

PROGRAM :

import numpy as np

from keras.models import Sequential

from keras.layers import Dense

from sklearn.model_selection import train_test_split

from sklearn.metrics import mean_squared_error

import matplotlib.pyplot as plt


np.random.seed(42)

X = np.random.rand(100, 1)

y = 3 * X + 2 + 0.1 * np.random.randn(100, 1)

# Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Build the regression model

model = Sequential()

model.add(Dense(units=10, input_dim=1, activation='relu')) # Hidden layer with ReLU activation

model.add(Dense(units=1)) # Output layer

# Compile the model

model.compile(optimizer='adam', loss='mean_squared_error')

# Train the model

model.fit(X_train, y_train, epochs=10, batch_size=8, verbose=1, validation_split=0.2)

# Evaluate the model on the test set

y_pred = model.predict(X_test)

# Calculate and print the mean squared error

mse = mean_squared_error(y_test, y_pred)

print("Mean Squared Error:", mse)

# Plot the results

plt.scatter(X_test, y_test, label='True data')

plt.scatter(X_test, y_pred, label='Predicted data')

plt.xlabel('X')

plt.ylabel('y')

plt.legend()

plt.show()

RESULT :
OUTPUT :

1/1 [==============================] - 0s 116ms/step - loss: 0.0406 - accuracy: 1.0000

Loss: 0.040563274174928665

Accuracy: 1.0

1/1 [==============================] - 0s 53ms/step

Predictions:

[[0.3149353 ]

[0.81543726]

[0.8309563 ]

[0.97927153]]
Ex No: 3 Implement a perceptron in TensorFlow/Keras
Date: Environment

PROGRAM :

import tensorflow as tf

from tensorflow import keras

from tensorflow.keras.layers import Dense

from tensorflow.keras.optimizers import SGD

import numpy as np

# Generate some sample data for a logical OR operation


X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) # Input features

y = np.array([0, 1, 1, 1]) # Output labels (OR gate)

# Define a simple perceptron model

model = keras.Sequential([

Dense(units=1, input_dim=2, activation='sigmoid') # 2 input features, 1 output unit, sigmoid activation

])

# Compile the model

model.compile(optimizer=SGD(learning_rate=0.1), loss='mean_squared_error', metrics=['accuracy'])

# Train the model

model.fit(X, y, epochs=1000, verbose=0) # You can adjust the number of epochs

# Evaluate the model

loss, accuracy = model.evaluate(X, y)

print("Loss:", loss)

print("Accuracy:", accuracy)

# Make predictions

predictions = model.predict(X)

print("Predictions:")

print(predictions)

RESULT :
OUTPUT:

1/1 [==============================] - 0s 193ms/step - loss: 0.5935 - accuracy: 0.7500

Loss: 0.5935246348381042

Accuracy: 0.75

1/1 [==============================] - 0s 50ms/step

Predictions:

[[0.5512892 ]

[0.55111045]

[0.55125475]

[0.3170568 ]]
Ex No: 4 Implement a Feed-Forward Network in
Date: TensorFlow/Keras

PROGRAM :

#exp4 Implement a feed forward network in tensorflow/keras

import tensorflow as tf

from tensorflow import keras

from tensorflow.keras.layers import Dense

import numpy as np
# Generate some sample data

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) # Input features

y = np.array([0, 1, 1, 0]) # Output labels (XOR gate)

# Define a feedforward neural network model

model = keras.Sequential([

Dense(units=4, input_dim=2, activation='relu'), # 2 input features, 4 hidden units with ReLU activation

Dense(units=1, activation='sigmoid') # 1 output unit with sigmoid activation

])

# Compile the model

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

# Train the model

model.fit(X, y, epochs=1000, verbose=0) # You can adjust the number of epochs

# Evaluate the model

loss, accuracy = model.evaluate(X, y)

print("Loss:", loss)

print("Accuracy:", accuracy)

# Make predictions

predictions = model.predict(X)

print("Predictions:")

print(predictions)

RESULT :
OUTPUT:

Epoch 1/10

1563/1563 [==============================] - 44s 27ms/step - loss: 1.7326 - accuracy: 0.3552 -


val_loss: 1.4943 - val_accuracy: 0.4481

Epoch 2/10

1563/1563 [==============================] - 40s 26ms/step - loss: 1.4067 - accuracy: 0.4913 -


val_loss: 1.2075 - val_accuracy: 0.5696

Epoch 3/10

1563/1563 [==============================] - 40s 25ms/step - loss: 1.2616 - accuracy: 0.5492 -


val_loss: 1.0966 - val_accuracy: 0.6044

Epoch 4/10

1563/1563 [==============================] - 40s 26ms/step - loss: 1.1674 - accuracy: 0.5863 -


val_loss: 1.0773 - val_accuracy: 0.6219

Epoch 5/10

1563/1563 [==============================] - 40s 26ms/step - loss: 1.0959 - accuracy: 0.6165 -


val_loss: 0.9905 - val_accuracy: 0.6524

Epoch 6/10

1563/1563 [==============================] - 41s 26ms/step - loss: 1.0386 - accuracy: 0.6353 -


val_loss: 0.9930 - val_accuracy: 0.6534

Epoch 7/10

1563/1563 [==============================] - 40s 26ms/step - loss: 0.9913 - accuracy: 0.6545 -


val_loss: 0.9512 - val_accuracy: 0.6715

Epoch 8/10

1563/1563 [==============================] - 40s 26ms/step - loss: 0.9412 - accuracy: 0.6726 -


val_loss: 0.9317 - val_accuracy: 0.6778

Epoch 9/10

1563/1563 [==============================] - 43s 27ms/step - loss: 0.9051 - accuracy: 0.6877 -


val_loss: 0.8986 - val_accuracy: 0.6899

Epoch 10/10

1563/1563 [==============================] - 40s 26ms/step - loss: 0.8721 - accuracy: 0.6975 -


val_loss: 0.8633 - val_accuracy: 0.7049

1/1 [==============================] - 0s 72ms/step

Predicted Class Label: cat


Ex No: 5 Implement an Image Classifier using CNN in
Date: TensorFlow/Keras

PROGRAM :

#exp5

import tensorflow as tf

from tensorflow import keras

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

from tensorflow.keras.datasets import cifar10


from tensorflow.keras.utils import to_categorical

import numpy as np

from PIL import Image

# Load and preprocess the CIFAR-10 dataset

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

x_train, x_test = x_train / 255.0, x_test / 255.0 # Normalize pixel values to the range [0, 1]

y_train = to_categorical(y_train, 10) # One-hot encode the labels

y_test = to_categorical(y_test, 10)

# Define the CNN model

model = keras.Sequential([

Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),

MaxPooling2D((2, 2)),

Conv2D(64, (3, 3), activation='relu'),

MaxPooling2D((2, 2)),

Conv2D(64, (3, 3), activation='relu'),

Flatten(),

Dense(64, activation='relu'),

Dropout(0.5),

Dense(10, activation='softmax')

])

# Compile the model

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

# Train the model

model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))

# Load and preprocess the custom image


custom_image_path = 'test.jpg'

custom_image = Image.open(custom_image_path)

custom_image = custom_image.resize((32, 32)) # Resize the image to match the input shape of the
model

custom_image = np.array(custom_image) / 255.0 # Normalize pixel values to [0, 1]

custom_image = np.expand_dims(custom_image, axis=0) # Add batch dimension

# Use the trained model to predict the class of the custom image

predicted_probs = model.predict(custom_image)

predicted_class = np.argmax(predicted_probs)

# Define class labels for CIFAR-10 dataset

class_labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

# Print the predicted class label

print("Predicted Class Label:", class_labels[predicted_class])

RESULT:
OUTPUT:

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz

11490434/11490434 [==============================] - 0s 0us/step

Epoch 1/10

1875/1875 [==============================] - 11s 5ms/step - loss: 0.4705 - accuracy: 0.8771 -


val_loss: 0.3108 - val_accuracy: 0.9157

Epoch 2/10

1875/1875 [==============================] - 12s 6ms/step - loss: 0.3040 - accuracy: 0.9150 -


val_loss: 0.2829 - val_accuracy: 0.9223

Epoch 3/10

1875/1875 [==============================] - 9s 5ms/step - loss: 0.2836 - accuracy: 0.9205 -


val_loss: 0.2712 - val_accuracy: 0.9246

Epoch 4/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.2733 - accuracy: 0.9242 -


val_loss: 0.2700 - val_accuracy: 0.9249

Epoch 5/10

1875/1875 [==============================] - 7s 4ms/step - loss: 0.2665 - accuracy: 0.9257 -


val_loss: 0.2681 - val_accuracy: 0.9255

Epoch 6/10

1875/1875 [==============================] - 4s 2ms/step - loss: 0.2618 - accuracy: 0.9272 -


val_loss: 0.2672 - val_accuracy: 0.9258

Epoch 7/10

1875/1875 [==============================] - 10s 5ms/step - loss: 0.2585 - accuracy: 0.9281 -


val_loss: 0.2655 - val_accuracy: 0.9263

Epoch 8/10

1875/1875 [==============================] - 8s 4ms/step - loss: 0.2552 - accuracy: 0.9295 -


val_loss: 0.2713 - val_accuracy: 0.9240

Epoch 9/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.2533 - accuracy: 0.9299 -


val_loss: 0.2656 - val_accuracy: 0.9266

Epoch 10/10

1875/1875 [==============================] - 4s 2ms/step - loss: 0.2509 - accuracy: 0.9306 -


val_loss: 0.2668 - val_accuracy: 0.9272

Epoch 1/10
Ex No: 6 Improve the Deep learning model by fine tuning hyper
Date: parameters

PROGRAM:

#exp6 improve the deep learning model by fine tuning hyper parameters

from tensorflow.keras.datasets import mnist

(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train.shape, y_train.shape, X_test.shape, y_test.shape

# Visualize the first input


1875/1875 [==============================] - 6s 3ms/step - loss: 0.3506 - accuracy: 0.9018 -
val_loss: 0.2010 - val_accuracy: 0.9398

Epoch 2/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.1837 - accuracy: 0.9472 -


val_loss: 0.1681 - val_accuracy: 0.9508

Epoch 3/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.1471 - accuracy: 0.9572 -


val_loss: 0.1391 - val_accuracy: 0.9569

Epoch 4/10

1875/1875 [==============================] - 6s 3ms/step - loss: 0.1242 - accuracy: 0.9634 -


val_loss: 0.1262 - val_accuracy: 0.9607

Epoch 5/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.1089 - accuracy: 0.9678 -


val_loss: 0.1167 - val_accuracy: 0.9632

Epoch 6/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.0968 - accuracy: 0.9707 -


val_loss: 0.1174 - val_accuracy: 0.9631

Epoch 7/10

1875/1875 [==============================] - 5s 3ms/step - loss: 0.0878 - accuracy: 0.9735 -


val_loss: 0.1089 - val_accuracy: 0.9656

Epoch 8/10

1875/1875 [==============================] - 8s 4ms/step - loss: 0.0796 - accuracy: 0.9759 -


val_loss: 0.1125 - val_accuracy: 0.9642

Epoch 9/10

1875/1875 [==============================] - 8s 4ms/step - loss: 0.0736 - accuracy: 0.9779 -


val_loss: 0.1088 - val_accuracy: 0.9658

Epoch 10/10

1875/1875 [==============================] - 6s 3ms/step - loss: 0.0689 - accuracy: 0.9791 -


val_loss: 0.1066 - val_accuracy: 0.9670

Epoch 1/10

1875/1875 [==============================] - 5s 2ms/step - loss: 2.0504 - accuracy: 0.3644 -


val_loss: 1.7476 - val_accuracy: 0.5351

Epoch 2/10

1875/1875 [==============================] - 5s 3ms/step - loss: 1.5124 - accuracy: 0.6248 -


val_loss: 1.2799 - val_accuracy: 0.7109
import matplotlib.pyplot as plt

plt.imshow(X_train[0])

# Create Adam Optimizer

from tensorflow.keras.optimizers import Adam

opt_1 = Adam(learning_rate=0.001)

# Base Model (Model 1)

from tensorflow.keras import layers

from tensorflow.keras.models import Sequential

model_1 = Sequential([layers.Input((28, 28)), layers.Lambda(lambda x: x / 255), layers.Flatten(),


layers.Dense(10, activation='softmax')])

model_1.compile(optimizer=opt_1, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model_1.fit(x=X_train, y=y_train, validation_data=(X_test, y_test), epochs=10)

# Adding number of parameters (Model 2)

opt_2 = Adam(learning_rate=0.001)

model_2 = Sequential([layers.Input((28, 28)), layers.Lambda(lambda x: x / 255), layers.Flatten(),


layers.Dense(32, activation='relu'), layers.Dense(10, activation='softmax')])

model_2.compile(optimizer=opt_2, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model_2.fit(x=X_train, y=y_train, validation_data=(X_test, y_test), epochs=10)

# Increasing the learning rate (Model 3)

opt_3 = Adam(learning_rate=0.00001)

model_3 = Sequential([layers.Input((28, 28)), layers.Lambda(lambda x: x / 255), layers.Flatten(),


layers.Dense(32, activation='relu'), layers.Dense(10, activation='softmax')])

model_3.compile(optimizer=opt_3, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model_3.fit(x=X_train, y=y_train, validation_data=(X_test, y_test), epochs=10)

# Greatly add number of parameters (Model 4)

opt_4 = Adam(learning_rate=0.001)

model_4 = Sequential([layers.Input((28, 28)), layers.Lambda(lambda x: x / 255), layers.Flatten(),


layers.Dense(128, activation='relu'), layers.Dense(128, activation='relu'), layers.Dense(10,
activation='softmax')])

model_4.compile(optimizer=opt_4, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model_4.fit(x=X_train, y=y_train, validation_data=(X_test, y_test), epochs=10)

# Add Regularization / Dropout to reduce overfitting


Epoch 3/10

1875/1875 [==============================] - 10s 6ms/step - loss: 1.1355 - accuracy: 0.7512 -


val_loss: 0.9752 - val_accuracy: 0.7977

Epoch 4/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.8931 - accuracy: 0.8088 -


val_loss: 0.7824 - val_accuracy: 0.8330

Epoch 5/10

1875/1875 [==============================] - 4s 2ms/step - loss: 0.7395 - accuracy: 0.8361 -


val_loss: 0.6605 - val_accuracy: 0.8533

Epoch 6/10

1875/1875 [==============================] - 6s 3ms/step - loss: 0.6392 - accuracy: 0.8532 -


val_loss: 0.5793 - val_accuracy: 0.8679

Epoch 7/10

1875/1875 [==============================] - 4s 2ms/step - loss: 0.5706 - accuracy: 0.8645 -


val_loss: 0.5232 - val_accuracy: 0.8765

Epoch 8/10

1875/1875 [==============================] - 6s 3ms/step - loss: 0.5214 - accuracy: 0.8733 -


val_loss: 0.4817 - val_accuracy: 0.8834

Epoch 9/10

1875/1875 [==============================] - 4s 2ms/step - loss: 0.4845 - accuracy: 0.8795 -


val_loss: 0.4506 - val_accuracy: 0.8895

Epoch 10/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.4559 - accuracy: 0.8844 -


val_loss: 0.4260 - val_accuracy: 0.8943

Epoch 1/10

1875/1875 [==============================] - 10s 5ms/step - loss: 0.2284 - accuracy: 0.9325 -


val_loss: 0.1319 - val_accuracy: 0.9583

Epoch 2/10

1875/1875 [==============================] - 9s 5ms/step - loss: 0.0973 - accuracy: 0.9703 -


val_loss: 0.0863 - val_accuracy: 0.9726

Epoch 3/10

1875/1875 [==============================] - 8s 4ms/step - loss: 0.0669 - accuracy: 0.9794 -


val_loss: 0.0949 - val_accuracy: 0.9712

Epoch 4/10
from tensorflow.keras.regularizers import L2

opt_5 = Adam(learning_rate=0.001)

model_5 = Sequential([layers.Input((28, 28)), layers.Lambda(lambda x: x / 255), layers.Flatten(),


layers.Dense(128, activation='relu', kernel_regularizer=L2(0.001)), layers.Dropout(0.05),
layers.Dense(128, activation='relu', kernel_regularizer=L2(0.001)), layers.Dropout(0.05),
layers.Dense(128, activation='relu', kernel_regularizer=L2(0.001)), layers.Dropout(0.05),
layers.Dense(10, activation='softmax')])

model_5.compile(optimizer=opt_5, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model_5.fit(x=X_train, y=y_train, validation_data=(X_test, y_test), epochs=10)

# Increase number of epochs

model_5.fit(x=X_train, y=y_train, validation_data=(X_test, y_test), epochs=10)


1875/1875 [==============================] - 9s 5ms/step - loss: 0.0525 - accuracy: 0.9833 -
val_loss: 0.0818 - val_accuracy: 0.9758

Epoch 5/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.0404 - accuracy: 0.9867 -


val_loss: 0.0822 - val_accuracy: 0.9758

Epoch 6/10

1875/1875 [==============================] - 10s 5ms/step - loss: 0.0318 - accuracy: 0.9898 -


val_loss: 0.0973 - val_accuracy: 0.9719

Epoch 7/10

1875/1875 [==============================] - 8s 4ms/step - loss: 0.0287 - accuracy: 0.9907 -


val_loss: 0.0806 - val_accuracy: 0.9788

Epoch 8/10

1875/1875 [==============================] - 10s 5ms/step - loss: 0.0242 - accuracy: 0.9919 -


val_loss: 0.0866 - val_accuracy: 0.9787

Epoch 9/10

1875/1875 [==============================] - 9s 5ms/step - loss: 0.0201 - accuracy: 0.9934 -


val_loss: 0.0815 - val_accuracy: 0.9793

Epoch 10/10

1875/1875 [==============================] - 10s 5ms/step - loss: 0.0183 - accuracy: 0.9938 -


val_loss: 0.0950 - val_accuracy: 0.9794

Epoch 1/10

1875/1875 [==============================] - 12s 6ms/step - loss: 0.5228 - accuracy: 0.9194 -


val_loss: 0.3283 - val_accuracy: 0.9527

Epoch 2/10

1875/1875 [==============================] - 15s 8ms/step - loss: 0.3069 - accuracy: 0.9561 -


val_loss: 0.2552 - val_accuracy: 0.9654

Epoch 3/10

1875/1875 [==============================] - 21s 11ms/step - loss: 0.2630 - accuracy: 0.9620 -


val_loss: 0.2355 - val_accuracy: 0.9664

Epoch 4/10

1875/1875 [==============================] - 19s 10ms/step - loss: 0.2440 - accuracy: 0.9639 -


val_loss: 0.2291 - val_accuracy: 0.9686

Epoch 5/10
1875/1875 [==============================] - 12s 6ms/step - loss: 0.2365 - accuracy: 0.9646 -
val_loss: 0.2048 - val_accuracy: 0.9723

Epoch 6/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.2265 - accuracy: 0.9665 -


val_loss: 0.2139 - val_accuracy: 0.9693

Epoch 7/10

1875/1875 [==============================] - 14s 7ms/step - loss: 0.2201 - accuracy: 0.9674 -


val_loss: 0.1980 - val_accuracy: 0.9717

Epoch 8/10

1875/1875 [==============================] - 18s 10ms/step - loss: 0.2165 - accuracy: 0.9671 -


val_loss: 0.2042 - val_accuracy: 0.9705

Epoch 9/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.2114 - accuracy: 0.9684 -


val_loss: 0.2177 - val_accuracy: 0.9659

Epoch 10/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.2115 - accuracy: 0.9675 -


val_loss: 0.2018 - val_accuracy: 0.9707

Epoch 1/10

1875/1875 [==============================] - 13s 7ms/step - loss: 0.2067 - accuracy: 0.9691 -


val_loss: 0.1988 - val_accuracy: 0.9720

Epoch 2/10

1875/1875 [==============================] - 13s 7ms/step - loss: 0.2053 - accuracy: 0.9693 -


val_loss: 0.2004 - val_accuracy: 0.9710

Epoch 3/10

1875/1875 [==============================] - 20s 10ms/step - loss: 0.2033 - accuracy: 0.9688 -


val_loss: 0.1915 - val_accuracy: 0.9731

Epoch 4/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.2019 - accuracy: 0.9695 -


val_loss: 0.2051 - val_accuracy: 0.9671

Epoch 5/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.2007 - accuracy: 0.9684 -


val_loss: 0.1862 - val_accuracy: 0.9750

Epoch 6/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.2005 - accuracy: 0.9690 -


val_loss: 0.2019 - val_accuracy: 0.9698
Epoch 7/10

1875/1875 [==============================] - 11s 6ms/step - loss: 0.1973 - accuracy: 0.9701 -


val_loss: 0.1935 - val_accuracy: 0.9706

Epoch 8/10

1875/1875 [==============================] - 14s 7ms/step - loss: 0.1976 - accuracy: 0.9692 -


val_loss: 0.1969 - val_accuracy: 0.9703

Epoch 9/10

1875/1875 [==============================] - 21s 11ms/step - loss: 0.1952 - accuracy: 0.9702 -


val_loss: 0.1840 - val_accuracy: 0.9755

Epoch 10/10

1875/1875 [==============================] - 12s 6ms/step - loss: 0.1966 - accuracy: 0.9697 -


val_loss: 0.2003 - val_accuracy: 0.9703

<keras.src.callbacks.History at 0x793bb1bce2c0>
RESULT:
OUTPUT:

Model: "sequential_6"

_________________________________________________________________

Layer (type) Output Shape Param #

=================================================================

keras_layer_1 (KerasLayer) (None, 1280) 2257984

dropout_4 (Dropout) (None, 1280) 0


Ex No: 7 Implement a Transfer Learning concept in Image
Date: Classification

PROGRAM:

#exp7

import matplotlib.pylab as plt

import tensorflow as tf

import tensorflow_hub as hub

import os
dense_13 (Dense) (None, 3) 3843

=================================================================

Total params: 2261827 (8.63 MB)

Trainable params: 3843 (15.01 KB)

Non-trainable params: 2257984 (8.61 MB)

_________________________________________________________________

Epoch 1/6

33/33 [==============================] - 54s 1s/step - loss: 1.0230 - acc: 0.5464 - val_loss: 0.5456 -
val_acc: 0.7594

Epoch 2/6

33/33 [==============================] - 46s 1s/step - loss: 0.5851 - acc: 0.7505 - val_loss: 0.4498 -
val_acc: 0.8722

Epoch 3/6

33/33 [==============================] - 43s 1s/step - loss: 0.4569 - acc: 0.8104 - val_loss: 0.3655 -
val_acc: 0.8647

Epoch 4/6

33/33 [==============================] - 49s 1s/step - loss: 0.4036 - acc: 0.8443 - val_loss: 0.3253 -
val_acc: 0.8797

Epoch 5/6

33/33 [==============================] - 49s 1s/step - loss: 0.3395 - acc: 0.8723 - val_loss: 0.2954 -
val_acc: 0.9248

Epoch 6/6

33/33 [==============================] - 48s 1s/step - loss: 0.3261 - acc: 0.8733 - val_loss: 0.2811 -
val_acc: 0.9173

4/4 [==============================] - 5s 1s/step - loss: 0.3093 - acc: 0.8750

1/1 [==============================] - 1s 797ms/step

[[0.10855925 0.54099596 0.35044467]]


import numpy as np

import tensorflow_datasets as tfds

import warnings

warnings.filterwarnings('ignore')

datasets, info = tfds.load(name='beans', with_info=True, as_supervised=True,


split=['train','test','validation'])

info

train, info_train = tfds.load(name='beans', with_info=True, split='test')

tfds.show_examples(train,info_train)

def scale(image, label):

image = tf.cast(image, tf.float32)

image /= 255.0

return tf.image.resize(image,[224,224]), tf.one_hot(label, 3)

def get_dataset(batch_size=32):

train_dataset_scaled = datasets[0].map(scale).shuffle(1000).batch(batch_size)

test_dataset_scaled = datasets[1].map(scale).batch(batch_size)

val_dataset_scaled = datasets[2].map(scale).batch(batch_size)

return train_dataset_scaled, test_dataset_scaled, val_dataset_scaled

train_dataset, test_dataset, val_dataset = get_dataset()

train_dataset.cache()

val_dataset.cache()

len(list(datasets[0]))

feature_extractor = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"

feature_extractor_layer = hub.KerasLayer(feature_extractor, input_shape=(224,224,3))

feature_extractor_layer.trainable = False

model = tf.keras.Sequential([

feature_extractor_layer, tf.keras.layers.Dropout(0.3), tf.keras.layers.Dense(3,activation='softmax')

])

model.summary()

model.compile(
Actual Label: bean_rust

Predicted Label: bean_rust

4/4 [==============================] - 9s 2s/step

tf.Tensor(

[[40 3 0]

[ 8 31 4]

[ 0 1 41]], shape=(3, 3), dtype=int32)


optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=['acc'])

history = model.fit(train_dataset, epochs=6, validation_data=val_dataset)

result=model.evaluate(test_dataset)

for test_sample in datasets[1].take(10):

image, label = test_sample[0], test_sample[1]

image_scaled, label_arr= scale(test_sample[0], test_sample[1])

image_scaled = np.expand_dims(image_scaled, axis=0)

img = tf.keras.preprocessing.image.img_to_array(image)

pred=model.predict(image_scaled)

print(pred)

plt.figure()

plt.imshow(image)

plt.show()

print("Actual Label: %s" % info.features["label"].names[label.numpy()])

print("Predicted Label: %s" % info.features["label"].names[np.argmax(pred)])

for f0,f1 in datasets[1].map(scale).batch(200):

y=np.argmax(f1, axis=1)

y_pred=np.argmax(model.predict(f0),axis=1)

print(tf.math.confusion_matrix(labels=y, predictions=y_pred, num_classes=3))

RESULT:
OUTPUT:

Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz

170498071/170498071 [==============================] - 4s 0us/step

WARNING:tensorflow:`input_shape` is undefined or non-square, or `rows` is not in [96, 128, 160, 192,


224]. Weights for input shape (224, 224) will be loaded as the default.

Downloading data from https://storage.googleapis.com/tensorflow/keras-


applications/mobilenet_v2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5

9406464/9406464 [==============================] - 0s 0us/step

Epoch 1/10

1563/1563 [==============================] - 100s 59ms/step - loss: 1.9126 - accuracy: 0.3041 -


val_loss: 1.8438 - val_accuracy: 0.3294

Epoch 2/10

1563/1563 [==============================] - 62s 40ms/step - loss: 1.8098 - accuracy: 0.3420 -


val_loss: 1.8148 - val_accuracy: 0.3475

Epoch 3/10

1563/1563 [==============================] - 61s 39ms/step - loss: 1.7668 - accuracy: 0.3557 -


val_loss: 1.7999 - val_accuracy: 0.3491

Epoch 4/10

1563/1563 [==============================] - 61s 39ms/step - loss: 1.7348 - accuracy: 0.3686 -


val_loss: 1.7927 - val_accuracy: 0.3475

Epoch 5/10

1563/1563 [==============================] - 58s 37ms/step - loss: 1.7075 - accuracy: 0.3810 -


val_loss: 1.7850 - val_accuracy: 0.3491

Epoch 6/10

1563/1563 [==============================] - 61s 39ms/step - loss: 1.6831 - accuracy: 0.3899 -


val_loss: 1.7851 - val_accuracy: 0.3574

Epoch 7/10

1563/1563 [==============================] - 61s 39ms/step - loss: 1.6637 - accuracy: 0.3959 -


val_loss: 1.7902 - val_accuracy: 0.3584

Epoch 8/10

1563/1563 [==============================] - 58s 37ms/step - loss: 1.6429 - accuracy: 0.4042 -


val_loss: 1.7944 - val_accuracy: 0.3565

Epoch 9/10

1563/1563 [==============================] - 59s 38ms/step - loss: 1.6255 - accuracy: 0.4104 -


val_loss: 1.7946 - val_accuracy: 0.3623
Ex No: 8 Using a pre trained model on Keras for Transfer Learning
Date:

PROGRAM:

#8modified

import numpy as np

from tensorflow.keras.applications import MobileNetV2

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, GlobalAveragePooling2D


Epoch 10/10

1563/1563 [==============================] - 60s 38ms/step - loss: 1.6095 - accuracy: 0.4162 -


val_loss: 1.8015 - val_accuracy: 0.3633

313/313 [==============================] - 9s 30ms/step - loss: 1.8015 - accuracy: 0.3633

Test accuracy: 0.36329999566078186


from tensorflow.keras.optimizers import Adam

from tensorflow.keras.datasets import cifar10

from tensorflow.keras.utils import to_categorical

# Load CIFAR-10 dataset

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

# Normalize pixel values to the range [0, 1]

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

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

# Convert labels to one-hot encoding

train_labels = to_categorical(train_labels, num_classes=10)

test_labels = to_categorical(test_labels, num_classes=10)

# Load the MobileNetV2 pre-trained model without the top classification layer

base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(32, 32, 3))

# Add your own classification layers on top of the pre-trained model

model = Sequential()

model.add(base_model)

model.add(GlobalAveragePooling2D())

model.add(Dense(256, activation='relu'))

model.add(Dense(10, activation='softmax')) # 10 classes in CIFAR-10

# Freeze the pre-trained layers to avoid retraining

for layer in base_model.layers:

layer.trainable = False

# Compile the model


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

# Train the model with transfer learning

model.fit(

train_images,

train_labels,

batch_size=32,

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}')

RESULT:
OUTPUT:

Loaded dataset with 25000 training samples, 25000 test samples

---review---

[1, 2, 365, 1234, 5, 1156, 354, 11, 14, 2, 2, 7, 1016, 2, 2, 356, 44, 4, 1349, 500, 746, 5, 200, 4, 4132, 11, 2,
2, 1117, 1831, 2, 5, 4831, 26, 6, 2, 4183, 17, 369, 37, 215, 1345, 143, 2, 5, 1838, 8, 1974, 15, 36, 119,
257, 85, 52, 486, 9, 6, 2, 2, 63, 271, 6, 196, 96, 949, 4121, 4, 2, 7, 4, 2212, 2436, 819, 63, 47, 77, 2, 180,
6, 227, 11, 94, 2494, 2, 13, 423, 4, 168, 7, 4, 22, 5, 89, 665, 71, 270, 56, 5, 13, 197, 12, 161, 2, 99, 76, 23,
2, 7, 419, 665, 40, 91, 85, 108, 7, 4, 2084, 5, 4773, 81, 55, 52, 1901]

---label---

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-


datasets/imdb_word_index.json

1641221/1641221 [==============================] - 0s 0us/step

---review with words---

['the', 'and', 'full', 'involving', 'to', 'impressive', 'boring', 'this', 'as', 'and', 'and', 'br', 'villain', 'and', 'and',
'need', 'has', 'of', 'costumes', 'b', 'message', 'to', 'may', 'of', 'props', 'this', 'and', 'and', 'concept', 'issue',
'and', 'to', "god's", 'he', 'is', 'and', 'unfolds', 'movie', 'women', 'like', "isn't", 'surely', "i'm", 'and', 'to',
'toward', 'in', "here's", 'for', 'from', 'did', 'having', 'because', 'very', 'quality', 'it', 'is', 'and', 'and', 'really',
'book', 'is', 'both', 'too', 'worked', 'carl', 'of', 'and', 'br', 'of', 'reviewer', 'closer', 'figure', 'really', 'there',
'will', 'and', 'things', 'is', 'far', 'this', 'make', 'mistakes', 'and', 'was', "couldn't", 'of', 'few', 'br', 'of', 'you',
'to', "don't", 'female', 'than', 'place', 'she', 'to', 'was', 'between', 'that', 'nothing', 'and', 'movies', 'get',
'are', 'and', 'br', 'yes', 'female', 'just', 'its', 'because', 'many', 'br', 'of', 'overly', 'to', 'descent', 'people',
'time', 'very', 'bland']

---label---

Maximum review length: 2697

Minimum review length: 14

Model: "sequential_8"

_________________________________________________________________

Layer (type) Output Shape Param #

=================================================================

embedding_1 (Embedding) (None, 500, 32) 160000

lstm (LSTM) (None, 100) 53200

dense_15 (Dense) (None, 1) 101


Ex No: 9 Perform Sentiment Analysis using RNN
Date:

PROGRAM :

from keras.datasets import imdb

vocabulary_size = 5000

(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words = vocabulary_size)

print('Loaded dataset with {} training samples, {} test samples'.format(len(X_train), len(X_test)))

print('---review---')

print(X_train[6])

print('---label---')
=================================================================

Total params: 213301 (833.21 KB)

Trainable params: 213301 (833.21 KB)

Non-trainable params: 0 (0.00 Byte)

_________________________________________________________________

None

Epoch 1/3

390/390 [==============================] - 329s 839ms/step - loss: 0.5208 - accuracy: 0.7455 -


val_loss: 0.6933 - val_accuracy: 0.4688

Epoch 2/3

390/390 [==============================] - 296s 757ms/step - loss: 0.4774 - accuracy: 0.7816 -


val_loss: 0.2735 - val_accuracy: 0.9531

Epoch 3/3

390/390 [==============================] - 323s 830ms/step - loss: 0.3032 - accuracy: 0.8773 -


val_loss: 0.2562 - val_accuracy: 0.9219

Test accuracy: 0.8694400191307068


print(y_train[6])

word2id = imdb.get_word_index()

id2word = {i: word for word, i in word2id.items()}

print('---review with words---')

print([id2word.get(i, ' ') for i in X_train[6]])

print('---label---')

print(y_train[6])

print('Maximum review length: {}'.format(

len(max((X_train + X_test), key=len))))

print('Minimum review length: {}'.format(

len(min((X_test + X_test), key=len))))

from keras.preprocessing import sequence

max_words = 500

X_train = sequence.pad_sequences(X_train, maxlen=max_words)

X_test = sequence.pad_sequences(X_test, maxlen=max_words)

from keras import Sequential

from keras.layers import Embedding, LSTM, Dense, Dropout

embedding_size=32

model=Sequential()

model.add(Embedding(vocabulary_size, embedding_size, input_length=max_words))

model.add(LSTM(100))

model.add(Dense(1, activation='sigmoid'))

print(model.summary())

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

batch_size = 64

num_epochs = 3

X_valid, y_valid = X_train[:batch_size], y_train[:batch_size]

X_train2, y_train2 = X_train[batch_size:], y_train[batch_size:]

model.fit(X_train2, y_train2, validation_data=(X_valid, y_valid), batch_size=batch_size,


epochs=num_epochs)
scores = model.evaluate(X_test, y_test, verbose=0)

print('Test accuracy:', scores[1])

RESULT :
OUTPUT:

Epoch 1/50

391/391 [==============================] - 9s 15ms/step - loss: 0.0500 - val_loss: 2.4432

Epoch 2/50

391/391 [==============================] - 4s 10ms/step - loss: 0.0167 - val_loss: 2.2989

Epoch 3/50

391/391 [==============================] - 4s 11ms/step - loss: 0.0127 - val_loss: 2.1644

Epoch 4/50

391/391 [==============================] - 4s 11ms/step - loss: 0.0092 - val_loss: 1.9999

Epoch 5/50

391/391 [==============================] - 5s 12ms/step - loss: 0.0059 - val_loss: 1.8826

Epoch 6/50

391/391 [==============================] - 5s 14ms/step - loss: 0.0044 - val_loss: 1.8453

Epoch 7/50

391/391 [==============================] - 4s 10ms/step - loss: 0.0037 - val_loss: 1.8315

Epoch 8/50

391/391 [==============================] - 4s 10ms/step - loss: 0.0030 - val_loss: 1.8091

Epoch 9/50

391/391 [==============================] - 6s 16ms/step - loss: 0.0023 - val_loss: 1.7798

Epoch 10/50

391/391 [==============================] - 7s 18ms/step - loss: 0.0018 - val_loss: 1.7508

Epoch 11/50

391/391 [==============================] - 7s 18ms/step - loss: 0.0014 - val_loss: 1.7283

Epoch 12/50

391/391 [==============================] - 6s 14ms/step - loss: 0.0011 - val_loss: 1.7099

Epoch 13/50

391/391 [==============================] - 5s 12ms/step - loss: 8.3746e-04 - val_loss: 1.6946

Epoch 14/50

391/391 [==============================] - 4s 10ms/step - loss: 6.5715e-04 - val_loss: 1.6803

Epoch 15/50

391/391 [==============================] - 4s 10ms/step - loss: 5.3298e-04 - val_loss: 1.6655


Ex No: 10 Implement an LSTM based Autoencoder in
Date: TensorFlow/Keras

PROGRAM :

#ex10modified

import numpy as np

import pandas as pd

import tensorflow as tf

from tensorflow.keras.layers import Input, LSTM, RepeatVector


Epoch 16/50

391/391 [==============================] - 5s 12ms/step - loss: 4.5067e-04 - val_loss: 1.6463

Epoch 17/50

391/391 [==============================] - 4s 10ms/step - loss: 3.9977e-04 - val_loss: 1.6290

Epoch 18/50

391/391 [==============================] - 4s 10ms/step - loss: 3.6568e-04 - val_loss: 1.6088

Epoch 19/50

391/391 [==============================] - 5s 13ms/step - loss: 3.3969e-04 - val_loss: 1.5898

Epoch 20/50

391/391 [==============================] - 4s 10ms/step - loss: 3.2057e-04 - val_loss: 1.5703

Epoch 21/50

391/391 [==============================] - 4s 10ms/step - loss: 3.0424e-04 - val_loss: 1.5515

Epoch 22/50

391/391 [==============================] - 5s 12ms/step - loss: 2.8821e-04 - val_loss: 1.5318

Epoch 23/50

391/391 [==============================] - 4s 10ms/step - loss: 2.7360e-04 - val_loss: 1.5141

Epoch 24/50

391/391 [==============================] - 4s 10ms/step - loss: 2.5993e-04 - val_loss: 1.4959

Epoch 25/50

391/391 [==============================] - 5s 12ms/step - loss: 2.4763e-04 - val_loss: 1.4814

Epoch 26/50

391/391 [==============================] - 4s 10ms/step - loss: 2.3611e-04 - val_loss: 1.4645

Epoch 27/50

391/391 [==============================] - 4s 10ms/step - loss: 2.2572e-04 - val_loss: 1.4493

Epoch 28/50

391/391 [==============================] - 5s 12ms/step - loss: 2.1465e-04 - val_loss: 1.4353

Epoch 29/50

391/391 [==============================] - 4s 10ms/step - loss: 2.0525e-04 - val_loss: 1.4215

Epoch 30/50

391/391 [==============================] - 4s 10ms/step - loss: 1.9676e-04 - val_loss: 1.4095

Epoch 31/50
from tensorflow.keras.models import Model

from sklearn.preprocessing import StandardScaler

# Load the data from 'JNJ.csv'

df = pd.read_csv('JNJ.csv')

data = df[['Close']].values # Assuming 'Close' column contains the data of interest

scaler = StandardScaler()

scaled_data = scaler.fit_transform(data)

# Define input sequence length and dimensionality

seq_length = 10

input_dim = scaled_data.shape[1]

# Define LSTM units in the encoder

latent_dim = 3

# Function to create sequences from data

def create_sequences(data, seq_length):

sequences = []

for i in range(len(data) - seq_length + 1):

sequence = data[i : i + seq_length]

sequences.append(sequence)

return np.array(sequences)

# Create sequences for training the autoencoder

X_train = create_sequences(scaled_data, seq_length)

# Define input layer

inputs = Input(shape=(seq_length, input_dim))


Epoch 32/50

391/391 [==============================] - 4s 10ms/step - loss: 1.8138e-04 - val_loss: 1.3879

Epoch 33/50

391/391 [==============================] - 4s 10ms/step - loss: 1.7507e-04 - val_loss: 1.3824

Epoch 34/50

391/391 [==============================] - 5s 12ms/step - loss: 1.6924e-04 - val_loss: 1.3732

Epoch 35/50

391/391 [==============================] - 4s 10ms/step - loss: 1.6465e-04 - val_loss: 1.3688

Epoch 36/50

391/391 [==============================] - 4s 10ms/step - loss: 1.6009e-04 - val_loss: 1.3624

Epoch 37/50

391/391 [==============================] - 5s 12ms/step - loss: 1.5708e-04 - val_loss: 1.3582

Epoch 38/50

391/391 [==============================] - 4s 10ms/step - loss: 1.5356e-04 - val_loss: 1.3555

Epoch 39/50

391/391 [==============================] - 4s 10ms/step - loss: 1.5100e-04 - val_loss: 1.3506

Epoch 40/50

391/391 [==============================] - 5s 12ms/step - loss: 1.4949e-04 - val_loss: 1.3452

Epoch 41/50

391/391 [==============================] - 4s 10ms/step - loss: 1.4679e-04 - val_loss: 1.3419

Epoch 42/50

391/391 [==============================] - 4s 10ms/step - loss: 1.4522e-04 - val_loss: 1.3402

Epoch 43/50

391/391 [==============================] - 5s 12ms/step - loss: 1.4359e-04 - val_loss: 1.3370

Epoch 44/50

391/391 [==============================] - 4s 10ms/step - loss: 1.4186e-04 - val_loss: 1.3367

Epoch 45/50

391/391 [==============================] - 4s 10ms/step - loss: 1.4108e-04 - val_loss: 1.3287

Epoch 46/50

391/391 [==============================] - 5s 12ms/step - loss: 1.3926e-04 - val_loss: 1.3271

Epoch 47/50
# Encoder

encoder = LSTM(latent_dim, return_sequences=False)(inputs)

encoded = RepeatVector(seq_length)(encoder)

# Decoder

decoder = LSTM(input_dim, return_sequences=True)(encoded)

# Define the autoencoder model

autoencoder = Model(inputs, decoder)

# Compile the model

autoencoder.compile(optimizer='adam', loss='mse')

# Train the autoencoder

autoencoder.fit(X_train, X_train, epochs=50, batch_size=32, validation_split=0.2)

# Generate predictions using the trained autoencoder

decoded_data = autoencoder.predict(X_train)

# Perform inverse scaling to get the original data back

decoded_data_original = scaler.inverse_transform(decoded_data.reshape(-1, input_dim))

# Show example output (first sequence) after training

print("Original Data:")

print(data[:seq_length])

print("\nReconstructed Data:")

print(decoded_data_original[0])
391/391 [==============================] - 4s 10ms/step - loss: 1.3826e-04 - val_loss: 1.3241

Epoch 48/50

391/391 [==============================] - 5s 13ms/step - loss: 1.3751e-04 - val_loss: 1.3166

Epoch 49/50

391/391 [==============================] - 5s 12ms/step - loss: 1.3676e-04 - val_loss: 1.3145

Epoch 50/50

391/391 [==============================] - 4s 10ms/step - loss: 1.3602e-04 - val_loss: 1.3140

489/489 [==============================] - 3s 4ms/step

Original Data:

[[0.22338 ]

[0.219907]

[0.217593]

[0.21412 ]

[0.210648]

[0.212963]

[0.217593]

[0.216435]

[0.21412 ]

[0.21412 ]]

Reconstructed Data:

[2.4625027]
RESULT:
OUTPUT:
4/4 [==============================] - 0s 4ms/step
Epoch: 0, Discriminator Loss: 0.595078706741333, Generator Loss:
0.5850967168807983
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
Epoch: 10, Discriminator Loss: 0.4640985429286957, Generator Loss:
0.7112982869148254
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
Epoch: 20, Discriminator Loss: 0.2534703016281128, Generator Loss:
1.2187508344650269
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 6ms/step
4/4 [==============================] - 0s 6ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 6ms/step
4/4 [==============================] - 0s 6ms/step
4/4 [==============================] - 0s 6ms/step
4/4 [==============================] - 0s 7ms/step
4/4 [==============================] - 0s 6ms/step
Epoch: 30, Discriminator Loss: 0.24690505862236023, Generator Loss:
1.46849524974823
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
Epoch: 40, Discriminator Loss: 0.28754252195358276, Generator Loss:
1.6038389205932617
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
Ex No: 11 Image generation using GAN
Date:

PROGRAM :

import numpy as np

import matplotlib.pyplot as plt

from tensorflow.keras.datasets import mnist

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Reshape, Flatten


4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
Epoch: 50, Discriminator Loss: 0.37254923582077026, Generator Loss:
1.4059895277023315
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 4ms/step
Epoch: 60, Discriminator Loss: 0.43914979696273804, Generator Loss:
1.3510558605194092
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
Epoch: 70, Discriminator Loss: 0.5920403599739075, Generator Loss:
0.6287504434585571
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
Epoch: 80, Discriminator Loss: 0.5499153137207031, Generator Loss:
0.9803311824798584
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
Epoch: 90, Discriminator Loss: 0.6397085189819336, Generator Loss:
0.7767328023910522
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 4ms/step
from tensorflow.keras.optimizers import Adam

# Load MNIST dataset (handwritten digits)

(train_images, _), (_, _) = mnist.load_data()

# Normalize and reshape images

train_images = (train_images.astype('float32') - 127.5) / 127.5

train_images = np.expand_dims(train_images, axis=-1)

# Define generator model

generator = Sequential([

Dense(256, input_shape=(100,), activation='relu'),

Dense(512, activation='relu'),

Dense(784, activation='tanh'),

Reshape((28, 28, 1))

])

# Define discriminator model

discriminator = Sequential([

Flatten(input_shape=(28, 28, 1)),

Dense(512, activation='relu'),

Dense(256, activation='relu'),

Dense(1, activation='sigmoid')

])

# Compile discriminator

discriminator.compile(optimizer=Adam(learning_rate=0.0002, beta_1=0.5), loss='binary_crossentropy',


metrics=['accuracy'])

# Define GAN model (generator -> discriminator)

gan = Sequential([generator, discriminator])


4/4 [==============================] - 0s 4ms/step
4/4 [==============================] - 0s 5ms/step
4/4 [==============================] - 0s 7ms/step
4/4 [==============================] - 0s 6ms/step
4/4 [==============================] - 0s 9ms/step
4/4 [==============================] - 0s 7ms/step
1/1 [==============================] - 0s 100ms/step
# Compile GAN (frozen discriminator, GAN's loss combines generator's loss and discriminator's loss)

discriminator.trainable = False

gan.compile(optimizer=Adam(learning_rate=0.0002, beta_1=0.5), loss='binary_crossentropy')

# Training parameters

epochs = 100

batch_size = 128

# Training loop

for epoch in range(epochs):

# Generate random noise for generator input

noise = np.random.normal(0, 1, (batch_size, 100))

# Generate fake images from noise using the generator

fake_images = generator.predict(noise)

# Sample real images from the training data

idx = np.random.randint(0, train_images.shape[0], batch_size)

real_images = train_images[idx]

# Combine real and fake images into a single batch

X = np.concatenate([real_images, fake_images])

# Labels for discriminator (1 for real images, 0 for fake images)

y_discriminator = np.zeros(2 * batch_size)

y_discriminator[:batch_size] = 1

# Train discriminator on the batch

d_loss = discriminator.train_on_batch(X, y_discriminator)

# Generate new noise for GAN training


noise = np.random.normal(0, 1, (batch_size, 100))

# Labels for GAN (1 to trick the discriminator)

y_gan = np.ones(batch_size)

# Train GAN (only updates the generator weights)

g_loss = gan.train_on_batch(noise, y_gan)

# Print progress every few epochs

if epoch % 10 == 0:

print(f'Epoch: {epoch}, Discriminator Loss: {d_loss[0]}, Generator Loss: {g_loss}')

# Generate and display some fake images

noise = np.random.normal(0, 1, (10, 100))

generated_images = generator.predict(noise)

plt.figure(figsize=(10, 10))

for i in range(10):

plt.subplot(1, 10, i + 1)

plt.imshow(generated_images[i].reshape(28, 28), cmap='gray')

plt.axis('off')

plt.tight_layout()

plt.show()

RESULT:

You might also like