在哪里使用射线演员导入张量流?
Where to import tensorflow using ray actors?
使用ray actor并行运行几个tensorflow模型,我问自己在哪里导入tensorflow:
# [1] maybe import tensorflow here?
@ray.remote(num_cpus=1)
class Remote_Runner:
# [2] maybe import tensorflow here?
def __init__(self, weights):
# [3] maybe import tensorflow here?
self.model=My_model()
self.model.set_weights(wegihts)
def do_something_with_model:
self.model.do_something()
由于'side-effects of importing TensorFlow and setting global state',文档中给出的示例提到在actor中导入tensorflow,但仅给出了一个射线远程函数的示例。那么我应该 运行 'import tensorflow as tf' 在 [1]、[2] 或 [3],还是其他地方?此处是否有可遵循的最佳实践,[1]、[2] 和 [3] 之间的区别是什么,即在每种情况下我如何访问 tensorflow 以及它们何时执行?
现在可能已修复此问题,但最安全的选择是在 [3] 导入 tensorflow。
根据 https://docs.ray.io/en/latest/using-ray-with-tensorflow.html 中提到的最佳实践,最好在 My_model()
中导入 tensorflow
你可以考虑下面的例子
def create_keras_model():
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation="relu", input_shape=(32, )))
# Add another:
model.add(layers.Dense(64, activation="relu"))
# Add a softmax layer with 10 output units:
model.add(layers.Dense(10, activation="softmax"))
model.compile(
optimizer=keras.optimizers.RMSprop(0.01),
loss=keras.losses.categorical_crossentropy,
metrics=[keras.metrics.categorical_accuracy])
return model
import ray
import numpy as np
ray.init()
def random_one_hot_labels(shape):
n, n_class = shape
classes = np.random.randint(0, n_class, n)
labels = np.zeros((n, n_class))
labels[np.arange(n), classes] = 1
return labels
# Use GPU wth
# @ray.remote(num_gpus=1)
@ray.remote
class Network(object):
def __init__(self):
self.model = create_keras_model()
self.dataset = np.random.random((1000, 32))
self.labels = random_one_hot_labels((1000, 10))
def train(self):
history = self.model.fit(self.dataset, self.labels, verbose=False)
return history.history
def get_weights(self):
return self.model.get_weights()
def set_weights(self, weights):
# Note that for simplicity this does not handle the optimizer state.
self.model.set_weights(weights)
使用ray actor并行运行几个tensorflow模型,我问自己在哪里导入tensorflow:
# [1] maybe import tensorflow here?
@ray.remote(num_cpus=1)
class Remote_Runner:
# [2] maybe import tensorflow here?
def __init__(self, weights):
# [3] maybe import tensorflow here?
self.model=My_model()
self.model.set_weights(wegihts)
def do_something_with_model:
self.model.do_something()
由于'side-effects of importing TensorFlow and setting global state',文档中给出的示例提到在actor中导入tensorflow,但仅给出了一个射线远程函数的示例。那么我应该 运行 'import tensorflow as tf' 在 [1]、[2] 或 [3],还是其他地方?此处是否有可遵循的最佳实践,[1]、[2] 和 [3] 之间的区别是什么,即在每种情况下我如何访问 tensorflow 以及它们何时执行?
现在可能已修复此问题,但最安全的选择是在 [3] 导入 tensorflow。
根据 https://docs.ray.io/en/latest/using-ray-with-tensorflow.html 中提到的最佳实践,最好在 My_model()
你可以考虑下面的例子
def create_keras_model():
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation="relu", input_shape=(32, )))
# Add another:
model.add(layers.Dense(64, activation="relu"))
# Add a softmax layer with 10 output units:
model.add(layers.Dense(10, activation="softmax"))
model.compile(
optimizer=keras.optimizers.RMSprop(0.01),
loss=keras.losses.categorical_crossentropy,
metrics=[keras.metrics.categorical_accuracy])
return model
import ray
import numpy as np
ray.init()
def random_one_hot_labels(shape):
n, n_class = shape
classes = np.random.randint(0, n_class, n)
labels = np.zeros((n, n_class))
labels[np.arange(n), classes] = 1
return labels
# Use GPU wth
# @ray.remote(num_gpus=1)
@ray.remote
class Network(object):
def __init__(self):
self.model = create_keras_model()
self.dataset = np.random.random((1000, 32))
self.labels = random_one_hot_labels((1000, 10))
def train(self):
history = self.model.fit(self.dataset, self.labels, verbose=False)
return history.history
def get_weights(self):
return self.model.get_weights()
def set_weights(self, weights):
# Note that for simplicity this does not handle the optimizer state.
self.model.set_weights(weights)