【问题标题】:Calculating percentile of dataset words and Tensorflow-hub model计算数据集单词的百分位数和 Tensorflow-hub 模型
【发布时间】:2021-10-02 17:45:02
【问题描述】:

我想计算 tensorflow-hub 模型中存在的数据集单词的百分位数(例如 ELMoUniversal Sentence Encoder)。对于像GloVe这样的局部模型,我使用一种幼稚的方法:读取局部模型,将其转移到set,然后计算百分位数:

f = open('../glove.6B.100d.txt', encoding="utf8")
#Read all the word into a list
...
intersect_words = set(dataset_words).intersect(glove_words)
percentile = len(intersect_words)/len(dataset_words)*100

对于 Tenorflow-hub 模型,有什么方法可以做到这一点吗?

【问题讨论】:

    标签: python tensorflow tensorflow-hub


    【解决方案1】:

    对于某些模型,词汇表是在 SavedModel 协议缓冲区中序列化的(例如 USE 和 ELMo),因此必须在 SavedModel 中手动找到并提取它(我使用逻辑从 @ 的 USE 中提取词汇表987654321@):

    import tensorflow_hub as hub
    from tensorflow.python.saved_model.loader_impl import parse_saved_model
    
    # This caches the model at `model_path`.
    hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
    model_path = '/tmp/tfhub_modules/063d866c06683311b44b4992fd46003be952409c/'
    saved_model = parse_saved_model(model_path)
    
    # The location of the tensor holding the vocab is model-specific.
    graph = saved_model.meta_graphs[0].graph_def
    function_ = graph.library.function
    embedding_node = function_[5].node_def[1]  # Node name is "Embedding_words".
    words_tensor = embedding_node.attr.get("value").tensor
    word_list = [s.decode('utf-8') for s in words_tensor.string_val]
    word_list[100:105]  # ['best', ',▁but', 'no', 'any', 'more']
    

    对于像google/Wiki-words-500/2这样的其他模型,我们比较幸运,因为词汇已经导出到assets/目录:

    hub.load("https://tfhub.dev/google/Wiki-words-500/2")
    !head /tmp/tfhub_modules/bf115a5fe517f019bebae05b433eaeee6415f5bf/assets/tokens.txt -n 40000 | tail
    # Antisense
    # Antiseptic
    # Antiseptics
    

    【讨论】:

    • 非常感谢,这正是我想要的
    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 2013-06-20
    • 2012-11-12
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多