Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please i need good report asap code: Programming:- import numpy as np #import the numpy library import matplotlib.pyplot as plt #import the matplotlib library def

Please i need good report asap image text in transcribed
image text in transcribed
image text in transcribed
code:
Programming:-
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
return table
def normalize(x): #define a function
x = x / 255
return x
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
class NeuralNetwork:
def __init__(self, X, y, batch = 64, lr = 1e-3, epochs = 50):
self.input = X
self.target = y
self.batch = batch
self.epochs = epochs
self.lr = lr
self.x = self.input[:self.batch] # batch input
self.y = self.target[:self.batch] # batch target value
self.loss = []
self.acc = []
self.init_weights()
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
def ReLU(self, x):
return np.maximum(0,x)
def dReLU(self,x):
return 1 * (x > 0)
def softmax(self, z):
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])]
np.random.shuffle(idx)
self.input = self.input[idx]
self.target = self.target[idx]
#Feed-forward definition
def feedforward(self):
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
def backprop(self):
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):
l = 0
acc = 0
self.shuffle()
for batch in range(self.input.shape[0]//self.batch-1):
start = batch*self.batch
end = (batch+1)*self.batch
self.x = self.input[start:end]
self.y = self.target[start:end]
self.feedforward()
self.backprop()
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
plt.figure(dpi = 125)
plt.plot(self.loss)
plt.xlabel("Epochs")
plt.ylabel("Loss")
def acc_plot(self):
plt.figure(dpi = 125)
plt.plot(self.acc)
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
def test(self,xtest,ytest):
self.x = xtest
self.y = ytest
self.feedforward()
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)
NN.train()
NN.plot()
NN.test(X_test,y_test)
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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions

Question

Explain how you would reduce stress at work.

Answered: 1 week ago