Question: Add code to the following python program so that it will run. The program trains a neural net using back-propagation to perform the XOR operation

Add code to the following python program so that it will run. The program trains a neural net using back-propagation to perform the XOR operation Finish the nnOutput and train functions so that the code will run. The program when run should print:

0.0310489 [

[ 0.004588 ]

[0.968793 ]

[0.964671 ]

[0.052381 ]]

import numpy as np

import pylab as pl

## you may need to add some other functions here

def nnOutput(alpha, beta, X):

## add your code here

return output;

################################################

## X is the input samples

## Y is the desired outputs

## nHiddens is the number of hidden units

## rhoh is the coefficient for alpha

## rhoo is the coefficient for beta

## mom is the momentum value

## wmax is the range of the weights

## nEpochs is the number of iterations

## alpha is the weights in the hidden layer

## beta is the weight in the output layer

################################################

def Train(X, Y, nHiddens, rhoh, rhoo, mom, wmax, nEpochs):

nSamples = np.shape(X)[0];

nInputs = np.shape(X)[1];

nOutputs = np.shape(Y)[1];

alpha = np.random.uniform(-wmax, wmax, (1+nInputs,nHiddens));

beta = np.random.uniform(-wmax, wmax, (1+nHiddens,nOutputs));

## add your code here

return alpha, beta;

def makeIndicatorVariables(labels, datasize, numClasses, class_labels):

indicator = np.zeros((datasize, numClasses));

## add your code here

return indicator;

def ComputeAccuracy(output, labels):

TP = 0;

FP = 0;

for i in range(0,np.shape(output)[0]):

predict = np.argmax(output[i,:]);

if (predict == labels[i]):

TP = TP + 1;

else:

FP = FP + 1;

Acc = 100 * TP / float(TP + FP);

print(Acc);

def DisplayDigit(sample, label):

sample = sample.reshape(28,28)

[fig, axs] = pl.subplots(1,1)

axs.imshow(sample, cmap='Greys_r')

print('The label is %d '%(label))

def RunDigit():

dataset = ReadDataSet()

dataset.Read('mnist.pkl')

[train_data, train_labels] = dataset.getTrainData()

## display one sample and its label

DisplayDigit(train_data[10], train_labels[10])

datasize = train_data.shape[0];

class_labels = sorted(np.unique(train_labels));

numClasses = len(class_labels);

indicator = makeIndicatorVariables(train_labels, datasize, numClasses, class_labels);

print('Training NN ...');

[alpha, beta] = Train(np.matrix(train_data), np.matrix(indicator), 500, 0.1, 0.001, 0.5, 0.0001, 1000);

print('Testing NN ...');

test_out = nnOutput(alpha, beta, train_data);

ComputeAccuracy(test_out, train_labels);

[test_data, test_labels] = dataset.getTestData()

test_out = nnOutput(alpha, beta, test_data);

ComputeAccuracy(test_out, test_labels);

if __name__ == '__main__':

RunDigit();

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!