Commit bbf7342c authored by SHREYANSH JAIN's avatar SHREYANSH JAIN

added mean square gradient

parent e0cb7c11
This diff is collapsed.
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 features
# You can use __init__ to initialise your weight and biases
# Create all class related variables here
self.dims = dims
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__':
......
This diff is collapsed.
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