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
bbf7342c
Commit
bbf7342c
authored
Sep 07, 2019
by
SHREYANSH JAIN
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added mean square gradient
parent
e0cb7c11
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
11555 additions
and
48 deletions
+11555
-48
Assignment1/error.log
Assignment1/error.log
+8000
-0
Assignment1/main.py
Assignment1/main.py
+40
-48
Assignment1/output.csv
Assignment1/output.csv
+3515
-0
No files found.
Assignment1/error.log
0 → 100644
View file @
bbf7342c
This diff is collapsed.
Click to expand it.
Assignment1/main.py
View file @
bbf7342c
import
numpy
as
np
import
argparse
import
csv
import
matplotlib.pyplot
as
plt
'''
You are only required to fill the following functions
mean_squared_loss
...
...
@@ -21,22 +21,19 @@ Don't modify function declaration (arguments)
'''
def
mean_squared_loss
(
xdata
,
ydata
,
weights
):
'''
weights = weight vector [D X 1]
xdata = input feature matrix [N X D]
ydata = output values [N X 1]
Return the mean squared loss
'''
guess
=
np
.
dot
(
xdata
,
weights
)
samples
=
np
.
shape
(
guess
)[
0
]
err
=
0.5
*
samples
*
np
.
sum
(
np
.
square
(
ydata
.
T
-
guess
))
return
err
raise
NotImplementedError
def
mean_squared_gradient
(
xdata
,
ydata
,
weights
):
'''
weights = weight vector [D X 1]
xdata = input feature matrix [N X D]
ydata = output values [N X 1]
Return the mean squared gradient
'''
samples
=
np
.
shape
(
xdata
)[
0
]
guess
=
np
.
dot
(
xdata
,
weights
)
gradient
=
(
1
/
samples
)
*
np
.
dot
(
xdata
.
T
,(
guess
-
ydata
.
T
))
return
gradient
raise
NotImplementedError
...
...
@@ -68,29 +65,25 @@ class LinearRegressor:
def
__init__
(
self
,
dims
):
# dims is the number of the feature
s
# You can use __init__ to initialise your weight and biases
# Create all class related variables here
self
.
dims
=
dim
s
self
.
W
=
np
.
zeros
(
dims
)
return
raise
NotImplementedError
def
train
(
self
,
xtrain
,
ytrain
,
loss_function
,
gradient_function
,
epoch
=
100
,
lr
=
1.0
):
'''
xtrain = input feature matrix [N X D]
ytrain = output values [N X 1]
learn weight vector [D X 1]
epoch = scalar parameter epoch
lr = scalar parameter learning rate
loss_function = loss function name for linear regression training
gradient_function = gradient name of loss function
'''
# You need to write the training loop to update weights here
def
train
(
self
,
xtrain
,
ytrain
,
loss_function
,
gradient_function
,
epoch
=
100
,
lr
=
1
):
errlog
=
[]
samples
=
np
.
shape
(
xtrain
)[
0
]
for
iterations
in
range
(
epoch
):
self
.
W
=
self
.
W
-
lr
*
gradient_function
(
xtrain
,
ytrain
,
self
.
W
)
errlog
.
append
(
loss_function
(
xtrain
,
ytrain
,
self
.
W
))
return
errlog
raise
NotImplementedError
def
predict
(
self
,
xtest
):
# This returns your prediction on xtest
return
np
.
dot
(
xtest
,
self
.
W
)
raise
NotImplementedError
...
...
@@ -120,18 +113,14 @@ def read_dataset(trainfile, testfile):
return
np
.
array
(
xtrain
),
np
.
array
(
ytrain
),
np
.
array
(
xtest
)
def
preprocess_dataset
(
xdata
,
ydata
=
None
):
'''
xdata = input feature matrix [N X D]
ydata = output values [N X 1]
Convert data xdata, ydata obtained from read_dataset() to a usable format by loss function
The ydata argument is optional so this function must work for the both the calls
xtrain_processed, ytrain_processed = preprocess_dataset(xtrain,ytrain)
xtest_processed = preprocess_dataset(xtest)
NOTE: You can ignore/drop few columns. You can feature scale the input data before processing further.
'''
xdata
=
xdata
[:,[
2
,
3
,
4
,
7
,
9
]]
xdata
=
xdata
.
astype
(
'float32'
)
bias
=
np
.
ones
((
np
.
shape
(
xdata
)[
0
],
1
))
xdata
=
np
.
concatenate
((
bias
,
xdata
),
axis
=
1
)
if
ydata
is
None
:
return
xdata
ydata
=
ydata
.
astype
(
'float32'
)
return
xdata
,
ydata
raise
NotImplementedError
dictionary_of_losses
=
{
...
...
@@ -147,17 +136,20 @@ def main():
# Uncomment the below lines and pass the appropriate value
xtrain
,
ytrain
,
xtest
=
read_dataset
(
args
.
train_file
,
args
.
test_file
)
#
xtrainprocessed, ytrainprocessed = preprocess_dataset(xtrain, ytrain)
#
xtestprocessed = preprocess_dataset(xtest)
xtrainprocessed
,
ytrainprocessed
=
preprocess_dataset
(
xtrain
,
ytrain
)
xtestprocessed
=
preprocess_dataset
(
xtest
)
# model = LinearRegressor(FILL HERE
)
model
=
LinearRegressor
(
np
.
shape
(
xtrainprocessed
)[
1
]
)
# The loss function is provided by command line argument
loss_fn
,
loss_grad
=
dictionary_of_losses
[
args
.
loss
]
# model.train(xtrainprocessed, ytrainprocessed, loss_fn, loss_grad, args.epoch, args.lr)
# ytest = model.predict(xtestprocessed)
errlog
=
model
.
train
(
xtrainprocessed
,
ytrainprocessed
,
loss_fn
,
loss_grad
,
args
.
epoch
,
args
.
lr
)
ytest
=
model
.
predict
(
xtestprocessed
)
ytest
=
ytest
.
astype
(
'int'
)
output
=
[(
i
,
np
.
absolute
(
ytest
[
i
]))
for
i
in
range
(
len
(
ytest
))]
np
.
savetxt
(
"output.csv"
,
output
,
delimiter
=
','
,
fmt
=
"
%
d"
,
header
=
"instance (id),count"
,
comments
=
''
)
np
.
savetxt
(
"error.log"
,
errlog
,
delimiter
=
'
\n
'
,
fmt
=
"
%
f"
)
if
__name__
==
'__main__'
:
...
...
Assignment1/output.csv
0 → 100644
View file @
bbf7342c
This diff is collapsed.
Click to expand it.
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