Practice Deep Learning Fundamentals With Complete Answers
These exercises will help you practice the essential Python libraries and concepts needed for Deep Learning. Each exercise includes a clear question and a complete Python code answer.
Exercise 1: Create a NumPy array and print its values
Question:
Create a NumPy array containing the numbers 1 to 5 and print it.
Answer (Python Code):
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)Exercise 2: Normalize an image pixel value
Question:
A pixel value is 200. Normalize it to a 0–1 scale.
Answer (Python Code):
pixel = 200
normalized = pixel / 255
print(normalized)Exercise 3: Load a CSV file using Pandas
Question:
Write a Python program to load a dataset named "data.csv" using Pandas.
Answer (Python Code):
import pandas as pd
data = pd.read_csv("data.csv")
print(data.head())Exercise 4: Plot a simple line graph using Matplotlib
Question:
Plot the points (1,2), (2,4), (3,6), (4,8) on a line graph.
Answer (Python Code):
import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [2,4,6,8]
plt.plot(x, y)
plt.xlabel("X values")
plt.ylabel("Y values")
plt.title("Simple Line Graph")
plt.show()Exercise 5: Build a simple neural network with one layer
Question:
Build a Keras model with one dense layer containing 5 neurons.
Answer (Python Code):
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential([
Dense(5, activation='relu', input_shape=(3,))
])
model.summary()Exercise 6: Perform label encoding on a list of animal names
Question:
Encode the labels ["cat", "dog", "cat", "horse"].
Answer (Python Code):
from sklearn.preprocessing import LabelEncoder
labels = ["cat", "dog", "cat", "horse"]
encoder = LabelEncoder()
encoded = encoder.fit_transform(labels)
print(encoded)Exercise 7: Create a random image tensor using TensorFlow
Question:
Generate a random image of shape 224×224×3.
Answer (Python Code):
import tensorflow as tf
image = tf.random.uniform((224, 224, 3))
print(image.shape)Exercise 8: Resize an image using TensorFlow
Question:
Resize a random 300×300×3 image to 128×128.
Answer (Python Code):
import tensorflow as tf
img = tf.random.uniform((300, 300, 3))
resized = tf.image.resize(img, (128, 128))
print(resized.shape)Exercise 9: Build a simple classifier model in Keras
Question:
Create a model with:
- 1 hidden layer (8 neurons, ReLU)
- Output layer (1 neuron, Sigmoid)
Answer (Python Code):
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential([
Dense(8, activation='relu', input_shape=(4,)),
Dense(1, activation='sigmoid')
])
model.summary()Exercise 10: Save a trained model
Question:
Write Python code to save a trained Keras model as "mymodel.h5".
Answer (Python Code):
# assume model is already built and trained
model.save("mymodel.h5")
print("Model saved successfully!")