-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.py
91 lines (67 loc) · 2.7 KB
/
10.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import ray
import tensorflow as tf
import time
ray.init(num_cpus=4, redirect_output=True)
@ray.remote
class SimpleModel(object):
def __init__(self):
x_data = tf.placeholder(tf.float32, shape=[100])
y_data = tf.placeholder(tf.float32, shape=[100])
w = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = w * x_data + b
self.loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
grads = optimizer.compute_gradients(self.loss)
self.train = optimizer.apply_gradients(grads)
init = tf.global_variables_initializer()
self.sess = tf.Session()
# Here we create the TensorFlowVariables object to assist with getting
# and setting weights.
self.variables = ray.experimental.TensorFlowVariables(self.loss, self.sess)
self.sess.run(init)
def set_weights(self, weights):
"""Set the neural net weights.
This method should assign the given weights to the neural net.
Args:
weights: Either a dict mapping strings (the variable names) to numpy
arrays or a single flattened numpy array containing all of the
concatenated weights.
"""
# EXERCISE: You will want to use self.variables here.
self.variables.set_flat(weights)
def get_weights(self):
"""Get the neural net weights.
This method should return the current neural net weights.
Returns:
Either a dict mapping strings (the variable names) to numpy arrays or
a single flattened numpy array containing all of the concatenated
weights.
"""
# EXERCISE: You will want to use self.variables here.
return self.variables.get_flat()
actors = [SimpleModel.remote() for _ in range(4)]
# Here 'weights' is a dictionary mapping variable names to the associated
# weights as a numpy array.
weights1=[]
#raise Exception('Implement this.')
for actor in actors:
weight0 = actor.get_weights.remote()
weights1.append(weight0)
#raise Exception('Implement this.')
weights1=ray.get(weights1)
print(weights1)
weight=np.mean(weights1)
p_arr=np.array([])
p_arr = np.concatenate((p_arr,[weight]))
p_arr = np.append(p_arr,weight)
for actor in actors:
actor.set_weights.remote(p_arr)
weights = ray.get([actor.get_weights.remote() for actor in actors])
for i in range(len(weights)):
np.testing.assert_equal(weights[i], weights[0])
print('Success! The test passed.')