【问题标题】:Tensorflow 2 tf.function input_signature for a list inputTensorflow 2 tf.function input_signature 用于列表输入
【发布时间】:2020-08-07 22:00:45
【问题描述】:

为了使用saved_model api 导出我的模型,我需要定义每个要在加载后调用的方法的input_signature。我不知道如何判断输入是一个可变长度的列表(例如tf.keras.Model.call)。

在 SO 上有一个关于input_signature 的未回答问题列表:

还有这个关于*argsTensorFlow 2 How to use *args in tf.function?,但它没有处理saved_model的问题。

【问题讨论】:

  • 你找到答案了吗?
  • 没那么有效
  • 我现在只使用不带 input_signature 的 @tf.function,并以这种方式拟合模型:model.fit(x=[inp1,inp2,etc], y=exp)

标签: python tensorflow model


【解决方案1】:

也许您可以使用张量而不是列表作为输入?

然后在tf.TensorSpec 中指定[None] 维度,以实现跟踪重用的灵活性。

由于 TensorFlow 会根据张量的形状匹配张量,因此使用 None 维度作为通配符将允许函数对可变大小的输入重复使用跟踪。如果您有不同长度的序列,或者每批有不同大小的图像,则可能会出现可变大小的输入。

@tf.function(input_signature=(tf.TensorSpec(shape=[None], dtype=tf.int32),))
def g(x):
  print('Tracing with', x)
  return x

# No retrace!
print(g(tf.constant([1, 2, 3])))
print(g(tf.constant([1, 2, 3, 4, 5])))
Tracing with Tensor("x:0", shape=(None,), dtype=int32)
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    相关资源
    最近更新 更多