Commit d768e8d6 authored by SHREYANSH JAIN's avatar SHREYANSH JAIN

added on kaggle with 96.8

parent 2c147717
......@@ -48,15 +48,16 @@ def preprocessMnist(X):
def taskMnist():
XTrain, YTrain, XVal, YVal, XTest, _ = loadMnist()
# Create a NeuralNetwork object 'nn1' as follows with optimal parameters. For parameter definition, refer to py file.
lr,batchSize,epochs = 0.01,256,50
lr,batchSize,epochs = 0.005,256,10000
nn1 = nn.NeuralNetwork(lr, batchSize, epochs)
# Add layers to neural network corresponding to inputs and outputs of given data
input_layer = XTrain.shape[1]
hidden_layer = 256
hidden_layer1 = 128
# hidden_layer2 = 64
output_layer = 10
# activation_fn = 'softmax' # 'relu'
nn1.addLayer(nn.FullyConnectedLayer(input_layer,hidden_layer,'relu'))
nn1.addLayer(nn.FullyConnectedLayer(hidden_layer,output_layer,'softmax'))
nn1.addLayer(nn.FullyConnectedLayer(input_layer,hidden_layer1,'relu'))
# nn1.addLayer(FullyConnectedLayer(hidden_layer1,hidden_layer2,'relu'))
nn1.addLayer(nn.FullyConnectedLayer(hidden_layer1,output_layer,'softmax'))
###############################################
# TASK 3b (Marks 13) - YOUR CODE HERE
# raise NotImplementedError
......
......@@ -26,23 +26,24 @@ class NeuralNetwork:
# validY - Corresponding list of validation data labels
for i in range(self.epochs):
dataSize = trainX.shape[0]
rndIndex = np.random.permutation(dataSize)
trainX,trainY = trainX[rndIndex],trainY[rndIndex]
print('Epoch',i+1)
batchDataX = trainX[:self.batchSize]
batchDataY = trainY[:self.batchSize]
# prediction = predict(batchDataX)
# dataSize = trainX.shape[0]
# rndIndex = np.random.permutation(dataSize)
# trainX,trainY = trainX[rndIndex],trainY[rndIndex]
numRand = np.random.randint(0,trainX.shape[0]-self.batchSize-1)
batchDataX = trainX[numRand:numRand+self.batchSize]
batchDataY = trainY[numRand:numRand+self.batchSize]
activations = []
activations.append(batchDataX)
for l in self.layers:
activations.append(l.forwardpass(activations[-1]))
L = self.crossEntropyLoss(batchDataY,activations[-1])
# prediction = predict(batchDataX)
# print(L)
delta = self.crossEntropyDelta(batchDataY,activations[-1])
for l in reversed(self.layers):
prev_activation = activations[self.layers.index(l)]
delta = l.backwardpass(prev_activation,delta)
l.updateWeights(0.01)
l.updateWeights(self.lr)
# The methods trains the weights and baises using the training data(trainX, trainY)
# Feel free to print accuracy at different points using the validate() or computerAccuracy() functions of this class
###############################################
......@@ -130,8 +131,8 @@ class FullyConnectedLayer:
###############################################
# TASK 1a (Marks 0) - YOUR CODE HERE
# raise NotImplementedError
self.weights = np.random.randn(in_nodes,out_nodes)/np.sqrt(in_nodes)
self.biases = np.zeros(out_nodes)
self.weights = np.random.randn(in_nodes,out_nodes)*1e-6
self.biases = np.zeros(out_nodes)*1e-6
###############################################
# NOTE: You must NOT change the above code but you can add extra variables if necessary
......@@ -173,7 +174,7 @@ class FullyConnectedLayer:
###############################################
# TASK 1c (Marks 3) - YOUR CODE HERE
exps = np.exp(X)
return exps / np.sum(exps)
return exps / np.sum(exps,axis=1,keepdims=True)
raise NotImplementedError
###############################################
......@@ -186,11 +187,8 @@ class FullyConnectedLayer:
# Hint: You might need to compute Jacobian first
###############################################
# TASK 1f (Marks 7) - YOUR CODE HERE
nonZero = delta[delta!=0]
indexList = []
for i in range(len(delta)):
temp = list(delta[i])
indexList.append(temp.index(nonZero[i]))
indexList = np.nonzero(delta)[1].reshape(-1)
for i in range(len(indexList)):
temp = X[i][indexList[i]]
X[i] = -temp*X[i]
......@@ -251,8 +249,8 @@ class FullyConnectedLayer:
# This function should actually update the weights using the gradients computed in the backwardpass
###############################################
# TASK 1h (Marks 2) - YOUR CODE HERE
self.weights = self.weights - lr*(self.weightsGrad)
self.biases = self.biases - lr*(self.biasesGrad)
self.weights += lr*(self.weightsGrad)
self.biases += lr*(self.biasesGrad)
# raise NotImplementedError
###############################################
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment