Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Machine learning question, use jupyter lab, no AI plz: Import this: import sys from packaging import version import sklearn import matplotlib.pyplot as plt import numpy

Machine learning question, use jupyter lab, no AI plz:
Import this:
import sys
from packaging import version
import sklearn
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import add_dummy_feature
import numpy as np
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.datasets import make_blobs
print("Sklearn package",sys.version_info)
print("Sklearn package",sklearn.__version__)
assert sys.version_info >=(3,10)
#assert version.parse(sklearn.__version__)>= version.parse("1.2.1")
plt.rc('font', size=12)
plt.rc('axes', labelsize=14, titlesize=14)
plt.rc('legend', fontsize=14)
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=10)
-----
Questions:
4. Q2 Early Stopping
4.1. Modify the gradient descent code for linear regression so that the gradient descent stops updating the weights when the changes in the loss is too small.
4.2. You can stop updating when abs(new_loss - old_loss) is less than a tolerance threshold of 0.00000001, tolerance =1e-8.
def f(x,w):
return((x.T@w).item())
def J_vectorized(X,y,w):
m= X.shape[0]
V = X@w-y
sum_squared_error = V.T @ V
return ((sum_squared_error/(2*m)).item())
def J_delta_vectorized(X,y,w):
m= X.shape[0]
sum_w = X.T @ (X@w-y)
#return ((sum*2/m))
return ((sum_w/m))
def simple_gradient_vectorized(X,y,theta, n_epochs, eta):
theta_path =[theta]
for epoch in range(n_epochs):
gradients = J_delta_vectorized(X,y,theta)
theta = theta - eta * gradients
theta_path.append(theta)
return(theta_path)
np.random.seed(42) # to make this code example reproducible
m =500 # number of instances
X =2* np.random.rand(m,1) # column vector
y =4+3* X + np.random.randn(m,1) # column vector
X_orig = X
X = add_dummy_feature(X) # add x0=1 to each instance
print("Before adding a '1' X[0]=", X_orig[0])
print("After adding a '1' X[0]=", X[0])
plt.plot(X_orig, y,"b.")
plt.grid()
plt.xlim(0,2)
plt.ylim(2,12)
##Without early stopping
eta =0.01 # learning rate
n_epochs =5000
m = len(X) # number of instances
np.random.seed(42)
theta = np.random.randn(2,1) # randomly initialized model parameters
theta_path = simple_gradient_vectorized(X,y,theta,n_epochs, eta)
print("Loss at final theta",J_vectorized(X,y,theta_path[-1]))
print("Number of updates made",len(theta_path)-1)
def simple_gradient_vectorized_early_stopping(X,y,theta, n_epochs, eta,tolerance):
theta_path =[theta]
current_loss = J_vectorized(X,y,theta) #initial loss
#iterate over n_epochs, update the gradient, calculate the new loss, break from the loop if
#the loss does not improve compared to the loss in the previous iteration.
#if(np.abs(current_loss-prev_loss)< tolerance)
return(theta_path)
4.3. The code should stop after around 2712 iterations
eta =0.01 # learning rate
n_epochs =5000
m = len(X) # number of instances
np.random.seed(42)
theta = np.random.randn(2,1) # randomly initialized model parameters
theta_path = simple_gradient_vectorized_early_stopping(X,y,theta,n_epochs, eta,1e-8)
print("Loss at final theta",J_vectorized(X,y,theta_path[-1]))
print("Number of updates made",len(theta_path)-1)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions