text_plain_output_1.png
import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import time

import pandas as pd
import numpy as np
data = pd.read_csv('../input/digit-recognizer/train.csv')
X = data.drop('label', axis=1).values
s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
one_hot = pd.get_dummies(s)
one_hot['label'] = one_hot.index
data = pd.merge(data, one_hot)
data = data.sample(frac=1)
data = data.drop('label', axis=1)
y = np.array(data[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]].values)
X = np.array(data.drop([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], axis=1).values)
X = X.T
y = y.T

import time
np.random.seed(42)
w1 = np.random.rand(32, 784)
b0 = np.ones((32, 42000))
w2 = np.random.rand(10, 32)
b1 = np.ones((10, 42000))
def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum(axis=0)
alpha = 0.005
loss = []
for i in range(10000):
    start = time.time()
    alpha = 0.5
    x1 = np.tanh(w1 @ X + b0)
    x2 = softmax(w2 @ x1 + b1)
    if i % 10 == 0:
        loss_ = -np.mean(np.sum(np.multiply(y, np.log(x2))))
        if i > 0:
            if loss_ > loss[len(loss) - 1]:
                alpha = 0.5
            elif (loss[len(loss) - 1] - loss_) / loss_ < 0.01:
                alpha = 10
        loss.append(loss_)
    delta_2 = np.multiply((x2 - y) / 42000, 1)
    delta_1 = np.multiply(w2.T @ delta_2, 1 - np.power(x1, 2))
    w1 = w1 - alpha * (delta_1 @ X.T)
    b0 = b0 - alpha * delta_1
    w2 = w2 - alpha * (delta_2 @ x1.T)
    b1 = b1 - alpha * delta_2

x2