import numpy as np #import the numpy library
import matplotlib.pyplot as plt #import the matplotlib library
def load_data(path): #define a function
def one_hot(y): #define a function
table = np.zeros((y.shape[0], 10))
for i in range(y.shape[0]):
table[i][int(y[i][0])] = 1
def normalize(x): #define a function
data = np.loadtxt('{}'.format(path), delimiter = ',')
return normalize(data[:,1:]),one_hot(data[:,:1])
X_train, y_train = load_data('mnist_train.csv')
X_test, y_test = load_data('mnist_test.csv')
#Design the neural network
def __init__(self, X, y, batch = 64, lr = 1e-3, epochs = 50):
self.x = self.input[:self.batch] # batch input
self.y = self.target[:self.batch] # batch target value
def init_weights(self): #define a function
self.W1 = np.random.randn(self.input.shape[1],256)
self.W2 = np.random.randn(self.W1.shape[1],128)
self.W3 = np.random.randn(self.W2.shape[1],self.y.shape[1])
self.b1 = np.random.randn(self.W1.shape[1],)
self.b2 = np.random.randn(self.W2.shape[1],)
self.b3 = np.random.randn(self.W3.shape[1],)
#define the activation function
z = z - np.max(z, axis = 1).reshape(z.shape[0],1)
return np.exp(z) / np.sum(np.exp(z), axis = 1).reshape(z.shape[0],1)
def shuffle(self): #define a function
idx = [i for i in range(self.input.shape[0])]
self.input = self.input[idx]
self.target = self.target[idx]
assert self.x.shape[1] == self.W1.shape[0]
self.z1 = self.x.dot(self.W1) + self.b1
self.a1 = self.ReLU(self.z1)
assert self.a1.shape[1] == self.W2.shape[0]
self.z2 = self.a1.dot(self.W2) + self.b2
self.a2 = self.ReLU(self.z2)
assert self.a2.shape[1] == self.W3.shape[0]
self.z3 = self.a2.dot(self.W3) + self.b3
self.a3 = self.softmax(self.z3)
self.error = self.a3 - self.y
#Backpropogation definition
dcost = (1/self.batch)*self.error
DW3 = np.dot(dcost.T,self.a2).T
DW2 = np.dot((np.dot((dcost),self.W3.T) * self.dReLU(self.z2)).T,self.a1).T
DW1 = np.dot((np.dot(np.dot((dcost),self.W3.T)*self.dReLU(self.z2),self.W2.T)*self.dReLU(self.z1)).T,self.x).T
db3 = np.sum(dcost,axis = 0)
db2 = np.sum(np.dot((dcost),self.W3.T) * self.dReLU(self.z2),axis = 0)
db1 = np.sum((np.dot(np.dot((dcost),self.W3.T)*self.dReLU(self.z2),self.W2.T)*self.dReLU(self.z1)),axis = 0)
assert DW3.shape == self.W3.shape
assert DW2.shape == self.W2.shape
assert DW1.shape == self.W1.shape
assert db3.shape == self.b3.shape
assert db2.shape == self.b2.shape
assert db1.shape == self.b1.shape
self.W3 = self.W3 - self.lr * DW3
self.W2 = self.W2 - self.lr * DW2
self.W1 = self.W1 - self.lr * DW1
self.b3 = self.b3 - self.lr * db3
self.b2 = self.b2 - self.lr * db2
self.b1 = self.b1 - self.lr * db1
def train(self): #define a function
for epoch in range(self.epochs):
for batch in range(self.input.shape[0]//self.batch-1):
end = (batch+1)*self.batch
self.x = self.input[start:end]
self.y = self.target[start:end]
l+=np.mean(self.error**2)
acc+= np.count_nonzero(np.argmax(self.a3,axis=1) == np.argmax(self.y,axis=1)) / self.batch
self.loss.append(l/(self.input.shape[0]//self.batch))
self.acc.append(acc*100/(self.input.shape[0]//self.batch))
def plot(self): #define a function
def test(self,xtest,ytest):
acc = np.count_nonzero(np.argmax(self.a3,axis=1) == np.argmax(self.y,axis=1)) / self.x.shape[0]
print("Accuracy:", 100 * acc, "%")
NN = NeuralNetwork(X_train, y_train)
Please i need a good report for this programming task code. We should provide a detailed description of the data set you used, describe the process of setti- ng up the model and preparing data for training, obtained results and conclusions. A rather formal report wri- tten in LATEX is expected. If you do not want to install additional software, you can use the www.overleaf.com to prepare a report. The template for which you need to prepare the report you can be find at HERE. Programming Explanation:- Normalization refers to rescaling actual-valued numeric attributes into a 00 to eleven variety. data normalization is used in system learning to make model training less sensitive to the size of capabilities. This allows our version to converge to better weights and, in turn, results in a more accurate version. Normalization makes the features more steady with each other, which lets in the model to predict outputs greater correctly. Python gives the preprocessing library, which includes the normalize function to normalize the information. It takes an array in as an enter and normalizes its values between 00 and 11. It then returns an output array with the identical dimensions because the input. A neural network is a system that learns the way to make predictions by using following these steps: Taking the enter facts making a prediction evaluating the prediction to the preferred output Adjusting its internal nation to expect correctly the next time Output:- Following is the output: Accuracy: 90.8 % 0.06 0.05 0.04 LOSS 0.03 0.02 10 30 40 50 20 Epochs