Assignment No 2
Assignment No 2
Conclusion : In this way we are able to learn about the Deep Neural
Network and its
implementation on the boston dataset.
Assignment no.2
Code to implement sentiment analysis :
import numpy as np
from keras.datasets import imdb
from keras.preprocessing.sequence import pad_sequences from
keras.models import Sequential
from keras.layers import Embedding, Bidirectional, LSTM, Dense
# Load the IMDB dataset
(x_train, y_train), (x_test, y_test) = imdb.load_data()
#
Pad or truncate the sequences to a fixed length of 250 words
max_len = 250
x_train = pad_sequences(x_train, maxlen=max_len) x_test =
pad_sequences(x_test, maxlen=max_len)
#Define the deep neural network architecture
model = Sequential()
model.add(Embedding(input_dim=10000, output_dim=128,
input_length=max_len))
model.add(Bidirectional(LSTM(64, return_sequences=True)))
model.add(Bidirectional(LSTM(32)))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
# Train the model
history = model.fit(x_train, y_train, epochs=10, batch_size=128,
validation_split=0.2)
# Evaluate the model on the test set
.loss, acc = model.evaluate(x_test, y_test, batch_size=128)
print(f'Test accuracy: {acc:.4f}, Test loss: {loss:.4f}')
This example implements a deep neural network with two layers of
bidirectional LSTM cells,
which are capable of learning complex patterns in sequence data. The
Embedding layer learns the
word embeddings from the input sequences, which are then fed into the
LSTM layers. The output
of the LSTM layers is then fed into a dense output layer with a sigmoid
activation function, which
outputs the binary classification.
The compile() method is used to compile the model with binary cross-
entropy loss and the Adam
optimizer. The fit() method is used to train the model on the training set
for 10 epochs with a
batch size of 128. The evaluate() method is used to evaluate the trained
model on the test set and
compute the accuracy and loss.
This example demonstrates how deep neural networks can be used for
binary classification on text
data, specifically for classifying movie reviews as positive or negative
based on the text content.
Conclusion :
In this way we are able to learn about the Deep Neural Network and its
implementation on
the IMDB dataset.Learn about sentiment analysis.
Assignment no. 3
Conclusion:
In this way we are able to implement Convolutional neural network
(CNN) Using MNIST
Fashion Dataset.
Assignment no. 4
Code to implement RNN
Import the required libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout
Load the dataset
data = pd.read_csv('GOOG.csv')
Prepare the data
# Extract the 'Open' column
dataset = data['Open'].values.reshape(-1, 1)
# Scale the data between 0 and 1
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# Create the training and testing datasets
training_data_len = int(len(dataset) * 0.8)
training_data = dataset[:training_data_len]
testing_data = dataset[training_data_len:]
def create_dataset(dataset, time_step=1):
X, Y = [], []
for i in range(len(dataset) - time_step - 1):
X.append(dataset[i:(i+time_step), 0])
Y.append(dataset[i+time_step, 0])
return np.array(X), np.array(Y)
# Create the training and testing datasets with a time step of 60 days
time_step = 60
.X_train, Y_train = create_dataset(training_data, time_step)
X_test, Y_test = create_dataset(testing_data, time_step)
# Reshape the training and testing datasets
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
Create the RNN model
model = Sequential()
model.add(LSTM(units=50, return_sequences=True,
input_shape=(X_train.shape[1], 1)))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(
Output:
Epoch 100/100
33/33 [==============================] - 7s 39ms/step - loss:
0.0013