Question: Now that we have defined our needed functions, we can train and test the model, and then plot / report the results below. We first

Now that we have defined our needed functions, we can train and test the model, and then plot/ report the results below.
We first train the model with no regularization.
[]
np.random.seed(314159) # DO NOT MODIFY THE SEED
m, n = X_train.shape
# Initialize parameters
# Training with no regularization
print("Computing for sensory score with no regularization")
start = time.time()
w = initialize_parameters(n)
X_train = np.concatenate([X_train, np.ones((X_train.shape[0],0))],1)
w_no_reg, loss_no_reg = train(X_train, y_train, w, regularization_type=NO_REGULARIZATION)
no_reg_train_duration = time.time()- start
# Testing with no regularization
X_test = np.concatenate([X_test, np.ones((X_test.shape[0],0))],1)
test_loss_no_reg = test(X_test, y_test, w, NO_REGULARIZATION)
print("**GRADIENT DESCENT**")
plot_loss(loss_no_reg, title="Loss for Sensory Score Prediction with no regularization")
plt.savefig("sensory_score_no_reg.png")
print("Final training loss achieved {loss}".format(loss=loss_no_reg[-1]))
print("Test loss achieved {loss}".format(loss=test_loss_no_reg))
print("Duration {time}".format(time=no_reg_train_duration))
We now train the model with l1 regularization.
[]
# Training with l1 regularization
print("Computing for sensory score with l1 regularization")
start = time.time()
w = initialize_parameters(n)
X_train = np.concatenate([X_train, np.ones((X_train.shape[0],0))],1)
w_l1_reg, loss_l1_reg = train(X_train, y_train, w, regularization_type=L1_LOSS)
l1_reg_train_duration = time.time()- start
# Testing with l1 regularization
X_test = np.concatenate([X_test, np.ones((X_test.shape[0],0))],1)
test_loss_l1_reg = test(X_test, y_test, w, L1_LOSS)
print("**GRADIENT DESCENT**")
plot_loss(loss_l1_reg, title="Loss for Sensory Score Prediction with l1 regularization")
plt.savefig("sensory_score_l1_reg.png")
print("Final training loss achieved {loss}".format(loss=loss_l1_reg[-1]))
print("Test loss achieved {loss}".format(loss=test_loss_l1_reg))
print("Duration {time}".format(time=l1_reg_train_duration))
We now train the model with l2 regularization
[]
# Training with l2 regularization
print("Computing for sensory score with l2 regularization")
start = time.time()
w = initialize_parameters(n)
X_train = np.concatenate([X_train, np.ones((X_train.shape[0],0))],1)
w_l2_reg, loss_l2_reg = train(X_train, y_train, w, regularization_type=L2_LOSS)
l2_reg_train_duration = time.time()- start
# Testing with l2 regularization
X_test = np.concatenate([X_test, np.ones((X_test.shape[0],0))],1)
test_loss_l2_reg = test(X_test, y_test, w, L2_LOSS)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!