Python 在何处使用光线角色导入tensorflow?
使用ray actors并行运行多个tensorflow模型,我问自己在哪里导入tensorflow:Python 在何处使用光线角色导入tensorflow?,python,tensorflow,import,python-import,ray,Python,Tensorflow,Import,Python Import,Ray,使用ray actors并行运行多个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?
# [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()
文档中给出的示例提到由于“导入tensorflow和设置全局状态的副作用”而在actor中导入tensorflow,但仅给出了光线远程功能的示例。那么我应该在[1]、[2]或[3]甚至其他地方运行“import tensorflow as tf”吗?这里是否有一个最佳实践,在[1 ]、[2 ]和[3 ]之间有什么区别,即在每种情况下如何访问TySoFLUM以及它们何时被执行?
现在可能是固定的,但是考虑的最安全的选项是在[3 ]中引入TysFooSo.
< P>。最好在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)