0% found this document useful (0 votes)
7 views18 pages

Neural Network Implementations Guide

The document outlines the implementation of various neural network architectures including a single-layer perceptron for the OR logic gate, a multi-layer perceptron for the XOR problem, a Radial Basis Function Network for function approximation, and a CNN for digit classification using the MNIST dataset. It also describes a CNN + SVM hybrid model for image classification, an LSTM model for horsepower prediction, and a Transformer model for text classification with attention visualization. Each section includes code snippets and visualizations to demonstrate the models' performance and results.
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)
7 views18 pages

Neural Network Implementations Guide

The document outlines the implementation of various neural network architectures including a single-layer perceptron for the OR logic gate, a multi-layer perceptron for the XOR problem, a Radial Basis Function Network for function approximation, and a CNN for digit classification using the MNIST dataset. It also describes a CNN + SVM hybrid model for image classification, an LSTM model for horsepower prediction, and a Transformer model for text classification with attention visualization. Each section includes code snippets and visualizations to demonstrate the models' performance and results.
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

1.

Implement a single-layer perceptron to solve the OR logic gate


problem.

import [Link] as plt


x1 = [0, 0, 1, 1]
x2 = [0, 1, 0, 1]
outputs = [0, 1, 1, 1]
[Link]()
[Link](x1, x2)
for i in range(len(outputs)):
[Link](x1[i] + 0.02, x2[i] + 0.02, f"OR={outputs[i]}")
[Link]("Input X1")
[Link]("Input X2")
[Link]("OR Logic Gate - Output Graph")
[Link](True)
[Link]()

Output :
2. Implement a multi-layer perceptron to solve a non-linear XOR problem.
Code :

import numpy as np
import [Link] as plt
X = [Link]([[0,0],[0,1],[1,0],[1,1]])
y = [Link]([[0],[1],[1],[0]])
def sigmoid(x):
return 1 / (1 + [Link](-x))
def sigmoid_deriv(x):
return x * (1 - x)
input_neurons = 2
hidden_neurons = 2
output_neurons = 1
lr = 0.5
epochs = 10000
[Link](42)
W1 = [Link](-1,1,(input_neurons, hidden_neurons))
b1 = [Link](-1,1,(1, hidden_neurons))
W2 = [Link](-1,1,(hidden_neurons, output_neurons))
b2 = [Link](-1,1,(1, output_neurons))
for epoch in range(epochs):
# Forward pass
z1 = [Link](X, W1) + b1
a1 = sigmoid(z1)
z2 = [Link](a1, W2) + b2
a2 = sigmoid(z2)
# Compute error
error = y - a2
# Backpropagation
d2 = error * sigmoid_deriv(a2)
d1 = [Link](W2.T) * sigmoid_deriv(a1)
# Update weights and biases
W2 += [Link](d2) * lr
b2 += [Link](d2, axis=0, keepdims=True) * lr
W1 += [Link](d1) * lr
b1 += [Link](d1, axis=0, keepdims=True) * lr
# Print final outputs
print("Final predictions after training:")
for i in range(len(X)):
z1 = [Link](X[i], W1) + b1
a1 = sigmoid(z1)
z2 = [Link](a1, W2) + b2
a2 = sigmoid(z2)
print(f"Input: {X[i]} -> Predicted: {a2[0][0]:.2f}")

# Visualization: Decision boundary


xx, yy = [Link]([Link](-0.5,1.5,200), [Link](-0.5,1.5,200))
grid = np.c_[[Link](), [Link]()]
# Forward pass for grid
z1_grid = sigmoid([Link](grid, W1) + b1)
z2_grid = sigmoid([Link](z1_grid, W2) + b2)
zz = z2_grid.reshape([Link])
[Link](xx, yy, zz, levels=[-0.1,0.5,1.1], alpha=0.3, colors=['blue','red'])
[Link](X[:,0], X[:,1], c=[Link](), s=100, cmap=[Link], edgecolors='k')
[Link]("X1")
[Link]("X2")
[Link]("MLP XOR Decision Boundary (NumPy Implementation)")
[Link]()

Output :
3. Implement a Radial Basis Function Network for Function
Approximation

Code :

import numpy as np
import [Link] as plt
from [Link] import KMeans
from [Link] import cdist

# 1. Generate Training Data


X = [Link](0, 2*[Link], 100).reshape(-1, 1)
Y = [Link](2*X) + 0.5*[Link](X)

# 2. Define Radial Basis Function


def rbf(x, c, s):
return [Link](-1/(2*s**2) * [Link](x-c, axis=1)**2)

# 3. Determine RBF Centers and Widths


num_rbf_neurons = 10
kmeans = KMeans(n_clusters=num_rbf_neurons, random_state=0).fit(X)
centers = kmeans.cluster_centers_
dMax = [Link](cdist(centers, centers))
sigma = dMax / [Link](2*num_rbf_neurons)

# 4. Construct RBF Feature Matrix


G = [Link](([Link][0], num_rbf_neurons))
for i, c in enumerate(centers):
G[:, i] = rbf(X, c, sigma)

# 5. Train Output Weights


W = [Link](G).dot(Y)

# 6. Define Prediction Function


def rbf_predict(X_new, centers, sigma, W):
G_new = [Link]((X_new.shape[0], len(centers)))
for i, c in enumerate(centers):
G_new[:, i] = rbf(X_new, c, sigma)
return G_new.dot(W)

# 7. Predict on Test Data


X_test = [Link](0, 2*[Link], 200).reshape(-1, 1)
Y_pred = rbf_predict(X_test, centers, sigma, W)

# 8. Plot Original Function and RBF Approximation


[Link](figsize=(8,5))
[Link](X, Y, 'b.', label='Original Data')
[Link](X_test, Y_pred, 'r-', label='RBF Approximation')
[Link](centers, np.zeros_like(centers), color='k', marker='x', label='RBF Centers')
[Link]('RBF Network Function Approximation')
[Link]('x')
[Link]('f(x)')
[Link]()
[Link]()

Output :

4. Implement a CNN for Digit Classification (MNIST)

# 1. Import Libraries
import tensorflow as tf
from [Link] import mnist
from [Link] import Sequential
from [Link] import Conv2D, MaxPooling2D, Flatten, Dense,
Dropout
from [Link] import to_categorical
import [Link] as plt

# 2. Load MNIST Dataset


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

# 3. Preprocess Data
# Reshape to (samples, height, width, channels)
X_train = X_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0
X_test = X_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0
# One-hot encode labels
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# 4. Build CNN Model


model = Sequential([
Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)),
MaxPooling2D(pool_size=(2,2)),
Conv2D(64, kernel_size=(3,3), activation='relu'),
MaxPooling2D(pool_size=(2,2)),
Flatten(),
Dense(128, activation='relu'),
Dropout(0.5),
Dense(10, activation='softmax')
])

# 5. Compile Model
[Link](optimizer='adam',loss='categorical_crossentropy',
metrics=['accuracy'])

# 6. Train Model
history = [Link](X_train, y_train, validation_split=0.1, epochs=5, batch_size=128,
verbose=1)

# 7. Evaluate Model
loss, accuracy = [Link](X_test, y_test)
print(f"Test Accuracy: {accuracy*100:.2f}%")

# 8. Plot Training History


[Link](figsize=(12,5))
[Link](1,2,1)
[Link]([Link]['accuracy'], label='Train Accuracy')
[Link]([Link]['val_accuracy'], label='Validation Accuracy')
[Link]('Accuracy')
[Link]('Epoch')
[Link]('Accuracy')
[Link]()
[Link](1,2,2)
[Link]([Link]['loss'], label='Train Loss')
[Link]([Link]['val_loss'], label='Validation Loss')
[Link]('Loss')
[Link]('Epoch')
[Link]('Loss')
[Link]()
[Link]()
Output :

5. Build a CNN + SVM Hybrid for Image Classification

Code :

# 1. Import Libraries
import numpy as np
import [Link] as plt
import seaborn as sns
from [Link] import mnist
from [Link] import Model
from [Link] import Input, Conv2D, MaxPooling2D, Flatten, Dense
from [Link] import SVC
from [Link] import accuracy_score, confusion_matrix
# 2. Load MNIST Dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# 3. Preprocess Data
X_train = X_train.reshape(-1,28,28,1).astype('float32') / 255.0
X_test = X_test.reshape(-1,28,28,1).astype('float32') / 255.0

# 4. Build CNN Feature Extractor using Functional API


inputs = Input(shape=(28,28,1))
x = Conv2D(32, (3,3), activation='relu', name='conv1')(inputs)
x = MaxPooling2D((2,2), name='pool1')(x)
x = Conv2D(64, (3,3), activation='relu', name='conv2')(x)
x = MaxPooling2D((2,2), name='pool2')(x)
x = Flatten(name='flatten')(x)
features = Dense(128, activation='relu', name='features')(x)

cnn_model = Model(inputs=inputs, outputs=features)

# 5. Extract CNN Features


cnn_features_train = cnn_model.predict(X_train, verbose=1)
cnn_features_test = cnn_model.predict(X_test, verbose=1)

# 6. Train SVM on CNN Features


svm = SVC(kernel='rbf')
[Link](cnn_features_train, y_train)

# 7. Predict with SVM


y_pred = [Link](cnn_features_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"CNN + SVM Hybrid Test Accuracy: {accuracy*100:.2f}%")

# 8. Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
[Link](figsize=(8,6))
[Link](cm, annot=True, fmt='d', cmap='Blues')
[Link]("Confusion Matrix")
[Link]("Predicted")
[Link]("Actual")
[Link]()

# 9. Visualize CNN Feature Maps for a Sample Image


sample_img = X_test[0].reshape(1,28,28,1)

# Get outputs of all Conv layers


conv_layers = [[Link] for layer in cnn_model.layers if 'conv' in [Link]]
activation_model = Model(inputs=cnn_model.input, outputs=conv_layers)
activations = activation_model.predict(sample_img)
# Plot first 6 feature maps of first conv layer
first_layer_activation = activations[0]
[Link](figsize=(12,6))
for i in range(6):
[Link](1,6,i+1)
[Link](first_layer_activation[0,:,:,i], cmap='viridis')
[Link]('off')
[Link]('First 6 Feature Maps of Conv Layer 1')
[Link]()

# 10. Display Sample Predictions


[Link](figsize=(10,4))
for i in range(10):
[Link](2,5,i+1)
[Link](X_test[i].reshape(28,28), cmap='gray')
[Link](f"Pred: {y_pred[i]}\nActual: {y_test[i]}")
[Link]('off')
[Link]('Sample CNN + SVM Predictions')
[Link]()

Output :
6. Implement a LSTM model for horsepower consumer prediction.

Code :

# 1. Import Libraries
import numpy as np
import [Link] as plt
from [Link] import MinMaxScaler
from [Link] import mean_squared_error, mean_absolute_error, r2_score
from [Link] import Sequential
from [Link] import LSTM, Dense

# 2. Generate Synthetic Horsepower Data


# (Demo purpose: replace this with real data if available)
time = [Link](0, 500, 1)
horsepower = 50 + 10*[Link](0.02*time) + 5*[Link](0.05*time) +
[Link](0,1,len(time))

[Link](figsize=(10,4))
[Link](time, horsepower)
[Link]('Synthetic Horsepower Data')
[Link]('Time')
[Link]('Horsepower')
[Link]()

# 3. Normalize Data
hp_data = [Link](-1,1)
scaler = MinMaxScaler(feature_range=(0,1))
hp_scaled = scaler.fit_transform(hp_data)

# 4. Prepare Time-Series Data for LSTM


def create_dataset(dataset, time_steps=10):
X, Y = [], []
for i in range(len(dataset) - time_steps):
[Link](dataset[i:i+time_steps, 0])
[Link](dataset[i+time_steps, 0])
return [Link](X), [Link](Y)

time_steps = 10
X, Y = create_dataset(hp_scaled, time_steps)
X = [Link]([Link][0], [Link][1], 1) # reshape for LSTM

# 5. Build LSTM Model


model = Sequential()
[Link](LSTM(50, return_sequences=False, input_shape=(time_steps,1)))
[Link](Dense(1))
[Link](optimizer='adam', loss='mean_squared_error')
# 6. Train the Model
history = [Link](X, Y, epochs=50, batch_size=16, validation_split=0.1, verbose=1)

# 7. Predict using LSTM


predicted_hp = [Link](X)
predicted_hp = scaler.inverse_transform(predicted_hp)
actual_hp = scaler.inverse_transform([Link](-1,1))

# 8. Evaluate Performance Metrics


mse = mean_squared_error(actual_hp, predicted_hp)
rmse = [Link](mse)
mae = mean_absolute_error(actual_hp, predicted_hp)
r2 = r2_score(actual_hp, predicted_hp)

print(f"Mean Squared Error (MSE): {mse:.4f}")


print(f"Root Mean Squared Error (RMSE): {rmse:.4f}")
print(f"Mean Absolute Error (MAE): {mae:.4f}")
print(f"R² Score: {r2:.4f}")

# 9. Plot Actual vs Predicted Horsepower


[Link](figsize=(10,5))
[Link](actual_hp, color='blue', label='Actual Horsepower')
[Link](predicted_hp, color='red', label='Predicted Horsepower')
[Link]('Horsepower Prediction using LSTM')
[Link]('Time')
[Link]('Horsepower')
[Link]()
[Link]()

# 10. Plot Training Loss


[Link](figsize=(8,4))
[Link]([Link]['loss'], label='Train Loss')
[Link]([Link]['val_loss'], label='Validation Loss')
[Link]('LSTM Training Loss')
[Link]('Epoch')
[Link]('Loss')
[Link]()
[Link]()
Output :
7. Implement Transformer text classification with attention visulization

Code :

# 1. Import Libraries
import tensorflow as tf
from [Link] import Layer, Input, Dense, Embedding, LayerNormalization,
Dropout, GlobalAveragePooling1D
from [Link] import Model
from [Link] import pad_sequences
from [Link] import imdb
import numpy as np
import [Link] as plt

# 2. Load IMDB Dataset


vocab_size = 10000
maxlen = 100 # shorter maxlen for visualization
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=vocab_size)

# Pad sequences
X_train = pad_sequences(X_train, maxlen=maxlen)
X_test = pad_sequences(X_test, maxlen=maxlen)

# Mapping index to words


word_index = imdb.get_word_index()
index_word = {v+3:k for k,v in word_index.items()}
index_word[0] = "<PAD>"
index_word[1] = "<START>"
index_word[2] = "<UNK>"
index_word[3] = "<UNUSED>"

# 3. Multi-Head Self-Attention Layer


class MultiHeadSelfAttention(Layer):
def __init__(self, embed_dim, num_heads=2):
super(MultiHeadSelfAttention, self).__init__()
self.embed_dim = embed_dim
self.num_heads = num_heads
if embed_dim % num_heads != 0:
raise ValueError("embedding dimension must be divisible by number of heads")
self.projection_dim = embed_dim // num_heads
self.query_dense = Dense(embed_dim)
self.key_dense = Dense(embed_dim)
self.value_dense = Dense(embed_dim)
self.combine_heads = Dense(embed_dim)

def attention(self, query, key, value):


score = [Link](query, key, transpose_b=True)
dim_key = [Link]([Link](key)[-1], tf.float32)
scaled_score = score / [Link](dim_key)
weights = [Link](scaled_score, axis=-1)
output = [Link](weights, value)
return output, weights

def separate_heads(self, x, batch_size):


x = [Link](x, (batch_size, -1, self.num_heads, self.projection_dim))
return [Link](x, perm=[0,2,1,3])

def call(self, inputs):


batch_size = [Link](inputs)[0]
query = self.query_dense(inputs)
key = self.key_dense(inputs)
value = self.value_dense(inputs)
query = self.separate_heads(query, batch_size)
key = self.separate_heads(key, batch_size)
value = self.separate_heads(value, batch_size)
attention_output, attention_weights = [Link](query, key, value)
attention_output = [Link](attention_output, perm=[0,2,1,3])
concat_attention = [Link](attention_output, (batch_size, -1, self.embed_dim))
output = self.combine_heads(concat_attention)
return output, attention_weights

# 4. Transformer Encoder Block


class TransformerBlock(Layer):
def __init__(self, embed_dim, num_heads, ff_dim, rate=0.1):
super(TransformerBlock, self).__init__()
[Link] = MultiHeadSelfAttention(embed_dim, num_heads)
[Link] = [Link]([Dense(ff_dim, activation='relu'), Dense(embed_dim)])
self.layernorm1 = LayerNormalization(epsilon=1e-6)
self.layernorm2 = LayerNormalization(epsilon=1e-6)
self.dropout1 = Dropout(rate)
self.dropout2 = Dropout(rate)

def call(self, inputs, training=False):


attn_output, attn_weights = [Link](inputs)
attn_output = self.dropout1(attn_output, training=training)
out1 = self.layernorm1(inputs + attn_output)
ffn_output = [Link](out1)
ffn_output = self.dropout2(ffn_output, training=training)
return self.layernorm2(out1 + ffn_output), attn_weights

# 5. Build Transformer Text Classifier


embed_dim = 64
num_heads = 2
ff_dim = 64

sequence_input = Input(shape=(maxlen,))
embedding_layer = Embedding(input_dim=vocab_size,
output_dim=embed_dim)(sequence_input)
transformer_block = TransformerBlock(embed_dim, num_heads, ff_dim)
x, attn_weights = transformer_block(embedding_layer)
x = GlobalAveragePooling1D()(x)
x = Dropout(0.1)(x)
x = Dense(20, activation='relu')(x)
output = Dense(1, activation='sigmoid')(x)

model = Model(inputs=sequence_input, outputs=output)


[Link](optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
[Link]()

# 6. Train the Model


history = [Link](X_train, y_train, validation_split=0.1, epochs=2, batch_size=64)

# 7. Evaluate
loss, accuracy = [Link](X_test, y_test)
print(f"Test Accuracy: {accuracy*100:.2f}%")

# 8. Attention Visualization for a Sample Review


sample_index = 0
sample_input = X_test[sample_index:sample_index+1]

# Build a model to output attention weights


attention_model = Model(inputs=sequence_input, outputs=[x, attn_weights])
pred, attn_out = attention_model.predict(sample_input)

# attn_out shape: (num_heads, seq_len, seq_len)


attn_out = attn_out[0] # first sample
attention_head = 0
attention_scores = attn_out[attention_head] # first head

# Map word indices back to text


sample_words = [index_word.get(idx, '?') for idx in X_test[sample_index] if idx != 0]

# Plot attention heatmap


[Link](figsize=(12,6))
[Link](attention_scores[:len(sample_words), :len(sample_words)], cmap='viridis')
[Link]()
[Link](ticks=[Link](len(sample_words)), labels=sample_words, rotation=90)
[Link](ticks=[Link](len(sample_words)), labels=sample_words)
[Link]("Attention Heatmap (Head 1)")
[Link]()

Output :
8. Apply BPE, WordPiece, or SentencePiece tokenizers on a sample text using
Tokenization and Basic Prompting Demo

# 1. Install libraries (run once)


!pip install tokenizers sentencepiece

# 2. Import libraries
from tokenizers import Tokenizer, models, pre_tokenizers, trainers
import sentencepiece as spm

# 3. Sample text
sample_text = "Machine learning enables computers to learn patterns from data."
print("Original Text:", sample_text)

# -----------------------------
# 4. BPE Tokenization
# -----------------------------
bpe_tokenizer = Tokenizer([Link]())
bpe_tokenizer.pre_tokenizer = pre_tokenizers.Whitespace() # split on whitespace

# Train BPE tokenizer


bpe_trainer = [Link](special_tokens=["<unk>", "<pad>", "<s>", "</s>"])
bpe_tokenizer.train_from_iterator([sample_text], trainer=bpe_trainer)

# Encode & decode


bpe_encoding = bpe_tokenizer.encode(sample_text)
print("\nBPE Tokens:", bpe_encoding.tokens)
print("BPE Token IDs:", bpe_encoding.ids)
print("Decoded Text:", bpe_tokenizer.decode(bpe_encoding.ids))

# -----------------------------
# 5. WordPiece Tokenization
# -----------------------------
wp_tokenizer = Tokenizer([Link](unk_token="[UNK]"))
wp_tokenizer.pre_tokenizer = pre_tokenizers.Whitespace()

# Train WordPiece tokenizer


wp_trainer = [Link](special_tokens=["[UNK]", "[PAD]", "[CLS]", "[SEP]"])
wp_tokenizer.train_from_iterator([sample_text], trainer=wp_trainer)

# Encode & decode


wp_encoding = wp_tokenizer.encode(sample_text)
print("\nWordPiece Tokens:", wp_encoding.tokens)
print("WordPiece Token IDs:", wp_encoding.ids)
print("Decoded Text:", wp_tokenizer.decode(wp_encoding.ids))

# -----------------------------
# 6. SentencePiece Tokenization
# -----------------------------
# Save sample text to file
with open("[Link]", "w") as f:
[Link](sample_text)

# Train SentencePiece BPE model


[Link](input='[Link]', model_prefix='spm_model',
vocab_size=50, model_type='bpe')

# Load model
sp = [Link]()
[Link]('spm_model.model')

# Encode & decode


sp_tokens = [Link](sample_text, out_type=str)
sp_ids = [Link](sample_text, out_type=int)
print("\nSentencePiece Tokens:", sp_tokens)
print("SentencePiece IDs:", sp_ids)
print("Decoded Text:", [Link](sp_ids))

Output :

You might also like