mirror of
https://github.com/wassname/ray.git
synced 2026-09-10 12:38:43 +08:00
Remove type information from remote decorator.
This commit is contained in:
@@ -59,7 +59,7 @@ workers. At the core of our loading code is the remote function
|
||||
retrieves the appropriate object.
|
||||
|
||||
```python
|
||||
@ray.remote([str, str, List], [np.ndarray, List])
|
||||
@ray.remote(num_return_vals=2)
|
||||
def load_tarfile_from_s3(bucket, s3_key, size=[]):
|
||||
# Pull the object with the given key and bucket from S3, untar the contents,
|
||||
# and return it.
|
||||
@@ -85,7 +85,7 @@ The other parallel component of this application is the training procedure. This
|
||||
is built on top of the remote function `compute_grad`.
|
||||
|
||||
```python
|
||||
@ray.remote([np.ndarray, np.ndarray, np.ndarray, List], [List])
|
||||
@ray.remote()
|
||||
def compute_grad(X, Y, mean, weights):
|
||||
# Load the weights into the network.
|
||||
# Subtract the mean and crop the images.
|
||||
|
||||
@@ -39,7 +39,7 @@ def load_chunk(tarfile, size=None):
|
||||
filenames.append(filename)
|
||||
return np.concatenate(result), filenames
|
||||
|
||||
@ray.remote([str, str, List], [np.ndarray, List])
|
||||
@ray.remote(num_return_vals=2)
|
||||
def load_tarfile_from_s3(bucket, s3_key, size=[]):
|
||||
"""Load an imagenet .tar file.
|
||||
|
||||
@@ -231,7 +231,7 @@ def net_initialization():
|
||||
def net_reinitialization(net_vars):
|
||||
return net_vars
|
||||
|
||||
@ray.remote([List], [int])
|
||||
@ray.remote()
|
||||
def num_images(batches):
|
||||
"""Counts number of images in batches.
|
||||
|
||||
@@ -244,7 +244,7 @@ def num_images(batches):
|
||||
shape_ids = [ra.shape.remote(batch) for batch in batches]
|
||||
return sum([ray.get(shape_id)[0] for shape_id in shape_ids])
|
||||
|
||||
@ray.remote([List], [np.ndarray])
|
||||
@ray.remote()
|
||||
def compute_mean_image(batches):
|
||||
"""Computes the mean image given a list of batches of images.
|
||||
|
||||
@@ -261,7 +261,7 @@ def compute_mean_image(batches):
|
||||
n_images = num_images.remote(batches)
|
||||
return np.sum(sum_images, axis=0).astype("float64") / ray.get(n_images)
|
||||
|
||||
@ray.remote([np.ndarray, np.ndarray, np.ndarray, np.ndarray], [np.ndarray, np.ndarray, np.ndarray, np.ndarray])
|
||||
@ray.remote(num_return_vals=4)
|
||||
def shuffle_arrays(first_images, first_labels, second_images, second_labels):
|
||||
"""Shuffles the images and labels from two batches.
|
||||
|
||||
@@ -306,7 +306,7 @@ def shuffle_pair(first_batch, second_batch):
|
||||
images1, labels1, images2, labels2 = shuffle_arrays.remote(first_batch[0], first_batch[1], second_batch[0], second_batch[1])
|
||||
return (images1, labels1), (images2, labels2)
|
||||
|
||||
@ray.remote([list, dict], [np.ndarray])
|
||||
@ray.remote()
|
||||
def filenames_to_labels(filenames, filename_label_dict):
|
||||
"""Converts filename strings to integer labels.
|
||||
|
||||
@@ -381,7 +381,7 @@ def shuffle(batches):
|
||||
new_batches.append(permuted_batches[-1])
|
||||
return new_batches
|
||||
|
||||
@ray.remote([np.ndarray, np.ndarray, np.ndarray, List], [List])
|
||||
@ray.remote()
|
||||
def compute_grad(X, Y, mean, weights):
|
||||
"""Computes the gradient of the network.
|
||||
|
||||
@@ -406,7 +406,7 @@ def compute_grad(X, Y, mean, weights):
|
||||
# Compute the gradients.
|
||||
return sess.run([g for (g, v) in comp_grads], feed_dict={images: subset_X, y_true: subset_Y, dropout: 0.5})
|
||||
|
||||
@ray.remote([np.ndarray, np.ndarray, List], [np.float32])
|
||||
@ray.remote()
|
||||
def compute_accuracy(X, Y, weights):
|
||||
"""Returns the accuracy of the network
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ complicated version of this remote function is defined in
|
||||
[hyperopt.py](hyperopt.py).
|
||||
|
||||
```python
|
||||
@ray.remote([dict, np.ndarray, np.ndarray, np.ndarray, np.ndarray], [float])
|
||||
@ray.remote()
|
||||
def train_cnn_and_compute_accuracy(hyperparameters, train_images, train_labels, validation_images, validation_labels):
|
||||
# Actual work omitted.
|
||||
return validation_accuracy
|
||||
|
||||
@@ -51,7 +51,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])
|
||||
@ray.remote()
|
||||
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"]
|
||||
|
||||
@@ -91,12 +91,12 @@ use remote functions to distribute the loading of the data.
|
||||
Now, lets turn `loss` and `grad` into remote functions.
|
||||
|
||||
```python
|
||||
@ray.remote([np.ndarray, np.ndarray, np.ndarray], [float])
|
||||
@ray.remote()
|
||||
def loss(theta, xs, ys):
|
||||
# compute the loss
|
||||
return loss
|
||||
|
||||
@ray.remote([np.ndarray, np.ndarray, np.ndarray], [np.ndarray])
|
||||
@ray.remote()
|
||||
def grad(theta, xs, ys):
|
||||
# compute the gradient
|
||||
return grad
|
||||
|
||||
@@ -74,14 +74,14 @@ if __name__ == "__main__":
|
||||
sess.run([update_w, update_b], feed_dict={w_new: theta[:w_size].reshape(w_shape), b_new: theta[w_size:]})
|
||||
|
||||
# Compute the loss on a batch of data.
|
||||
@ray.remote([np.ndarray, np.ndarray, np.ndarray], [float])
|
||||
@ray.remote()
|
||||
def loss(theta, xs, ys):
|
||||
sess, _, _, cross_entropy, _, x, y_, _, _ = ray.reusables.net_vars
|
||||
load_weights(theta)
|
||||
return float(sess.run(cross_entropy, feed_dict={x: xs, y_: ys}))
|
||||
|
||||
# Compute the gradient of the loss on a batch of data.
|
||||
@ray.remote([np.ndarray, np.ndarray, np.ndarray], [np.ndarray])
|
||||
@ray.remote()
|
||||
def grad(theta, xs, ys):
|
||||
sess, _, _, _, cross_entropy_grads, x, y_, _, _ = ray.reusables.net_vars
|
||||
load_weights(theta)
|
||||
|
||||
@@ -32,7 +32,7 @@ estimate of the gradient. Below is a simplified pseudocode version of this
|
||||
function.
|
||||
|
||||
```python
|
||||
@ray.remote([dict], [dict, float])
|
||||
@ray.remote(num_return_vals=2)
|
||||
def compute_gradient(model):
|
||||
# Retrieve the game environment.
|
||||
env = ray.reusables.env
|
||||
|
||||
@@ -72,7 +72,7 @@ def policy_backward(eph, epx, epdlogp, model):
|
||||
dW1 = np.dot(dh.T, epx)
|
||||
return {"W1": dW1, "W2": dW2}
|
||||
|
||||
@ray.remote([dict], [dict, float])
|
||||
@ray.remote(num_return_vals=2)
|
||||
def compute_gradient(model):
|
||||
env = ray.reusables.env
|
||||
observation = env.reset()
|
||||
|
||||
@@ -79,7 +79,7 @@ we use reusable variables to store the gym environment and the neural network po
|
||||
then used in the remote `do_rollout` function to do a remote rollout:
|
||||
|
||||
```python
|
||||
@ray.remote([np.ndarray, int, int], [dict])
|
||||
@ray.remote()
|
||||
def do_rollout(policy, timestep_limit, seed):
|
||||
# Retrieve the game environment.
|
||||
env = ray.reusables.env
|
||||
|
||||
Reference in New Issue
Block a user