Simple Artificial Neural Network (ANN) for Classification
import tensorflow as tf
from [Link] import Sequential
from [Link] import Dense
import numpy as np
# Sample data
X = [Link]([[0,0],[0,1],[1,0],[1,1]])
y = [Link]([0,1,1,0]) # XOR
# Build model
model = Sequential([
Dense(4, activation='relu', input_shape=(2,)),
Dense(1, activation='sigmoid')
])
[Link](optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
# Train model
[Link](X, y, epochs=500, verbose=0)
# Predict
print([Link](X))
2. CNN for Image Classification
import tensorflow as tf
(X_train, y_train), (X_test, y_test) = [Link].load_data()
X_train = X_train.reshape(-1,28,28,1)/255.0
X_test = X_test.reshape(-1,28,28,1)/255.0
model = [Link]([
[Link].Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),
[Link].MaxPooling2D((2,2)),
[Link](),
[Link](64, activation='relu'),
[Link](10, activation='softmax')
])
[Link](optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
[Link](X_train, y_train, epochs=5)
Lab experiment 1 : To design and implement a Multi-Layer Perceptron (MLP) using
TensorFlow/Keras for classifying handwritten digits and analyze the effect of activation functions,
optimization techniques, and regularization methods.
# Import required libraries
import tensorflow as tf
from [Link] import Sequential
from [Link] import Dense, Flatten, Dropout
# Step 1: Load the MNIST dataset
(X_train, y_train), (X_test, y_test) = [Link].load_data()
# Step 2: Normalize pixel values (0-255 --> 0-1)
X_train = X_train / 255.0
X_test = X_test / 255.0
# Step 3: Create the MLP model
model = Sequential()
# Convert 28x28 image into a 1D vector
[Link](Flatten(input_shape=(28, 28)))
# Hidden Layer 1
[Link](Dense(128, activation='relu'))
# Dropout layer (reduces overfitting)
[Link](Dropout(0.2))
# Hidden Layer 2
[Link](Dense(64, activation='relu'))
# Output layer (10 digits: 0-9)
[Link](Dense(10, activation='softmax'))
# Step 4: Compile the model
[Link](
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
# Step 5: Train the model
[Link](X_train, y_train, epochs=5, batch_size=32)
# Step 6: Evaluate the model
loss, accuracy = [Link](X_test, y_test)
print("Test Loss:", loss)
print("Test Accuracy:", accuracy)
# Step 7: Predict the first test image
prediction = [Link](X_test[:1])
print("Predicted Digit:", [Link]())
print("Actual Digit :", y_test[0])
Lab experiment 2: To design and implement a Deep Neural Network (DNN) to solve the XOR
(Exclusive OR) classification problem and understand the need for hidden layers in neural networks ..
Program 1: XOR Classification using Deep Neural Network (DNN)
import numpy as np
import tensorflow as tf
from [Link] import Sequential
from [Link] import Dense
# XOR Dataset
X = [Link]([[0, 0],
[0, 1],
[1, 0],
[1, 1]], dtype=float)
y = [Link]([[0],
[1],
[1],
[0]], dtype=float)
# Build DNN Model
model = Sequential([
Dense(8, activation='relu', input_shape=(2,)),
Dense(4, activation='relu'),
Dense(1, activation='sigmoid')
])
# Compile Model
[Link](optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
# Train Model
[Link](X, y, epochs=1000, verbose=0)
# Evaluate Model
loss, accuracy = [Link](X, y, verbose=0)
print("Accuracy:", accuracy)
# Predictions
predictions = [Link](X)
print("\nPredicted Outputs:")
print([Link](predictions))
print("\nActual Outputs:")
print(y)
Program 2: XOR Classification using MLP (Single Hidden Layer)
import numpy as np
import tensorflow as tf
from [Link] import Sequential
from [Link] import Dense
# XOR Dataset
X = [Link]([[0,0],
[0,1],
[1,0],
[1,1]])
y = [Link]([0,1,1,0])
# Create Model
model = Sequential([
Dense(4, activation='relu', input_shape=(2,)),
Dense(1, activation='sigmoid')
])
# Compile
[Link](optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
# Train
[Link](X, y, epochs=500, verbose=0)
# Test
loss, acc = [Link](X, y, verbose=0)
print("Accuracy:", acc)
# Predict
print("Predictions:")
print([Link]([Link](X)))