mirror of
https://github.com/vale981/ray
synced 2025-03-06 10:31:39 -05:00
Merge pull request #151 from amplab/hyper
Hyper-parameter Optimization code
This commit is contained in:
commit
3e7fe2e4bd
3 changed files with 125 additions and 0 deletions
39
examples/hyperopt/driver.py
Normal file
39
examples/hyperopt/driver.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
import numpy as np
|
||||
import ray
|
||||
import ray.services as services
|
||||
import os
|
||||
|
||||
import functions
|
||||
|
||||
num_workers = 3
|
||||
samples = 50
|
||||
epochs = 100
|
||||
|
||||
worker_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
worker_path = os.path.join(worker_dir, "worker.py")
|
||||
services.start_singlenode_cluster(return_drivers=False, num_objstores=1, num_workers_per_objstore=num_workers, worker_path=worker_path)
|
||||
|
||||
best_params = None
|
||||
best_accuracy = 0
|
||||
|
||||
results = []
|
||||
|
||||
for i in range(samples):
|
||||
learning_rate = 10 ** np.random.uniform(-6, 1)
|
||||
batch_size = np.random.randint(30, 100)
|
||||
dropout = np.random.uniform(0, 1)
|
||||
stddev = 10 ** np.random.uniform(-3, 1)
|
||||
randparams = {"learning_rate": learning_rate, "batch_size": batch_size, "dropout": dropout, "stddev": stddev}
|
||||
results.append((randparams, functions.train_cnn(randparams, epochs)))
|
||||
|
||||
for i in range(samples):
|
||||
params, ref = results[i]
|
||||
accuracy = ray.get(ref)
|
||||
print "With hyperparameters {}, we achieve an accuracy of {:.4}%.".format(params, 100 * accuracy)
|
||||
if accuracy > best_accuracy:
|
||||
best_params = params
|
||||
best_accuracy = accuracy
|
||||
print "Best parameters are now {}.".format(params)
|
||||
|
||||
print "Best parameters over {} samples was {}, with an accuracy of {:.4}%.".format(samples, best_params, 100 * best_accuracy)
|
||||
services.cleanup()
|
70
examples/hyperopt/functions.py
Normal file
70
examples/hyperopt/functions.py
Normal file
|
@ -0,0 +1,70 @@
|
|||
# Most of the tensorflow code is adapted from Tensorflow's tutorial on using CNNs to train MNIST
|
||||
# https://www.tensorflow.org/versions/r0.9/tutorials/mnist/pros/index.html#build-a-multilayer-convolutional-network
|
||||
import tensorflow as tf
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
import numpy as np
|
||||
import ray
|
||||
|
||||
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
|
||||
|
||||
def weight(shape, stddev):
|
||||
initial = tf.truncated_normal(shape, stddev=stddev)
|
||||
return tf.Variable(initial)
|
||||
|
||||
def bias(shape):
|
||||
initial = tf.constant(0.1, shape=shape)
|
||||
return tf.Variable(initial)
|
||||
|
||||
def conv2d(x, W):
|
||||
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME")
|
||||
|
||||
def max_pool_2x2(x):
|
||||
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
|
||||
|
||||
@ray.remote([dict, int], [float])
|
||||
def train_cnn(params, epochs):
|
||||
learning_rate = params["learning_rate"]
|
||||
batch_size = params["batch_size"]
|
||||
keep = 1 - params["dropout"]
|
||||
stddev = params["stddev"]
|
||||
x = tf.placeholder(tf.float32, shape=[None, 784])
|
||||
y = tf.placeholder(tf.float32, shape=[None, 10])
|
||||
keep_prob = tf.placeholder(tf.float32)
|
||||
train_step, accuracy = cnn_setup(x, y, keep_prob, learning_rate, stddev)
|
||||
with tf.Session() as sess:
|
||||
sess.run(tf.initialize_all_variables())
|
||||
for i in range(1, epochs):
|
||||
batch = mnist.train.next_batch(batch_size)
|
||||
sess.run(train_step, feed_dict={x: batch[0], y: batch[1], keep_prob: keep})
|
||||
if i % 100 == 0: # checks if accuracy is low enough to stop early every set number of epochs
|
||||
train_ac = accuracy.eval(feed_dict={x: batch[0], y: batch[1], keep_prob: 1.0})
|
||||
if train_ac < 0.25: # Accuracy threshold is on a application to application basis.
|
||||
totalacc = accuracy.eval(feed_dict={x: mnist.validation.images, y: mnist.validation.labels, keep_prob: 1.0})
|
||||
return totalacc
|
||||
totalacc = accuracy.eval(feed_dict={x: mnist.validation.images, y: mnist.validation.labels, keep_prob: 1.0})
|
||||
return totalacc
|
||||
|
||||
def cnn_setup(x, y, keep_prob, lr, stddev):
|
||||
first_hidden = 32
|
||||
second_hidden = 64
|
||||
fc_hidden = 1024
|
||||
W_conv1 = weight([5, 5, 1, first_hidden], stddev)
|
||||
B_conv1 = bias([first_hidden])
|
||||
x_image = tf.reshape(x, [-1, 28, 28, 1])
|
||||
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + B_conv1)
|
||||
h_pool1 = max_pool_2x2(h_conv1)
|
||||
W_conv2 = weight([5, 5, first_hidden, second_hidden], stddev)
|
||||
b_conv2 = bias([second_hidden])
|
||||
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
|
||||
h_pool2 = max_pool_2x2(h_conv2)
|
||||
W_fc1 = weight([7 * 7 * second_hidden, fc_hidden], stddev)
|
||||
b_fc1 = bias([fc_hidden])
|
||||
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * second_hidden])
|
||||
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
|
||||
h_fc1_drop= tf.nn.dropout(h_fc1, keep_prob)
|
||||
W_fc2 = weight([fc_hidden, 10], stddev)
|
||||
b_fc2 = bias([10])
|
||||
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
|
||||
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_conv), reduction_indices=[1]))
|
||||
correct_pred = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))
|
||||
return tf.train.AdamOptimizer(lr).minimize(cross_entropy), tf.reduce_mean(tf.cast(correct_pred, tf.float32))
|
16
examples/hyperopt/worker.py
Normal file
16
examples/hyperopt/worker.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
import argparse
|
||||
import ray
|
||||
import ray.worker as worker
|
||||
|
||||
import functions
|
||||
|
||||
parser = argparse.ArgumentParser(description="Parse addresses for the worker to connect to.")
|
||||
parser.add_argument("--scheduler-address", default="127.0.0.1:10001", type=str, help="the scheduler's address")
|
||||
parser.add_argument("--objstore-address", default="127.0.0.1:20001", type=str, help="the objstore's address")
|
||||
parser.add_argument("--worker-address", default="127.0.0.1:40001", type=str, help="the worker's address")
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = parser.parse_args()
|
||||
ray.connect(args.scheduler_address, args.objstore_address, args.worker_address)
|
||||
ray.register_module(functions)
|
||||
worker.main_loop()
|
Loading…
Add table
Reference in a new issue