DL LAB Manual (Uma)
DL LAB Manual (Uma)
Course Objectives:
Understand the context of Neural networks and deep learning.
Introduce major Deep learning algorithms, the problem settings, and their applications to solve
real world problems
Course Outcomes (CO):
After completion of the course, students will be able to
Identify the Deep learning algorithms which are more appropriate for various types of learning
tasks in various domains
Implementing Deep learning algorithms and solve real-world problems.
List of Experiments:
1. Introduction of Keras.
2. Installing Keras and packages in Keras.
3. Train the model to add two numbers and report the result.
4. Train the model to multiply two matrices and report the result using keras.
5. Train the model to print the prime numbers using Keras
6. Recurrent Neural Network
a. Numpy implement of a simple recurrent neural network
b. Create a recurrent layer in keras
c. Prepare IMDB data for movie review classification problem.
d. Train the model with embedding and simple RNN layers.
e. Plot the Results
7. Consider temperature-forecast as one the example for recurrent neural network and
implement the following.
a. Inspect the data of the weather dataset
b. Parsing the data
c. Plotting the temperature timeseries
d. Plotting the first 10 days of the temperature timeseries
8. Long short-term memory network
a. Implement LSTM using LSTM layer in keras
b. Train and evaluate using reversed sequences for IMDB data
c. Train and evaluate a bidirectional LSTM for IMDB data
9. Train and evaluate a Gated Recurrent Unit based model
a. By using GRU layer
b. By adding dropout and recurrent dropout to GRU layer.
c. Train a bidirectional GRU for temperature prediction data
10. Convolutional Neural Networks
a. Preparing the IMDB data
b. Train and evaluate a simple 1D convent on IMDB Data
c. Train and evaluate a simple 1D convent on temperature prediction data
11. Develop a traditional LSTM for sequence classification problem.
PROJECTS:
1)Write a program for Multilabel Movie Poster Classification.
2)Write a program for Predicting Bike-Sharing patterns
References:
1) Ian Goodfellow, YoshuaBengio, Aaraon Courville, “Deep Learning (Adaptive Computation and
Machine Learning series)”, MIT Press, 2016.
1. Introduction of Keras
Keras is an open-source deep learning framework that provides a high-level interface for building and
training neural networks. It was developed with a focus on enabling fast experimentation and prototyping of
deep learning models. Keras is designed to be user-friendly, modular, and extensible, making it suitable for both
beginners and experienced machine learning practitioners.
1. **User-friendly API**: Keras offers a simple and intuitive API that allows users to quickly build and train
neural networks without having to write complex low-level code.
2. **Modularity**: Keras follows a modular design, allowing users to easily assemble neural network layers to
create complex architectures. It provides a wide range of built-in layers, such as dense (fully connected),
convolutional, recurrent, and more.
3. **Flexibility**: Keras supports both convolutional and recurrent neural networks, as well as combinations of
the two. It also provides support for arbitrary network architectures through its functional API, which enables
the creation of complex models with shared layers and multiple inputs or outputs.
4. **Extensibility**: Keras allows users to create custom layers, loss functions, and metrics, making it easy to
extend its functionality to suit specific use cases.
5. **Compatibility**: Keras can run on top of various deep learning libraries, including TensorFlow, Microsoft
Cognitive Toolkit (CNTK), and Theano. Since TensorFlow 2.0, Keras has been integrated directly into
TensorFlow as its official high-level API, making it the default choice for building neural networks with
TensorFlow.
Overall, Keras provides a powerful and flexible platform for building and training deep learning models,
making it a popular choice among researchers and practitioners in the field of machine learning.
You can install TensorFlow using pip, the Python package manager. Here's how you can install it:
```
pip install tensorflow
```
This command will install the latest version of TensorFlow available on PyPI (Python Package Index).
If you need specific versions of TensorFlow, you can specify the version number using the `==` syntax. For
example:
```
pip install tensorflow==2.7.0
```
Once TensorFlow (and therefore Keras) is installed, you can start using Keras for building and training your
deep learning models.
If you need additional packages for specific functionalities in Keras, such as plotting or data manipulation,
you can install them separately using pip. For example:
```
pip install matplotlib # For plotting
pip install pandas # For data manipulation
```
Make sure to install these packages in the same Python environment where TensorFlow/Keras is installed to
ensure compatibility.
3. Train the model to add two numbers and report the result.
# Training data
X_train = [
[2, 3],
[4, 5],
[6, 7],
[8, 9]
for i in range(len(X_train)):
prediction = sum(X_train[i])
for j in range(len(X_train[i])):
X_test = [
[10, 11],
[20, 30]
print("Predictions:", predictions)
OUTPUT:
Predictions: [21, 50]
4.Train the model to multiply two matrices and report the result using keras.
import numpy as np
X_train = np.array([
])
y_train = np.array([
])
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Reshape((2, 2))
])
model.compile(optimizer='adam', loss='mean_squared_error')
X_test = np.array([
predictions = model.predict(X_test)
print("Predictions:")
print(predictions)
OUTPUT:
Predictions:
[ 1.0109447 7.6764035]]
[[ 2.5122128 4.8759027]
import tensorflow as tf
def is_prime(n):
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
max_divisor = int(n**0.5) + 1
if n % d == 0:
return False
return True
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(1,)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
X_test = np.array([1001, 1009, 1013, 1019, 1021]) # Test some prime numbers
predictions = model.predict(X_test)
print("Predictions:")
print(predictions)
OUTPUT:
Predictions:
[[0.03500264]
[0.03425518]
[0.03388853]
[0.03334414]
[0.03316411]]
return 1 / (1 + np.exp(-x))
class SimpleRNNCell:
self.input_size = input_size
self.hidden_size = hidden_size
self.x = x
self.prev_hidden = prev_hidden
self.hidden = sigmoid(self.a)
return self.hidden
class SimpleRNN:
self.input_size = input_size
self.hidden_size = hidden_size
hiddens = []
for x in inputs:
hiddens.append(prev_hidden)
# Example usage
input_size = 3
hidden_size = 2
output = rnn.forward(inputs)
print("Output:")
print(output)
# Example usage
output = simple_rnn_layer(inputs)
print("Output:")
print(output)
num_words = 10000 # Only keep the top 10,000 most frequently occurring words
model = Sequential([
SimpleRNN(units=32),
Dense(units=1, activation='sigmoid')
])
history = model.history.history
plt.plot(history['accuracy'])
plt.plot(history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.show()
plt.plot(history['loss'])
plt.plot(history['val_loss'])
K.UMA MAHESWARI Asst. Professor CSE DEPT
(20A31502) DEEP LEARNING KLMCEW
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.show()
OUTPUT:
Output:
[[[1.09470334e-03 1.88426756e-03]]
[[2.03490379e-08 7.22700935e-06]]
[[3.79022223e-13 2.76662083e-08]]]
Output:
Epoch 1/5
Epoch 2/5
Epoch 3/5
Epoch 4/5
Epoch 5/5
weather_data = pd.read_csv('weather_dataset.csv')
# Inspect the first few rows of the dataset to understand its structure
print(weather_data.head())
dates = pd.to_datetime(weather_data['date'])
temperatures = weather_data['temperature']
plt.figure(figsize=(10, 6))
plt.plot(dates, temperatures)
plt.title('Temperature Timeseries')
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.grid(True)
plt.show()
K.UMA MAHESWARI Asst. Professor CSE DEPT
(20A31502) DEEP LEARNING KLMCEW
plt.figure(figsize=(10, 6))
plt.xlabel('Date')
plt.ylabel('Temperature (°C)')
plt.grid(True)
plt.show()
OUTPUT:
date temperature
0 2024-01-01 20.5
1 2024-01-02 22.3
2 2024-01-03 19.8
3 2024-01-04 18.2
4 2024-01-05 17.5
num_words = 10000 # Only keep the top 10,000 most frequently occurring words
lstm_model = Sequential([
LSTM(units=32),
Dense(units=1, activation='sigmoid')
])
bidirectional_model = Sequential([
Bidirectional(LSTM(units=32)),
Dense(units=1, activation='sigmoid')
])
OUTPUT:
Epoch 1/5
Epoch 2/5
...
import tensorflow as tf
num_words = 10000 # Only keep the top 10,000 most frequently occurring words
gru_model = Sequential([
GRU(units=32),
Dense(units=1, activation='sigmoid')
])
gru_dropout_model = Sequential([
Dense(units=1, activation='sigmoid')
])
bidirectional_gru_model = Sequential([
tf.keras.layers.Bidirectional(GRU(units=32)),
Dense(1)
])
bidirectional_gru_model.compile(optimizer='adam', loss='mean_squared_error')
OUTPUT:
Epoch 1/5
Epoch 2/5
...
num_words = 10000 # Only keep the top 10,000 most frequently occurring words
cnn_model_imdb = Sequential([
GlobalMaxPooling1D(),
Dense(units=1, activation='sigmoid')
])
print(f'Simple 1D CNN on IMDB Data Test Loss: {loss}, Test Accuracy: {accuracy}')
cnn_model_temp = Sequential([
GlobalMaxPooling1D(),
Dense(1)
])
cnn_model_temp.compile(optimizer='adam', loss='mean_squared_error')
OUTPUT:
Training and evaluating 1D CNN on IMDB Data:
Epoch 1/5
...
Epoch 1/5
...