-
Notifications
You must be signed in to change notification settings - Fork 1
/
spearmint-tuner.py
48 lines (33 loc) · 1.51 KB
/
spearmint-tuner.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
35
36
37
38
39
40
41
42
43
44
# function that is called by spearmint. This calls the classifier and returns the value that needs to be minimized
# you may choose to return either the loss or the error on the validation set.
# we always do this :/
import numpy as np
# import whichever classifier you need: LinearSVM/Softmax
# Uncomment below line for SVM
# from f15ece6504.classifiers import LinearSVM
# Uncomment below line for Softmax
# from f15ece6504.classifiers import Softmax
# see load_cifar10_tvt.py in the hw0/ folder. This code helps you get your data in a single go.
# This is basically a function that has all the steps you did in the ipython notebook to ready your data
# for the classifier
from load_cifar10_tvt import load_cifar10_train_val
def get_valError(learning_rate,reg):
# load data
X_train,y_train,X_val,y_val = load_cifar10_train_val()
# init the classifier you need
# Uncomment below line for SVM
# classifier = LinearSVM()
# Uncomment below line for Softmax
# classifier = Softmax()
# train classifier
loss_hist = classifier.train(X_train, y_train, learning_rate, reg, num_iters=1500, verbose=True)
# get validation error
y_val_pred = classifier.predict(X_val)
val_accuracy = np.mean(y_val == y_val_pred)
# return error rate
return (1 - val_accuracy)
# The main function interfaces the above function with spearmint.
def main(job_id, params):
print 'Anything printed here will end up in the output directory for job #%d' % job_id
print params
return get_valError(params['learning_rate'], params['reg'])