Reputation: 15
<p><pre>Initialize the weight_vector and intercept term to zeros (Write your code in def initialize_weights())
Create a loss function (Write your code in def logloss())
logloss=−1∗1nΣforeachYt,Ypred(Ytlog10(Ypred)+(1−Yt)log10(1−Ypred))
for each epoch:
for each batch of data points in train: (keep batch size=1)
calculate the gradient of loss function w.r.t each weight in weight vector (write your code in def gradient_dw())
dw(t)=xn(yn−σ((w(t))Txn+bt))−λNw(t))
Calculate the gradient of the intercept (write your code in def gradient_db()) check this
db(t)=yn−σ((w(t))Txn+bt))
Update weights and intercept (check the equation number 32 in the above mentioned pdf):
w(t+1)←w(t)+α(dw(t))
b(t+1)←b(t)+α(db(t))
calculate the log loss for train and test with the updated weights (you can check the python assignment 10th question)
And if you wish, you can compare the previous loss and the current loss, if it is not updating, then you can stop the training
append this loss in the list ( this will be used to see how loss is changing for each epoch after the training is over )</pre></p>
importing libraries
import numpy as np
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn import linear_model
Creating custom dataset
X, y = make_classification(n_samples=50000, n_features=15, n_informative=10, n_redundant=5,
n_classes=2, weights=[0.7], class_sep=0.7, random_state=15)
Splitting data into train and test
# you need not standardize the data as it is already standardized
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=15)
def initialize_weights(row_vector):
''' In this function, we will initialize our weights and bias'''
#initialize the weights as 1d array consisting of all zeros similar to the dimensions of row_vector
#you use zeros_like function to initialize zero, check this link https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros_like.html
#initialize bias to zero
w = np.zeros_like(X_train[0])
b=0
return w,b
dim=X_train[0]
w,b = initialize_weights(dim)
print('w =',(w))
print('b =',str(b))
def sigmoid(z):
''' In this function, we will return sigmoid of z'''
# compute sigmoid(z) and return
return 1 /(1+np.exp(-z))
def logloss(y_true,y_pred):
# you have been given two arrays y_true and y_pred and you have to calculate the logloss
#while dealing with numpy arrays you can use vectorized operations for quicker calculations as compared to using loops
#https://www.pythonlikeyoumeanit.com/Module3_IntroducingNumpy/VectorizedOperations.html
#https://www.geeksforgeeks.org/vectorized-operations-in-numpy/
#write your code here
sum=0
for i in range(len(y_true)):
sum += (y_true[i] * np.log10(y_pred[i])) + ((1 - y_true[i]) * np.log10(1 - y_pred[i]))
loss = -1 * (1 / len(y_true)) * sum
return loss
#make sure that the sigmoid function returns a scalar value, you can use dot function operation
def gradient_dw(x,y,w,b,alpha,N):
'''In this function, we will compute the gardient w.r.to w '''
dw = x * (y - sigmoid(np.dot(w,x) + b) - (alpha / N) * w)
return dw
#sb should be a scalar value
def gradient_db(x,y,w,b):
'''In this function, we will compute gradient w.r.to b '''
db = y - sigmoid(np.dot(w,x)+ b)
return db
# prediction function used to compute predicted_y given the dataset X
def pred(w,b, X):
N = len(X)
predict = []
for i in range(N):
z=np.dot(w,X[i])+b
predict.append(sigmoid(z))
return np.array(predict)
def train(X_train,y_train,X_test,y_test,epochs,alpha,eta0):
''' In this function, we will implement logistic regression'''
#Here eta0 is learning rate
#implement the code as follows
# for every epoch
# for every data point(X_train,y_train)
#compute gradient w.r.to w (call the gradient_dw() function)
#compute gradient w.r.to b (call the gradient_db() function)
#update w, b
# predict the output of x_train [for all data points in X_train] using pred function with updated weights
#compute the loss between predicted and actual values (call the loss function)
# store all the train loss values in a list
# predict the output of x_test [for all data points in X_test] using pred function with updated weights
#compute the loss between predicted and actual values (call the loss function)
# store all the test loss values in a list
# you can also compare previous loss and current loss, if loss is not updating then stop the process
# you have to return w,b , train_loss and test loss
train_loss = []
test_loss = []
# initalize the weights (call the initialize_weights(X_train[0]) function)
w,b = initialize_weights(X_train[0]) # Initialize the weights
#write your code to perform SGD
# for every epoch
for i in range(epochs):
train_pred = []
test_pred = []
# for every data point(X_train,y_train)
for j in range(N):
#compute gradient w.r.to w (call the gradient_dw() function)
dw= gradient_dw(X_train[j],y_train[j],w,b,alpha,N)
#compute gradient w.r.to b (call the gradient_db() function)
db = gradient_db(X_train[j],y_train[j],w,b)
#update w, b
w= w+ (eta0* dw)
b =b+ (eta0* db)
#predict the output of x_train [for all data points in X_train] using pred function with updated weights
for k in range(0,N):
w1_predict = pred(w,b,X_train[k])
train_pred.append(w1_predict)
##compute the loss between predicted and actual values (call the loss function)
loss_pred1 = logloss(y_train,train_pred)
train_loss.append(loss_pred1)
# predict the output of x_test [for all data points in X_test] using pred function with updated weights
for k in range(len(X_test)):
w2_predict = pred(w,b,X_test[k])
test_pred.append(w2_predict)
#compute the loss between predicted and actual values (call the loss function)
loss_pred2 = logloss(y_test,test_pred)
test_loss.append(loss_pred2)
return w,b,train_loss,test_loss
alpha=0.001
eta0=0.001
N=len(X_train)
epochs=6
w,b,train_loss,test_loss=train(X_train,y_train,X_test,y_test,epochs,alpha,eta0)
from matplotlib import pyplot as plt
epoch = [i for i in range(1,epochs+1,1)]
plt.figure(figsize=(8,6))
plt.grid()
plt.plot(epoch,train_loss , label='train log loss')
plt.plot(epoch,test_loss, label='test log loss')
plt.xlabel("epoch number")
plt.ylabel("log loss")
plt.title("log loss curve of logistic regression")
plt.legend()
plt.show
I am getting this below error. I used grader function check all the function, its coming true. However. I am getting an below error while running the code. I tried use reshape to change the shape of train loss. still getting the error. Please help
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-28-52c352e46321> in <module>()
1 plt.figure(figsize=(8,6))
2 plt.grid()
----> 3 plt.plot(epoch,train_loss , label='train log loss')
4 plt.plot(epoch,test_loss, label='test log loss')
5 plt.xlabel("epoch number")
3 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
340
341 if x.shape[0] != y.shape[0]:
--> 342 raise ValueError(f"x and y must have same first dimension, but "
343 f"have shapes {x.shape} and {y.shape}")
344 if x.ndim > 2 or y.ndim > 2:
ValueError: x and y must have same first dimension, but have shapes (6,) and (1350,)
Upvotes: 0
Views: 114