Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
ML725
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
SHREYANSH JAIN
ML725
Commits
d768e8d6
Commit
d768e8d6
authored
Oct 08, 2019
by
SHREYANSH JAIN
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added on kaggle with 96.8
parent
2c147717
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
23 deletions
+22
-23
Assignment2/main.py
Assignment2/main.py
+6
-5
Assignment2/nn.py
Assignment2/nn.py
+16
-18
No files found.
Assignment2/main.py
View file @
d768e8d6
...
...
@@ -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.0
1
,
256
,
5
0
lr
,
batchSize
,
epochs
=
0.0
05
,
256
,
1000
0
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_layer
1
,
output_layer
,
'softmax'
))
###############################################
# TASK 3b (Marks 13) - YOUR CODE HERE
# raise NotImplementedError
...
...
Assignment2/nn.py
View file @
d768e8d6
...
...
@@ -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
###############################################
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment