TF版本:最新母版,b083cea
下面是一个使用TF2的简单示例。0急切模式,它使用MirroredStrategy成功运行,但参数ServerStrategy出错。
我可以用TF2。0即时模式支持参数服务器策略现在?到目前为止,我还没有找到一个成功的例子:(
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import tensorflow_datasets as tfds
import os, json
datasets, info = tfds.load(name='mnist', with_info=True, as_supervised=True)
mnist_train, mnist_test = datasets['train'], datasets['test']
os.environ['TF_CONFIG'] = json.dumps({
"cluster": {
"worker": ["localhost:12345"],
"ps": ["localhost:12346"]
},
"task": {"type": "worker", "index": 0}
})
strategy = tf.distribute.experimental.ParameterServerStrategy()
#strategy = tf.distribute.MirroredStrategy()
print('Number of devices: {}'.format(strategy.num_replicas_in_sync))
num_train_examples = info.splits['train'].num_examples
num_test_examples = info.splits['test'].num_examples
BUFFER_SIZE = 10000
BATCH_SIZE_PER_REPLICA = 64
BATCH_SIZE = BATCH_SIZE_PER_REPLICA * strategy.num_replicas_in_sync
def scale(image, label):
image = tf.cast(image, tf.float32)
image /= 255
return image, label
train_dataset = mnist_train.map(scale).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
eval_dataset = mnist_test.map(scale).batch(BATCH_SIZE)
with strategy.scope():
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(loss='sparse_categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(),
metrics=['accuracy'])
checkpoint_dir = './training_checkpoints'
# Name of the checkpoint files
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt_{epoch}")
# Function for decaying the learning rate.
# You can define any decay function you need.
def decay(epoch):
if epoch < 3:
return 1e-3
elif epoch >= 3 and epoch < 7:
return 1e-4
else:
return 1e-5
# Callback for printing the LR at the end of each epoch.
class PrintLR(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
print('\nLearning rate for epoch {} is {}'.format(epoch + 1,
model.optimizer.lr.numpy()))
callbacks = [
tf.keras.callbacks.TensorBoard(log_dir='./logs'),
tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_prefix,
save_weights_only=True),
tf.keras.callbacks.LearningRateScheduler(decay),
PrintLR()
]
model.fit(train_dataset, epochs=12, callbacks=callbacks)
model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))
eval_loss, eval_acc = model.evaluate(eval_dataset)
print('Eval loss: {}, Eval Accuracy: {}'.format(eval_loss, eval_acc))
错误消息
tf.keras.layers.Dense(10, activation='softmax') File "/usr/local/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py", line 456, in _method_wrapper result = method(self, *args, **kwargs) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/sequential.py", line 116, in __init__ super(Sequential, self).__init__(name=name, autocast=False) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 199, in __init__ self._init_batch_counters() File "/usr/local/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py", line 456, in _method_wrapper result = method(self, *args, **kwargs) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 206, in _init_batch_counters self._train_counter = variables.Variable(0, dtype='int64', aggregation=agg) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 261, in __call__ return cls._variable_v2_call(*args, **kwargs) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 255, in _variable_v2_call shape=shape) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 66, in getter return captured_getter(captured_previous, **kwargs) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py", line 1769, in creator_with_resource_vars return self._create_variable(next_creator, **kwargs) File "/usr/local/lib/python3.7/site-packages/tensorflow/python/distribute/parameter_server_strategy.py", line 455, in _create_variable with ops.device(self._variable_device): File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 5183, in device "tf.device does not support functions when eager execution " RuntimeError: tf.device does not support functions when eager execution is enabled.
在tf中。分配实验的ParameterServerStrategy页,其状态如下
注意:此策略仅适用于估计器API。创建RunConfig时,将此策略的实例传递给experimental_distribute参数。然后,此RunConfig实例应传递给调用train_and_evaluate的估计器实例。
下面是一个关于如何使用tf的示例。分配实验的ParameterServerStrategy()
-
strategy = tf.distribute.experimental.ParameterServerStrategy()
run_config = tf.estimator.RunConfig(
experimental_distribute.train_distribute=strategy)
estimator = tf.estimator.Estimator(config=run_config)
tf.estimator.train_and_evaluate(estimator,...)
此外,如果您转到使用TensorFlow进行分布式培训的页面,它将解释在TF 2.0的哪些场景中支持什么,
希望这回答了你的问题。快乐学习。
这篇来自radius networks的博客文章讨论了Android设备如何还不能用作iBeacons(又名:BLE外设模式),即使设备的硬件支持BLE,因为Android没有用于BLE外设模式的API。 注:BLE表示蓝牙低能量 我非常需要来自Android的BLE外设模式支持,目前我会同意黑客的一些东西,希望Android最终会支持这个功能集,顺便说一下,这已经是一个功能请求
编辑:因为人们仍然从google登陆这里,你必须在OpenGL环境中调用每一个OpenGL方法。因此,在使用GL做任何事情之前,请确保您在一个上下文中。 我试图用lwjgl在我的屏幕上呈现一个简单的文本,但是每次都失败了!当我启动游戏时,它崩溃了,并向我抛出错误: 我需要使用现代openGL还是什么?我真的需要帮助
我正在使用Jackson模式模块为Rest API生成JSON模式,当开始使用https://github.com/fge/json-schema-validator针对对我的API的请求实现JSON模式验证时,我意识到Jackson生成的是模式v3,而验证器只支持V4-draft。在寻找其他java JSON模式生成器库之前,您能确认Jackson不支持V4吗?您能推荐其他java JSON模式
问题内容: 我试图快速了解仿制药在做什么。 我创建了这个样本游乐场 该行给我这个错误 从另一个堆栈溢出问题中,我看到较早的Xcode版本中的此错误意味着 但是在上面的操场上,缺少什么编译器? 我在swift4 / xcode9上。 更新资料 遵循建议后,错误发生变化: 比@ vini- app建议删除UIViewController以使其工作。我仍然不明白为什么UIViewController在那
当我试图将LWJGL程序的矩阵模式设置为GL_投影时,我遇到了一个错误。 错误是: 异常线程"main"java.lang.IllegalStateException:函数不支持org.lwjgl.BufferChecks.checkFunctionAddress(BufferChecks.java:58)在org.lwjgl.opengl.GL11.glMatrixMode(GL11.java:
本文向大家介绍Android实现夜间模式切换功能实现代码,包括了Android实现夜间模式切换功能实现代码的使用技巧和注意事项,需要的朋友参考一下 现在很多App都有夜间模式,特别是阅读类的App,夜间模式现在已经是阅读类App的标配了,事实上,日间模式与夜间模式就是给App定义并应用两套不同颜色的主题,用户可以自动或者手动的开启,今天用Android自带的support包来实现夜间模式。由于Su