【问题标题】:confuse about parameter 'tokens_length' of elmo model in tensorflow hub混淆 tensorflow hub 中 elmo 模型的参数“tokens_length”
【发布时间】:2019-06-25 12:26:28
【问题描述】:

我正在 tensorflow hub 中查看 ELMo 模型,我不太清楚 tokens_length = [6, 5] 在流示例中的含义是什么: (https://tfhub.dev/google/elmo/2)

elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
tokens_input = [["the", "cat", "is", "on", "the", "mat"],
                ["dogs", "are", "in", "the", "fog", ""]]
tokens_length = [6, 5]
embeddings = elmo(
    inputs={
        "tokens": tokens_input,
        "sequence_len": tokens_length
    },
    signature="tokens",
    as_dict=True)["elmo"]

它不喜欢输入标记句子的最大长度,也不喜欢[每个句子的最大单词数,句子数],这让我感到困惑。 有人可以解释一下吗? 谢谢!

【问题讨论】:

    标签: tensorflow nlp embedding elmo


    【解决方案1】:

    第一个示例的长度为6,第二个示例的长度为5:。即

    猫在垫子上”有 6 个字长,而“狗在雾中”只有 5 个字长。输入中额外的空字符串确实会增加一些混乱:-/

    如果您阅读该页面上的文档,它会解释为什么需要这样做(bold 字体是我的)

    使用标记签名,模块将标记化的句子作为输入。输入张量是一个形状为 [batch_size, ma​​x_length] 的字符串张量和一个形状为 [batch_size] 对应于句子长度的 int32 张量。 长度输入是必要的,以排除长度变化的句子的填充

    【讨论】:

    • 啊哈,谢谢!还有一个问题,在使用令牌签名时,我是否必须为每个句子提供有效长度?例如训练数据集有 100 个样本,并且给定 tokens_length 就像一个 100 维向量记录每个样本的长度。如果这是真的,那似乎有点麻烦:(
    • 很高兴为您提供帮助! :-) 这是我的理解,是的。不过,计算每个样本中的单词应该不会太麻烦吧?
    • emmm...不,不是太麻烦,但仍然需要做一些工作。也许作为一个初学者,我只是“被宠坏了”,哈哈
    • 那么这样的代码可以写吗? embeddings = elmo(inputs={"tokens": tokens_input, "sequence_len": [len(sample) - sample.count("") for sample in tokens_input]}, signature="tokens", as_dict=True)["elmo "] 这将返回每个句子的长度,没有填充。
    猜你喜欢
    • 2019-12-28
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2019-10-22
    • 1970-01-01
    • 2021-08-06
    • 2018-09-26
    相关资源
    最近更新 更多