mirror of
https://github.com/wassname/ray.git
synced 2026-08-11 11:24:51 +08:00
enable running example apps in cluster mode (#357)
This commit is contained in:
committed by
Philipp Moritz
parent
feee1de56f
commit
13df8302e6
@@ -64,7 +64,7 @@ def generate_random_params():
|
||||
results = []
|
||||
for _ in range(100):
|
||||
randparams = generate_random_params()
|
||||
results.append((randparams, train_cnn_and_compute_accuracy(randparams, epochs)))
|
||||
results.append((randparams, train_cnn_and_compute_accuracy(randparams, train_images, train_labels, validation_images, validation_labels)))
|
||||
```
|
||||
|
||||
Then we can inspect the contents of `results` and see which set of
|
||||
@@ -105,7 +105,7 @@ computation. Instead, it simply submits a number of tasks to the scheduler.
|
||||
result_ids = []
|
||||
for _ in range(100):
|
||||
params = generate_random_params()
|
||||
results.append((params, train_cnn_and_compute_accuracy.remote(params, epochs)))
|
||||
results.append((params, train_cnn_and_compute_accuracy.remote(params, train_images, train_labels, validation_images, validation_labels)))
|
||||
```
|
||||
|
||||
If we wish to wait until the results have all been retrieved, we can retrieve
|
||||
|
||||
@@ -2,20 +2,34 @@
|
||||
# https://www.tensorflow.org/versions/r0.9/tutorials/mnist/pros/index.html#build-a-multilayer-convolutional-network
|
||||
import numpy as np
|
||||
import ray
|
||||
import os
|
||||
import argparse
|
||||
|
||||
import tensorflow as tf
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
|
||||
import hyperopt
|
||||
|
||||
parser = argparse.ArgumentParser(description="Run the hyperparameter optimization example.")
|
||||
parser.add_argument("--node-ip-address", default=None, type=str, help="The IP address of this node.")
|
||||
parser.add_argument("--scheduler-address", default=None, type=str, help="The address of the scheduler.")
|
||||
parser.add_argument("--trials", default=2, type=int, help="The number of random trials to do.")
|
||||
parser.add_argument("--steps", default=10, type=int, help="The number of steps of training to do per network.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
ray.init(start_ray_local=True, num_workers=3)
|
||||
args = parser.parse_args()
|
||||
|
||||
# If node_ip_address and scheduler_address are provided, then this command
|
||||
# will connect the driver to the existing scheduler. If not, it will start
|
||||
# a local scheduler and connect to it.
|
||||
ray.init(start_ray_local=(args.node_ip_address is None),
|
||||
node_ip_address=args.node_ip_address,
|
||||
scheduler_address=args.scheduler_address,
|
||||
num_workers=(10 if args.node_ip_address is None else None))
|
||||
|
||||
# The number of sets of random hyperparameters to try.
|
||||
trials = 2
|
||||
trials = args.trials
|
||||
# The number of training passes over the dataset to use for network.
|
||||
epochs = 10
|
||||
steps = args.steps
|
||||
|
||||
# Load the mnist data and turn the data into remote objects.
|
||||
print "Downloading the MNIST dataset. This may take a minute."
|
||||
@@ -37,7 +51,7 @@ if __name__ == "__main__":
|
||||
dropout = np.random.uniform(0, 1)
|
||||
stddev = 10 ** np.random.uniform(-5, 5)
|
||||
params = {"learning_rate": learning_rate, "batch_size": batch_size, "dropout": dropout, "stddev": stddev}
|
||||
results.append((params, hyperopt.train_cnn_and_compute_accuracy.remote(params, epochs, train_images, train_labels, validation_images, validation_labels)))
|
||||
results.append((params, hyperopt.train_cnn_and_compute_accuracy.remote(params, steps, train_images, train_labels, validation_images, validation_labels)))
|
||||
|
||||
# Fetch the results of the tasks and print the results.
|
||||
for i in range(trials):
|
||||
|
||||
@@ -52,7 +52,7 @@ def cnn_setup(x, y, keep_prob, lr, stddev):
|
||||
# Define a remote function that takes a set of hyperparameters as well as the
|
||||
# data, consructs and trains a network, and returns the validation accuracy.
|
||||
@ray.remote([dict, int, np.ndarray, np.ndarray, np.ndarray, np.ndarray], [float])
|
||||
def train_cnn_and_compute_accuracy(params, epochs, train_images, train_labels, validation_images, validation_labels):
|
||||
def train_cnn_and_compute_accuracy(params, steps, train_images, train_labels, validation_images, validation_labels):
|
||||
# Extract the hyperparameters from the params dictionary.
|
||||
learning_rate = params["learning_rate"]
|
||||
batch_size = params["batch_size"]
|
||||
@@ -68,7 +68,7 @@ def train_cnn_and_compute_accuracy(params, epochs, train_images, train_labels, v
|
||||
with tf.Session() as sess:
|
||||
# Initialize the network weights.
|
||||
sess.run(tf.initialize_all_variables())
|
||||
for i in range(1, epochs):
|
||||
for i in range(1, steps + 1):
|
||||
# Fetch the next batch of data.
|
||||
image_batch = get_batch(train_images, i, batch_size)
|
||||
label_batch = get_batch(train_labels, i, batch_size)
|
||||
@@ -82,7 +82,7 @@ def train_cnn_and_compute_accuracy(params, epochs, train_images, train_labels, v
|
||||
if train_ac < 0.25:
|
||||
# Compute the validation accuracy and return.
|
||||
totalacc = accuracy.eval(feed_dict={x: validation_images, y: validation_labels, keep_prob: 1.0})
|
||||
return totalacc
|
||||
return float(totalacc)
|
||||
# Training is done, compute the validation accuracy and return.
|
||||
totalacc = accuracy.eval(feed_dict={x: validation_images, y: validation_labels, keep_prob: 1.0})
|
||||
return float(totalacc)
|
||||
|
||||
Reference in New Issue
Block a user