-
Notifications
You must be signed in to change notification settings - Fork 0
/
validationCurve.py
34 lines (29 loc) · 1.42 KB
/
validationCurve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
import numpy as np
from scipy import optimize as op
import matplotlib.pyplot as plt
import cost
def generateValidation(X_train, y_train, X_val, y_val, initial_params, input_layer_size, hidden_layer_size, num_labels):
lambda_vec = [0.0, 3.0, 5.0, 7.0, 8.0, 10.0, 15.0, 20.0]
error_train = []
error_val = []
for i in range(0, len(lambda_vec)):
args = (input_layer_size, hidden_layer_size, num_labels, X_train, y_train, lambda_vec[i])
#get the corresponding theta
theta = op.fmin_cg(cost.nn_cost, x0 = initial_params, fprime = cost.backprop, args = args, maxiter = 150)
#get the error w/ that theta on training set
ith_error_train = cost.nn_cost(theta, input_layer_size, hidden_layer_size, num_labels, X_train, y_train, 0)
error_train.append(ith_error_train)
#get the error w/ that theta on validation set
ith_error_val = cost.nn_cost(theta, input_layer_size, hidden_layer_size, num_labels, X_val, y_val, 0)
error_val.append(ith_error_val)
print(error_train)
print('break')
print(error_val)
plt.plot(lambda_vec, error_train, 'r', label = "Training Error")
plt.plot(lambda_vec, error_val, 'b', label = "Validation Error")
plt.xlabel('lambda')
plt.ylabel('error')
plt.title('Validation Curve -- Automating Lambda Selection')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show()