我实现了图像分类的TFF代码。TFF版本0.18.0,我这样写:
iterative_process = tff.learning.build_federated_averaging_process(model_fn, server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.0), client_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=0.001))
state = iterative_process.initialize()
但我发现这个警告:
WARNING:tensorflow:AutoGraph could not transform <function <lambda> at 0x7fca141a6d08> and will run it as-is.
Cause: could not parse the source code of <function <lambda> at 0x7fca141a6d08>: found multiple definitions with identical signatures at the location. This error may be avoided by defining each lambda on a single line and with unique argument names.
Match 0:
(lambda : tf.keras.optimizers.SGD(learning_rate=1.0))
Match 1:
(lambda : tf.keras.optimizers.SGD(learning_rate=0.001))
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
所以请告诉我怎样才能避免这个警告。谢谢
发布于 2021-04-05 08:45:01
首先,这个警告对我来说似乎不是问题。TFF需要构造优化器的函数,以防这些优化器依赖于内部变量(例如,Adagrad和Adam,它们是有状态的,并使用变量来跟踪预处理和动量项)--TFF需要能够捕获这些变量的结构,以确保正确的代码可以在设备上运行,因此不需要签名来转换这些函数--Python函数就足够了。
其次,我相信一个简单的选择会让警告消失,那就是为你的优化器fns使用一个命名函数。也就是说,如果您使用类似于
def server_optimizer_fn():
return tf.keras.optimizers.SGD(learning_rate=1.)
def client_optimizer_fn():
return tf.keras.optimizers.SGD(learning_rate=0.001)
iterative_process = tff.learning.build_federated_averaging_process(
model_fn,
server_optimizer_fn=server_optimizer_fn,
client_optimizer_fn=client_optimizer_fn)
签名不应该再抱怨了。
https://stackoverflow.com/questions/66581075
复制相似问题