TFF:保存模型时发生TypeError

时间:2020-05-29 18:14:38

标签: tensorflow-federated

请我需要帮助! 编写完TFF的代码后,我想保存我的模型,所以我在代码末尾添加了这一行

ckpt_manager = checkpoint_manager.FileCheckpointManager("model.h5")
ckpt_manager.save_checkpoint(state, round_num=1)

错误是:

TypeError: To be compatible with tf.contrib.eager.defun, Python functions must return zero or more Tensors; in compilation of <function FileCheckpointManager.save_checkpoint.<locals>.<lambda> at 0x7f06b40de730>, found return value of type <class 'tensorflow_federated.python.common_libs.anonymous_tuple.AnonymousTuple'>, which is not a Tensor.

1 个答案:

答案 0 :(得分:0)

FileCheckpointManager在后​​台使用纯TensorFlow API,因此无法理解用于结构化数据的TFF的AnonymousTuple类型。

有必要将AnonymousTuple转换回其创建时的Python结构。有时可以通过AnonymousTuple上的_asdict() method完成此操作,但是如果元组具有任何未命名的字段,则该操作将失败。否则,必须执行类似from_tff_result method here的操作。