Start working toward Python3 compatibility. (#117)

This commit is contained in:
Robert Nishihara
2016-12-11 12:25:31 -08:00
committed by Philipp Moritz
parent 3d083c8b58
commit ddba1df802
48 changed files with 206 additions and 103 deletions
+5 -1
View File
@@ -1,6 +1,10 @@
# The code for AlexNet is copied and adapted from the TensorFlow repository
# https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/alexnet/alexnet_benchmark.py.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import ray
import numpy as np
import tarfile, io
@@ -390,7 +394,7 @@ def shuffle(batches):
# Randomly permute the order of the batches.
permuted_batches = np.random.permutation(batches)
new_batches = []
for i in range(len(batches) / 2):
for i in range(len(batches) // 2):
# Swap data between consecutive batches.
shuffled_batch1, shuffled_batch2 = shuffle_pair(permuted_batches[2 * i], permuted_batches[2 * i + 1])
new_batches += [shuffled_batch1, shuffled_batch2]
+8 -4
View File
@@ -1,3 +1,7 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import ray
import os
@@ -28,7 +32,7 @@ if __name__ == "__main__":
imagenet_bucket = s3_resource.Bucket(args.s3_bucket)
objects = imagenet_bucket.objects.filter(Prefix=args.key_prefix)
image_tar_files = [str(obj.key) for obj in objects.all()]
print "Images will be downloaded from {} files.".format(len(image_tar_files))
print("Images will be downloaded from {} files.".format(len(image_tar_files)))
# Downloading the label file, and create a dictionary mapping the filenames of
# the images to their labels.
@@ -38,7 +42,7 @@ if __name__ == "__main__":
filename_label_pairs = [line.split(" ") for line in filename_label_str]
filename_label_dict = dict([(os.path.basename(name), label) for name, label in filename_label_pairs])
filename_label_dict_id = ray.put(filename_label_dict)
print "Labels extracted."
print("Labels extracted.")
# Download the imagenet dataset.
imagenet_data = alexnet.load_tarfiles_from_s3(args.s3_bucket, image_tar_files, [256, 256])
@@ -60,7 +64,7 @@ if __name__ == "__main__":
# Initialize the network and optimizer weights. This is only run once on the
# driver. We initialize the weights manually on the workers.
sess.run(init_all_variables)
print "Initialized network weights."
print("Initialized network weights.")
iteration = 0
while True:
@@ -82,7 +86,7 @@ if __name__ == "__main__":
gradient_ids.append(alexnet.compute_grad.remote(x_id, y_id, mean_id, weights_id))
# Print the accuracy on a random training batch.
print "Iteration {}: accuracy = {:.3}%".format(iteration, 100 * ray.get(accuracy))
print("Iteration {}: accuracy = {:.3}%".format(iteration, 100 * ray.get(accuracy)))
# Fetch the gradients. This blocks until the gradients have been computed.
gradient_sets = ray.get(gradient_ids)
+1 -1
View File
@@ -148,7 +148,7 @@ while len(remaining_ids) > 0:
ready_ids, remaining_ids = ray.wait(remaining_ids, num_returns=1)
# Get the accuracy corresponding to the ready object ID.
accuracy = ray.get(ready_ids[0])
print "Accuracy {}".format(accuracy)
print("Accuracy {}".format(accuracy))
```
Note that the above example does not associate the accuracy with the parameters
+10 -5
View File
@@ -1,5 +1,10 @@
# 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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import ray
import argparse
@@ -24,7 +29,7 @@ if __name__ == "__main__":
steps = args.steps
# Load the mnist data and turn the data into remote objects.
print "Downloading the MNIST dataset. This may take a minute."
print("Downloading the MNIST dataset. This may take a minute.")
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
train_images = ray.put(mnist.train.images)
train_labels = ray.put(mnist.train.labels)
@@ -65,20 +70,20 @@ if __name__ == "__main__":
result_id = ready_ids[0]
params = params_mapping[result_id]
accuracy = ray.get(result_id)
print """We achieve accuracy {:.3}% with
print("""We achieve accuracy {:.3}% with
learning_rate: {:.2}
batch_size: {}
dropout: {:.2}
stddev: {:.2}
""".format(100 * accuracy, params["learning_rate"], params["batch_size"], params["dropout"], params["stddev"])
""".format(100 * accuracy, params["learning_rate"], params["batch_size"], params["dropout"], params["stddev"]))
if accuracy > best_accuracy:
best_params = params
best_accuracy = accuracy
# Record the best performing set of hyperparameters.
print """Best accuracy over {} trials was {:.3} with
print("""Best accuracy over {} trials was {:.3} with
learning_rate: {:.2}
batch_size: {}
dropout: {:.2}
stddev: {:.2}
""".format(trials, 100 * best_accuracy, best_params["learning_rate"], best_params["batch_size"], best_params["dropout"], best_params["stddev"])
""".format(trials, 100 * best_accuracy, best_params["learning_rate"], best_params["batch_size"], best_params["dropout"], best_params["stddev"]))
+5 -1
View File
@@ -1,3 +1,7 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import ray
import numpy as np
import tensorflow as tf
@@ -6,7 +10,7 @@ def get_batch(data, batch_index, batch_size):
# This method currently drops data when num_data is not divisible by
# batch_size.
num_data = data.shape[0]
num_batches = num_data / batch_size
num_batches = num_data // batch_size
batch_index %= num_batches
return data[(batch_index * batch_size):((batch_index + 1) * batch_size)]
+1 -1
View File
@@ -32,7 +32,7 @@ built in methods for loading the data.
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
batch_size = 100
num_batches = mnist.train.num_examples / batch_size
num_batches = mnist.train.num_examples // batch_size
batches = [mnist.train.next_batch(batch_size) for _ in range(num_batches)]
```
+8 -4
View File
@@ -1,3 +1,7 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import ray
import numpy as np
@@ -115,16 +119,16 @@ if __name__ == "__main__":
# algorithm.
# Load the mnist data and turn the data into remote objects.
print "Downloading the MNIST dataset. This may take a minute."
print("Downloading the MNIST dataset. This may take a minute.")
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
batch_size = 100
num_batches = mnist.train.num_examples / batch_size
num_batches = mnist.train.num_examples // batch_size
batches = [mnist.train.next_batch(batch_size) for _ in range(num_batches)]
print "Putting MNIST in the object store."
print("Putting MNIST in the object store.")
batch_ids = [(ray.put(xs), ray.put(ys)) for (xs, ys) in batches]
# Initialize the weights for the network to the vector of all zeros.
theta_init = 1e-2 * np.random.normal(size=dim)
# Use L-BFGS to minimize the loss function.
print "Running L-BFGS."
print("Running L-BFGS.")
result = scipy.optimize.fmin_l_bfgs_b(full_loss, theta_init, maxiter=10, fprime=full_grad, disp=True)
+5 -1
View File
@@ -1,6 +1,10 @@
# This code is copied and adapted from Andrej Karpathy's code for learning to
# play Pong https://gist.github.com/karpathy/a4166c7fe253700972fcbc77e4ea32c5.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import cPickle as pickle
import ray
@@ -135,7 +139,7 @@ if __name__ == "__main__":
reward_sum = ray.get(reward_sums[i])
for k in model: grad_buffer[k] += grad[k] # accumulate grad over batch
running_reward = reward_sum if running_reward is None else running_reward * 0.99 + reward_sum * 0.01
print "Batch {}. episode reward total was {}. running mean: {}".format(batch_num, reward_sum, running_reward)
print("Batch {}. episode reward total was {}. running mean: {}".format(batch_num, reward_sum, running_reward))
for k, v in model.iteritems():
g = grad_buffer[k] # gradient
rmsprop_cache[k] = decay_rate * rmsprop_cache[k] + (1 - decay_rate) * g ** 2