首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在tensorflow_transform中获得apply_vocabulary之前的词汇量?

如何在tensorflow_transform中获得apply_vocabulary之前的词汇量?
EN

Stack Overflow用户
提问于 2022-01-28 20:14:01
回答 1查看 351关注 0票数 0

也在https://github.com/tensorflow/transform/issues/261上发布了这个问题

我在TFX中使用tft,需要将字符串列表类标签转换为preprocesing_fn中的多个热点指示器。实质上:

代码语言:javascript
复制
vocab = tft.vocabulary(inputs['label'])
outputs['label'] = tf.cast(
    tf.sparse.to_indicator(
       tft.apply_vocabulary(inputs['label'], vocab),
       vocab_size=VOCAB_SIZE,
    ),
    "int64",
)

我试图从find的结果中获得VOCAB_SIZE,但无法找到满足延迟执行和已知形状的方法。下面最接近的是不会传递保存的模型导出,因为标签的形状是未知的。

代码语言:javascript
复制
def _make_table_initializer(filename_tensor):
    return tf.lookup.TextFileInitializer(
        filename=filename_tensor,
        key_dtype=tf.string,
        key_index=tf.lookup.TextFileIndex.WHOLE_LINE,
        value_dtype=tf.int64,
        value_index=tf.lookup.TextFileIndex.LINE_NUMBER,
    )

def _vocab_size(deferred_vocab_filename_tensor):
    initializer = _make_table_initializer(deferred_vocab_filename_tensor)
    table = tf.lookup.StaticHashTable(initializer, default_value=-1)
    table_size = table.size()
    return table_size

deferred_vocab_and_filename = tft.vocabulary(inputs['label'])
vocab_applied = tft.apply_vocabulary(inputs['label'], deferred_vocab_and_filename)
vocab_size = _vocab_size(deferred_vocab_and_filename)
outputs['label'] = tf.cast(
    tf.sparse.to_indicator(vocab_applied, vocab_size=vocab_size),
    "int64",
)

得到

代码语言:javascript
复制
ValueError: Feature label (Tensor("Identity_3:0", shape=(None, None), dtype=int64)) had invalid shape (None, None) for FixedLenFeature: apart from the batch dimension, all dimensions must have known size [while running 'Analyze/CreateSavedModel[tf_v2_only]/CreateSavedModel']

知道如何做到这一点吗?

EN

回答 1

Stack Overflow用户

发布于 2022-02-24 07:03:35

根据github中的这个评论,您可以使用tft.experimental.get_vocabulary_size_by_name (链接)来实现相同的功能。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70899772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档