【问题标题】:TensorFlow Federated: How can I write an Input Spec for a model with more than one inputTensorFlow Federated:如何为具有多个输入的模型编写输入规范
【发布时间】:2020-04-04 20:37:35
【问题描述】:

我正在尝试使用 tensorflow 提供的联邦学习库制作图像字幕模型,但我遇到了这个错误

Input 0 of layer dense is incompatible with the layer: : expected min_ndim=2, found ndim=1.

这是我的 input_spec:

input_spec=collections.OrderedDict(x=(tf.TensorSpec(shape=(2048,), dtype=tf.float32), tf.TensorSpec(shape=(34,), dtype=tf.int32)), y=tf.TensorSpec(shape=(None), dtype=tf.int32))

该模型将图像特征作为第一个输入,将词汇列表作为第二个输入,但我无法在 input_spec 变量中表达这一点。我尝试将其表示为列表列表,但它仍然不起作用。接下来我可以尝试什么?

【问题讨论】:

    标签: python tensorflow tensorflow-federated


    【解决方案1】:

    好问题!在我看来,这个错误来自 TensorFlow,表明您可能具有正确的嵌套结构,但叶子可能已关闭。从 TFF 的角度来看,您的输入规范看起来“应该有效”,因此它可能与您拥有的数据略有不匹配

    我要尝试的第一件事-如果您有一个示例tf.data.Dataset 将被传递到您的客户端计算,您可以简单地直接从该数据集读取input_spec 作为element_spec 属性。这看起来像:

    # ds = example dataset
    input_spec = ds.element_spec
    

    这是最简单的路径。如果您有类似“numpy 数组列表的列表”之类的东西,仍然有一种方法可以让您从数据本身中提取这些信息——下面的代码 sn-p 应该可以帮助您:

    # data = list of list of numpy arrays
    input_spec = tf.nest.map_structure(lambda x: tf.TensorSpec(x.shape, x.dtype), data)
    

    最后,如果你有tf.Tensors的列表,TensorFlow提供了类似的功能:

    # tensor_structure = list of lists of tensors
    tf.nest.map_structure(tf.TensorSpec.from_tensor, tensor_structure)
    

    简而言之,我建议不要手动指定input_spec,而是让数据告诉你它的输入规范应该是什么。

    【讨论】:

    • 我尝试了你的第一个建议,它奏效了!非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2022-01-23
    • 2021-12-26
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多