MCPcopy Index your code
hub / github.com/lazyprogrammer/machine_learning_examples / grid_search

Function grid_search

ann_class2/grid_search.py:18–61  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16
17
18def grid_search():
19 # get the data and split into train/test
20 X, Y = get_spiral()
21 # X, Y = get_clouds()
22 X, Y = shuffle(X, Y)
23 Ntrain = int(0.7*len(X))
24 Xtrain, Ytrain = X[:Ntrain], Y[:Ntrain]
25 Xtest, Ytest = X[Ntrain:], Y[Ntrain:]
26
27 # hyperparameters to try
28 hidden_layer_sizes = [
29 [300],
30 [100,100],
31 [50,50,50],
32 ]
33 learning_rates = [1e-4, 1e-3, 1e-2]
34 l2_penalties = [0., 0.1, 1.0]
35
36 # loop through all possible hyperparameter settings
37 best_validation_rate = 0
38 best_hls = None
39 best_lr = None
40 best_l2 = None
41 for hls in hidden_layer_sizes:
42 for lr in learning_rates:
43 for l2 in l2_penalties:
44 model = ANN(hls)
45 model.fit(Xtrain, Ytrain, learning_rate=lr, reg=l2, mu=0.99, epochs=3000, show_fig=False)
46 validation_accuracy = model.score(Xtest, Ytest)
47 train_accuracy = model.score(Xtrain, Ytrain)
48 print(
49 "validation_accuracy: %.3f, train_accuracy: %.3f, settings: %s, %s, %s" %
50 (validation_accuracy, train_accuracy, hls, lr, l2)
51 )
52 if validation_accuracy > best_validation_rate:
53 best_validation_rate = validation_accuracy
54 best_hls = hls
55 best_lr = lr
56 best_l2 = l2
57 print("Best validation_accuracy:", best_validation_rate)
58 print("Best settings:")
59 print("hidden_layer_sizes:", best_hls)
60 print("learning_rate:", best_lr)
61 print("l2:", best_l2)
62
63
64# def one():

Callers 1

grid_search.pyFile · 0.85

Calls 4

fitMethod · 0.95
scoreMethod · 0.95
get_spiralFunction · 0.90
ANNClass · 0.90

Tested by

no test coverage detected