Deeplg 3
Deeplg 3
ROLL NO : 12212161
import numpy as np
import os
def load_data():
raise FileNotFoundError("Data files not found! Please check the file paths.")
y = y.flatten()
raise ValueError(f"Labels out of range! Found values between {np.min(y)} and {np.max(y)}")
return X, y
# One-hot encoding function
encoded[np.arange(y.size), y] = 1
return encoded
# Split dataset into Train (60%), Validation (20%), and Test (20%)
indices = np.arange(X.shape[0])
np.random.shuffle(indices)
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):
def initialize_weights(layer_sizes):
weights = []
biases = []
# Forward propagation
activations = [X]
pre_activations = []
pre_activations.append(Z)
if activation_func == "sigmoid":
A = sigmoid(Z)
A = tanh(Z)
activations.append(A)
A = softmax(Z)
activations.append(A)
# Backward propagation
m = y_true.shape[0]
dZ = activations[-1] - y_true
if activation_func == "sigmoid":
dZ = dA * sigmoid_derivative(activations[i + 1])
dZ = dA * tanh_derivative(activations[i + 1])
y_train_oh = one_hot_encode(y_train)
y_val_oh = one_hot_encode(y_val)
for i in range(len(weights)):
weights[i] -= lr * d_weights[i]
biases[i] -= lr * d_biases[i]
# Calculate loss
train_losses.append(train_loss)
val_losses.append(val_loss)
if epoch % 10 == 0:
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.show()
X, y = load_data()
if X.shape[1] != 784:
print(f"Warning: Expected 784 features per sample, found {X.shape[1]}. Reshaping data...")
# Run experiments
Warning: Expected 784 features per sample, found 400. Reshaping data...