【问题标题】:Converting an input to UTF-8 with @tf.function使用 @tf.function 将输入转换为 UTF-8
【发布时间】:2021-04-27 06:32:38
【问题描述】:

我使用的是 TensorFlow 2.4.1。我尝试使用 tf.strings.unicode_decode 来解码带有 @tf.function 的 base64 编码字符串,但是发生了错误,其中 ValueError: Rank of input 必须是静态已知的。我检查了 tf.strings.unicode_decode 在没有@tf.function 的情况下可以正常工作。有没有办法用@tf.function 解码 base64 编码的字符串?非常感谢您的回答。

我加载了一个 SavedModel 并想更改 serving_default。但我被困在将输入转换为UTF-8。这是我试过的代码。

class CustomTransformer(tf.keras.Model):
    def __init__(self):
        super(CustomTransformer, self).__init__()
        self.model = tf.saved_model.load('./models/transformer/1')
  
    @tf.function(input_signature=[tf.TensorSpec(shape=None, dtype=tf.string)])
    def call(self, input):

        # Error occurred. ValueError: Rank of `input` must be statically known.
        _input_str = tf.strings.unicode_decode(input_data, 'UTF-8')

        return _input_str

这是错误信息。

ValueError: Rank of `input` must be statically known.

当尝试从加载的 SavedModel 更改 serving_default 时,是否有办法将输入转换为 UTF-8

【问题讨论】:

    标签: python tensorflow tensorflow2.0 tensorflow-serving


    【解决方案1】:

    使用tf.strings.unicode_decode 时,您需要指定一个形状。 (见documentation)。在这种情况下,因为您使用的是没有任何维度(简单字符串)的张量,所以只需提供一个空元组作为形状:

    @tf.function(input_signature=[tf.TensorSpec(shape=(), dtype=tf.string)])
    

    【讨论】:

    • 非常感谢!有用!我尝试使用 chr() 将 UTF-8 解码输出转换为字符,我得到 TypeError: an integer is required (got type Tensor)。有没有办法解决这个问题?
    • 您可能想问其他问题,了解更多详情。
    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    相关资源
    最近更新 更多