【问题标题】:How to make prediction with TFF?如何使用 TFF 进行预测?
【发布时间】:2021-01-05 11:29:34
【问题描述】:

我的问题是:如何使用 Tensorflow Federated 预测此类图像的标签?

完成模型评估后,我想预测给定图像的标签。就像在 Keras 中一样,我们这样做:

# new instance where we do not know the answer
Xnew = array([[0.89337759, 0.65864154]])
# make a prediction
ynew = model.predict_classes(Xnew)
# show the inputs and predicted outputs
print("X=%s, Predicted=%s" % (Xnew[0], ynew[0]))

输出:

X=[0.89337759 0.65864154], Predicted=[0]

以下是 state 和 model_fn 的创建方式:


def model_fn():
    keras_model = create_compiled_keras_model()
    return tff.learning.from_compiled_keras_model(keras_model, sample_batch) 

iterative_process = tff.learning.build_federated_averaging_process(model_fn, server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.0),client_weight_fn=None)
state = iterative_process.initialize()

我发现这个错误:

list(self._name_to_index.keys())[:10]))
AttributeError: The tuple of length 2 does not have named field "assign_weights_to". Fields (up to first 10): ['trainable', 'non_trainable']

谢谢

【问题讨论】:

  • 答案将取决于state 是如何创建的(tff.learning 是否使用过?如果是,model_fn 参数是如何定义的?)可以用这些细节扩展问题吗?跨度>

标签: tensorflow-federated


【解决方案1】:

(需要 TFF 0.16.0 或更新版本)

由于代码是从tf.keras.Model 构建tff.learning.Model,您可以在tff.learning.ModelWeights 对象(state.model 的类型)上使用assign_weights_to 方法。 这个方法在Federated Learning for Text Generation教程中使用。

这可能看起来像(靠近底部,早期部分是示例 FL 训练循环):


def create_keras_model() -> tf.keras.Model:
  ...

def model_fn():
  ...
  return tff.learning.from_keras_model(create_keras_model())

training_process = tff.learning. build_federated_averaging_process(model_fn, ...)

state = training_process.initialize()
for _ in range(NUM_ROUNDS):
  state, metrics = training_process.next(state, ...)

model_for_inference = create_keras_model()
state.model.assign_weights_to(model_for_inference)

一旦 state 的权重被分配回 Keras 模型,代码就可以使用标准的 Keras API,例如 tf.keras.Model.predict_on_batch

predictions = model_for_inference.predict_on_batch(batch)

【讨论】:

  • 感谢您的宝贵时间,当我在 10 轮后执行您的解决方案时,我发现此错误 AttributeError: The tuple of length 2 does not have named field "assign_weights_to". Fields (up to first 10): ['trainable', 'non_trainable']
  • 正在使用什么版本的 TFF?此错误消息可能意味着在使用tff.federated_computation 注释的函数内部调用了assign_weights_to,这将不起作用。需要在initializenext返回的state上使用。请参阅教程中提到的答案中的def keras_evaluate(state, round_num),其中调用keras_model.evaluate 可以替换为keras_model.predict_on_batch
  • 能否包含 TFF 的版本以及错误消息中包含的堆栈跟踪?这将有助于提供更好的答案。
  • TFF 0.12.0 Traceback (most recent call last): File "d.py", line 171, in <module> state.model.assign_weights_to(model_for_inference) File "/home/hp/anaconda3/lib/python3.6/site-packages/tensorflow_federated/python/common_libs/anonymous_tuple.py", line 138, in __getattr__ list(self._name_to_index.keys())[:10])) AttributeError: The tuple of length 2 does not have named field "assign_weights_to". Fields (up to first 10): ['trainable', 'non_trainable']
  • 上述答案可能需要更新版本的 TFF,可能是 0.16.0 之后的版本。在问题中,构造sample_batch 的方法可以创建批次。它应该是模型期望格式的嵌套结构。
猜你喜欢
  • 1970-01-01
  • 2011-09-14
  • 2020-10-30
  • 2015-06-06
  • 2012-12-13
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 2022-01-02
相关资源
最近更新 更多