0% found this document useful (0 votes)
13 views8 pages

Deeplg 3

The document contains Python code for a neural network implementation, including data loading, preprocessing, training, and testing functionalities. It utilizes libraries such as NumPy and Matplotlib, and includes methods for forward and backward propagation, weight initialization, and loss calculation. The model achieves a test accuracy of 90.90% after training on a dataset with 5000 samples.

Uploaded by

hudsonnnnn16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Deeplg 3

The document contains Python code for a neural network implementation, including data loading, preprocessing, training, and testing functionalities. It utilizes libraries such as NumPy and Matplotlib, and includes methods for forward and backward propagation, weight initialization, and loss calculation. The model achieves a test accuracy of 90.90% after training on a dataset with 5000 samples.

Uploaded by

hudsonnnnn16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

NAME : P KOUSHIK REDDY

ROLL NO : 12212161

Exp 4,5 code :

import numpy as np

import matplotlib.pyplot as plt

import os

# Load Dataset from Given Paths

def load_data():

data_path = r"C:\Users\THE ULTIMATE\Downloads\data.txt"

label_path = r"C:\Users\THE ULTIMATE\Downloads\label.txt"

if not os.path.exists(data_path) or not os.path.exists(label_path):

raise FileNotFoundError("Data files not found! Please check the file paths.")

# Load using the correct delimiter (CSV format)

X = np.loadtxt(data_path, delimiter=",") / 255.0 # Normalize pixel values (0-1)

y = np.loadtxt(label_path, delimiter=",", dtype=int) # Labels as integers

# Ensure labels are 1D

y = y.flatten()

# Ensure labels are within range [0-9]

if np.min(y) < 0 or np.max(y) >= 10:

raise ValueError(f"Labels out of range! Found values between {np.min(y)} and {np.max(y)}")

print(f"Dataset loaded successfully! Shape of X: {X.shape}, Shape of y: {y.shape}")

return X, y
# One-hot encoding function

def one_hot_encode(y, num_classes=10):

encoded = np.zeros((y.size, num_classes))

encoded[np.arange(y.size), y] = 1

return encoded

# Split dataset into Train (60%), Validation (20%), and Test (20%)

def split_data(X, y):

indices = np.arange(X.shape[0])

np.random.shuffle(indices)

train_size = int(0.6 * X.shape[0])

val_size = int(0.2 * X.shape[0])

train_idx, val_idx, test_idx = indices[:train_size], indices[train_size:train_size+val_size],


indices[train_size+val_size:]

return X[train_idx], y[train_idx], X[val_idx], y[val_idx], X[test_idx], y[test_idx]

# Activation functions and their derivatives

def sigmoid(x):

return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):

return x * (1 - x)

def tanh(x):

return np.tanh(x)

def tanh_derivative(x):
return 1 - np.tanh(x) ** 2

def softmax(x):

exp_x = np.exp(x - np.max(x, axis=1, keepdims=True)) # Stability improvement

return exp_x / np.sum(exp_x, axis=1, keepdims=True)

# Initialize network weights and biases

def initialize_weights(layer_sizes):

weights = []

biases = []

for i in range(len(layer_sizes) - 1):

weights.append(np.random.randn(layer_sizes[i], layer_sizes[i + 1]) * 0.01)

biases.append(np.zeros((1, layer_sizes[i + 1])))

return weights, biases

# Forward propagation

def forward_propagation(X, weights, biases, activation_func):

activations = [X]

pre_activations = []

for i in range(len(weights) - 1): # Hidden layers

Z = np.dot(activations[-1], weights[i]) + biases[i]

pre_activations.append(Z)

if activation_func == "sigmoid":

A = sigmoid(Z)

elif activation_func == "tanh":

A = tanh(Z)

activations.append(A)

# Output layer (Softmax)

Z = np.dot(activations[-1], weights[-1]) + biases[-1]


pre_activations.append(Z)

A = softmax(Z)

activations.append(A)

return activations, pre_activations

# Backward propagation

def backward_propagation(y_true, activations, pre_activations, weights, activation_func):

m = y_true.shape[0]

d_weights = [None] * len(weights)

d_biases = [None] * len(weights)

# Output layer error

dZ = activations[-1] - y_true

d_weights[-1] = np.dot(activations[-2].T, dZ) / m

d_biases[-1] = np.sum(dZ, axis=0, keepdims=True) / m

# Backpropagate through hidden layers

for i in range(len(weights) - 2, -1, -1):

if activation_func == "sigmoid":

dA = np.dot(dZ, weights[i + 1].T)

dZ = dA * sigmoid_derivative(activations[i + 1])

elif activation_func == "tanh":

dA = np.dot(dZ, weights[i + 1].T)

dZ = dA * tanh_derivative(activations[i + 1])

d_weights[i] = np.dot(activations[i].T, dZ) / m

d_biases[i] = np.sum(dZ, axis=0, keepdims=True) / m

return d_weights, d_biases


# Train the network

def train(X_train, y_train, X_val, y_val, architecture, activation_func="sigmoid", epochs=100,


lr=0.01):

weights, biases = initialize_weights(architecture)

y_train_oh = one_hot_encode(y_train)

y_val_oh = one_hot_encode(y_val)

train_losses, val_losses = [], []

for epoch in range(epochs):

activations, pre_activations = forward_propagation(X_train, weights, biases, activation_func)

d_weights, d_biases = backward_propagation(y_train_oh, activations, pre_activations, weights,


activation_func)

# Update weights and biases

for i in range(len(weights)):

weights[i] -= lr * d_weights[i]

biases[i] -= lr * d_biases[i]

# Calculate loss

train_loss = -np.mean(y_train_oh * np.log(activations[-1] + 1e-8))

val_activations, _ = forward_propagation(X_val, weights, biases, activation_func)

val_loss = -np.mean(y_val_oh * np.log(val_activations[-1] + 1e-8))

train_losses.append(train_loss)

val_losses.append(val_loss)

if epoch % 10 == 0:

print(f"Epoch {epoch}: Train Loss = {train_loss:.4f}, Val Loss = {val_loss:.4f}")

# Plot loss curve

plt.plot(range(epochs), train_losses, label="Train Loss")


plt.plot(range(epochs), val_losses, label="Validation Loss")

plt.xlabel("Epochs")

plt.ylabel("Loss")

plt.legend()

plt.show()

return weights, biases

# Test the model

def test(X_test, y_test, weights, biases, activation_func="sigmoid"):

activations, _ = forward_propagation(X_test, weights, biases, activation_func)

y_pred = np.argmax(activations[-1], axis=1)

accuracy = np.mean(y_pred == y_test) * 100

print(f"Test Accuracy: {accuracy:.2f}%")

# Load and preprocess data

X, y = load_data()

# Fix shape issue if needed

if X.shape[1] != 784:

print(f"Warning: Expected 784 features per sample, found {X.shape[1]}. Reshaping data...")

X = X[:, :784] # Trim extra columns

X_train, y_train, X_val, y_val, X_test, y_test = split_data(X, y)

# Run experiments

architectures = [[X.shape[1], 300, 10]]

for arch in architectures:

print(f"\nTraining architecture: {arch}")

weights, biases = train(X_train, y_train, X_val, y_val, architecture=arch, epochs=100, lr=0.01)


test(X_test, y_test, weights, biases)

Dataset loaded successfully! Shape of X: (5000, 400), Shape of y: (50000,)

Warning: Expected 784 features per sample, found 400. Reshaping data...

Training architecture: [400, 300, 10]

Epoch 0: Train Loss = 0.2406, Val Loss = 0.1869

Epoch 10: Train Loss = 0.0545, Val Loss = 0.0528

Epoch 20: Train Loss = 0.0456, Val Loss = 0.0451

Epoch 30: Train Loss = 0.0420, Val Loss = 0.0416

Epoch 40: Train Loss = 0.0398, Val Loss = 0.0395

Epoch 50: Train Loss = 0.0384, Val Loss = 0.0382

Epoch 60: Train Loss = 0.0374, Val Loss = 0.0372

Epoch 70: Train Loss = 0.0367, Val Loss = 0.0366

Epoch 80: Train Loss = 0.0362, Val Loss = 0.0361

Epoch 90: Train Loss = 0.0358, Val Loss = 0.0357

Test Accuracy: 90.90%

You might also like