简体   繁体   中英

Tensorflow 2 tf.function input_signature for a list input

In order to export my model with the saved_model api, I need to define the input_signature of each method intended to be called after loading. I don't know how to tell that the input is a list with variable length (as it is for tf.keras.Model.call for instance).

There is a list of unanswered questions about input_signature on SO:

and also this one about *args : TensorFlow 2 How to use *args in tf.function? but it does not handle the problem of saved_model .

Maybe you could use a Tensor instead of a list as input?

Then specify a [None] dimension in tf.TensorSpec to allow for flexibility in trace reuse.

Since TensorFlow matches tensors based on their shape, using a None dimension as a wildcard will allow Functions to reuse traces for variably-sized input. Variably-sized input can occur if you have sequences of different length, or images of different sizes for each batch.

@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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM