<jupyter_start><jupyter_text>Setting up the Environment<jupyter_code>import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>Getting the Data<jupyter_code>data = pd.read_csv("./train.csv")
data.sample(frac = 1).reset_index(drop = True)
data.head()
print("No of Examples: ",data.shape[0])<jupyter_output>No of Examples:  42000<jupyter_text>Splitting the Data into Train and Validation<jupyter_code>train_data = data.iloc[:33600]
valid_data = data.iloc[33600:]
print(train_data.shape)
print(valid_data.shape)
X_train = train_data.iloc[:,1:].reset_index(drop = True)
y_train = train_data.iloc[:,0].reset_index(drop = True)

X_valid = valid_data.iloc[:,1:].reset_index(drop = True)
y_valid = valid_data.iloc[:,0].reset_index(drop = True)
print("X_train : ", X_train.shape)
print("y_train : ", y_train.shape)
print("X_valid : ", X_valid.shape)
print("y_valid : ", y_valid.shape)<jupyter_output>X_train :  (33600, 784)
y_train :  (33600,)
X_valid :  (8400, 784)
y_valid :  (8400,)<jupyter_text>Getting the Train Input and Labels into Shape<jupyter_code>X_train = X_train.to_numpy().transpose()
# One Hot Encoding the Labels
label_mat = np.zeros((10, y_train.shape[0]))

for i in range(y_train.shape[0]):
    label_mat[y_train[i]][i] = 1
    
y_train_onehot = label_mat
print("X_train : ", X_train.shape)
print("y_train : ", y_train_onehot.shape)<jupyter_output>X_train :  (784, 33600)
y_train :  (10, 33600)<jupyter_text>Rescaling the Train Input<jupyter_code>X_train = X_train / 255<jupyter_output><empty_output><jupyter_text>Modelling the Neural Network and Functions<jupyter_code>def sigmoid(x):
    return 1.0/(1+ np.exp(-x))
  
def sigmoid_derivative(x):
    s = sigmoid(x)
    return s * (1.0 - s) 

class NeuralNetwork:   
    def __init__(self, sizes): 
        np.random.seed(7)
        self.num_layers = len(sizes)
        self.sizes = sizes
        self.biases = np.array([np.zeros((y,1)) for y in sizes[1:]])
        self.weights = np.array([np.random.randn(y, x) * 0.05 for x, y in zip(sizes[:-1], sizes[1:])]) 

    def feedforward(self, a):        
        for b, w in zip(self.biases, self.weights):
            a = sigmoid(np.dot(w, a)+b)
        return a
    
    def cost_func(self,a2,y):
        logloss = np.multiply(np.log(a2),y) + np.multiply((1-y),np.log(1-a2))
        cost = -( np.sum(logloss) ) / m
        cost = np.squeeze(cost)
        return cost
    
    def test(self,t):
        test_output = self.feedforward(t)
        t_label = test_output.argmax(axis = 0)
        return t_label

    def cost_derivative(self, output_activations, y):
        return (output_activations-y)    
        
    def backprop(self,x,y,alpha):
        n_b = np.array([np.zeros(b.shape) for b in self.biases])     
        n_w = np.array([np.zeros(w.shape) for w in self.weights])
        m = x.shape[1]
        
        # feedforward pass
        activation = x
        activations = [x] 
        zs = [] 
        for b, w in zip(self.biases, self.weights):            
            z = np.dot(w, activation)+b
            zs.append(z)
            activation = sigmoid(z)
            activations.append(activation)
            
        cost = self.cost_func(activations[-1],y)
       
        delta = self.cost_derivative(activations[-1], y)                                     
        n_b[-1] = (1/m) * delta.sum(axis=1,keepdims = True )        
        n_w[-1] = (1/m) * np.dot(delta, activations[-2].transpose())
        
        # l=1 means last layer, l=2 means 2nd layer layer, so on and so forth.
        for l in range(2, self.num_layers):
            z = zs[-l]
            sd = sigmoid_derivative(z)
            delta = np.dot(self.weights[-l+1].transpose(), delta) * sd
            n_b[-l] = (1/m) * delta.sum(axis=1, keepdims= True)
            n_w[-l] = (1/m) * np.dot(delta, activations[-l-1].transpose())

        self.weights = self.weights - alpha * n_w 
        self.biases = self.biases - alpha * n_b 
        
        return cost<jupyter_output><empty_output><jupyter_text>Training the Model<jupyter_code>n, m = X_train.shape
print("No of Training Examples: ", m)
print("No of Pixels in each image: ", n)
sizes = [n,500,10]  # No of units in each layers
learning_rate = 0.1
epochs = 10000
nn = NeuralNetwork(sizes)
for i in range(epochs):
    cost = nn.backprop(X_train, y_train_onehot, learning_rate)
    if (i%10 == 0) or (i == epochs-1):
        print("Iteration: {0} ---> Cost: {1}".format(i,cost))<jupyter_output>Iteration: 0 ---> Cost: 7.814775643575334
Iteration: 10 ---> Cost: 3.1218717215801046
Iteration: 20 ---> Cost: 2.979932475022972
Iteration: 30 ---> Cost: 2.843202139634936
Iteration: 40 ---> Cost: 2.710422967517665
Iteration: 50 ---> Cost: 2.581791068799056
Iteration: 60 ---> Cost: 2.458342814616878
Iteration: 70 ---> Cost: 2.341389246104713
Iteration: 80 ---> Cost: 2.2320924085424063
Iteration: 90 ---> Cost: 2.131221187181326
Iteration: 100 ---> Cost: 2.039070178169018
Iteration: 110 ---> Cost: 1.95549889096356
Iteration: 120 ---> Cost: 1.8800390182934674
Iteration: 130 ---> Cost: 1.8120210185758678
Iteration: 140 ---> Cost: 1.750686171214641
Iteration: 150 ---> Cost: 1.695268728916242
Iteration: 160 ---> Cost: 1.64504697095832
Iteration: 170 ---> Cost: 1.5993692284153718
Iteration: 180 ---> Cost: 1.5576627849316256
Iteration: 190 ---> Cost: 1.5194325387974248
Iteration: 200 ---> Cost: 1.4842543987014711
Iteration: 210 ---> Cost: 1.4517665821218577
Iteration: 220 ---> Cost: 1.42166063[...]<jupyter_text>Getting the Train Accuracy<jupyter_code>train_preds = nn.test(X_train)
count = 0
for i in range(X_train.shape[1]):
    if train_preds[i] == y_train[i]:
        count += 1
    
print("Training Accuracy: ", count / X_train.shape[1])<jupyter_output>Training Accuracy:  0.9507440476190476<jupyter_text>Getting the Validation Accuracy<jupyter_code>X_valid = X_valid.to_numpy().transpose()
X_valid = X_valid / 255

print("X_valid: ", X_valid.shape)
print("y_valid: ", y_valid.shape)
valid_preds = nn.test(X_valid)
count = 0
for i in range(X_valid.shape[1]):
    if valid_preds[i] == y_valid[i]:
        count += 1

print("Validation Accuracy: ", count / X_valid.shape[1])<jupyter_output>Validation Accuracy:  0.945<jupyter_text>Getting the Test Data<jupyter_code>test_data = pd.read_csv("./test.csv")
test_data.head()
X_test = test_data.to_numpy().transpose()
X_test = X_test / 255
print("X_test: ", X_test.shape)
test_preds = nn.test(X_test)
test_preds.shape<jupyter_output><empty_output><jupyter_text>Creating the Output CSV File<jupyter_code>imageid = np.arange(test_preds.shape[0]) + 1
imageid
df = pd.DataFrame({'ImageId': imageid, 'Label': test_preds})
df.head()
df.to_csv('output_N_Hidden_Layers.csv', index = False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14694968086685467, 'fraction_numerical': 0.08698233635149176, 'mean_word_length': 3.3668178872326635, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '395115', 'license': 'MIT', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2573, 'n_words': 503, 'path': 'N_Hidden_Layers.ipynb', 'repo_name': 'rdutta1999/MNIST', 'size': '6737'}