Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please write the code without using packages 1. Impiementation of the LinearRegression class Before evaluating any data, we need some code to actually fit, predict,
please write the code without using packages
1. Impiementation of the LinearRegression class Before evaluating any data, we need some code to actually fit, predict, and score samples. This will be implemented in LinearRegression.py provided in this repository. The skeleton of the class is already there. In part 1 , you will need to implement the fit, predict, and score functions. After implementing these 3 functions, you will be able to use this model simply with any regression task. 1.2 The fit method The fit method should accept 6 parameters: 1. the input data 2. the target values 3. batch_size, int - The size of each batch during training 4. regularization, int - The factor of L2 regularization to add, default to 0 5. max_epochs, int - The maximum number of times the model should train through the entire training set 6. patience, int - The number of epochs to wait for the validation set to decrease Other parameters can be added as long as they are optional. This method should use gradient descent to optimize the model parameters using mean squared error as the loss function. So that the model will converge to a solution, early stopping must be used. To do this, set aside 10% of the training data as a validation set. After each step of gradient descent, evaluate the loss on the validation set. If the loss on the validation set increases for 3 consecutive steps, stop training. If it decreases, save the current model parameters. After training is complete, used the saved parameters to set the model parameters. 1.3 The predict method The predict method should accept 1 parameter: 1. the input data This method should run a forward pass of the model and return the predicted values. Given n samples with d features each and m output values, let XRnd be the input data, WRdm be the model parameters, and bRnm be the bias terms. The predicted values are given by: y=XW+bRnm 1.4 The score method The score method should accept 2 parameters: 1. the input data 2. the target values This method will predict the values for the input data and then compute the mean squared error between the predicted values and the target values. The mean squared error is given by: MSE=nm1i=1n(yiy^i)2 where n is the number of samples, m is the output size, yi is the target value for the i hat (y}i $ is the predicted value for the $is th sampleStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started