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 numpy as np
import argparse import argparse
import csv import csv
import matplotlib.pyplot as plt
''' '''
You are only required to fill the following functions You are only required to fill the following functions
mean_squared_loss mean_squared_loss
...@@ -21,22 +21,19 @@ Don't modify function declaration (arguments) ...@@ -21,22 +21,19 @@ Don't modify function declaration (arguments)
''' '''
def mean_squared_loss(xdata, ydata, weights): def mean_squared_loss(xdata, ydata, weights):
'''
weights = weight vector [D X 1] guess = np.dot(xdata,weights)
xdata = input feature matrix [N X D] samples = np.shape(guess)[0]
ydata = output values [N X 1] err = 0.5*samples*np.sum(np.square(ydata.T-guess))
Return the mean squared loss return err
'''
raise NotImplementedError raise NotImplementedError
def mean_squared_gradient(xdata, ydata, weights): def mean_squared_gradient(xdata, ydata, weights):
'''
weights = weight vector [D X 1] samples = np.shape(xdata)[0]
xdata = input feature matrix [N X D] guess = np.dot(xdata,weights)
ydata = output values [N X 1] gradient = (1/samples)*np.dot(xdata.T,(guess-ydata.T))
Return the mean squared gradient return gradient
'''
raise NotImplementedError raise NotImplementedError
...@@ -68,29 +65,25 @@ class LinearRegressor: ...@@ -68,29 +65,25 @@ class LinearRegressor:
def __init__(self,dims): def __init__(self,dims):
# dims is the number of the features self.dims = dims
# You can use __init__ to initialise your weight and biases self.W = np.zeros(dims)
# Create all class related variables here return
raise NotImplementedError raise NotImplementedError
def train(self, xtrain, ytrain, loss_function, gradient_function, epoch=100, lr=1.0): def train(self, xtrain, ytrain, loss_function, gradient_function, epoch=100, lr=1):
''' errlog = []
xtrain = input feature matrix [N X D] samples = np.shape(xtrain)[0]
ytrain = output values [N X 1] for iterations in range(epoch):
learn weight vector [D X 1] self.W = self.W - lr*gradient_function(xtrain,ytrain,self.W)
epoch = scalar parameter epoch errlog.append(loss_function(xtrain,ytrain,self.W))
lr = scalar parameter learning rate return errlog
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
raise NotImplementedError raise NotImplementedError
def predict(self, xtest): def predict(self, xtest):
# This returns your prediction on xtest # This returns your prediction on xtest
return np.dot(xtest,self.W)
raise NotImplementedError raise NotImplementedError
...@@ -120,18 +113,14 @@ def read_dataset(trainfile, testfile): ...@@ -120,18 +113,14 @@ def read_dataset(trainfile, testfile):
return np.array(xtrain), np.array(ytrain), np.array(xtest) return np.array(xtrain), np.array(ytrain), np.array(xtest)
def preprocess_dataset(xdata, ydata=None): def preprocess_dataset(xdata, ydata=None):
''' xdata = xdata[:,[2,3,4,7,9]]
xdata = input feature matrix [N X D] xdata = xdata.astype('float32')
ydata = output values [N X 1] bias = np.ones((np.shape(xdata)[0],1))
Convert data xdata, ydata obtained from read_dataset() to a usable format by loss function xdata = np.concatenate((bias,xdata),axis=1)
if ydata is None:
The ydata argument is optional so this function must work for the both the calls return xdata
xtrain_processed, ytrain_processed = preprocess_dataset(xtrain,ytrain) ydata = ydata.astype('float32')
xtest_processed = preprocess_dataset(xtest) return xdata,ydata
NOTE: You can ignore/drop few columns. You can feature scale the input data before processing further.
'''
raise NotImplementedError raise NotImplementedError
dictionary_of_losses = { dictionary_of_losses = {
...@@ -147,17 +136,20 @@ def main(): ...@@ -147,17 +136,20 @@ def main():
# Uncomment the below lines and pass the appropriate value # Uncomment the below lines and pass the appropriate value
xtrain, ytrain, xtest = read_dataset(args.train_file, args.test_file) xtrain, ytrain, xtest = read_dataset(args.train_file, args.test_file)
# xtrainprocessed, ytrainprocessed = preprocess_dataset(xtrain, ytrain) xtrainprocessed, ytrainprocessed = preprocess_dataset(xtrain, ytrain)
# xtestprocessed = preprocess_dataset(xtest) xtestprocessed = preprocess_dataset(xtest)
# model = LinearRegressor(FILL HERE) model = LinearRegressor(np.shape(xtrainprocessed)[1])
# The loss function is provided by command line argument # The loss function is provided by command line argument
loss_fn, loss_grad = dictionary_of_losses[args.loss] loss_fn, loss_grad = dictionary_of_losses[args.loss]
# model.train(xtrainprocessed, ytrainprocessed, loss_fn, loss_grad, args.epoch, args.lr) errlog = model.train(xtrainprocessed, ytrainprocessed, loss_fn, loss_grad, args.epoch, args.lr)
ytest = model.predict(xtestprocessed)
# 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__': 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