【问题标题】:Is there a way to use pre-trained Embedding with Tf-Idf in tensorflow?有没有办法在张量流中使用带有 Tf-Idf 的预训练嵌入?
【发布时间】:2022-01-03 08:59:01
【问题描述】:

我正在使用文本分类的默认和基本实现:


 

  tokenizer = Tokenizer(num_words=vocab_size, filters = filters)
  tokenizer.fit_on_texts(list(train_X))
  train_X = tokenizer.texts_to_sequences(train_X)
  val_X = tokenizer.texts_to_sequences(val_X)
  train_X = pad_sequences(train_X, maxlen=maxlen)
  val_X = pad_sequences(val_X, maxlen=maxlen)

 def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32') # For loading Embedding

  embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE))
  all_embs = np.stack(embeddings_index.values())
  emb_mean,emb_std = all_embs.mean(), all_embs.std()
  embed_dim = all_embs.shape[1]

  word_index = tokenizer.word_index
  vocab_size = min(vocab_size, len(word_index))

  embedding_matrix = np.random.normal(emb_mean, emb_std, (vocab_size, embed_dim)) # vocab_size was nb_words
  for word, i in word_index.items():
      if i >= vocab_size: continue
      embedding_vector = embeddings_index.get(word)
      if embedding_vector is not None: embedding_matrix[i] = embedding_vector

它工作正常,但有没有办法texts_to_matrix,那里有binart, tfidf, count 等选项。我可以将它们与现有的嵌入一起使用吗?

一种可能的方法是使用多输入模型,然后将两个输入连接到一个位置。除此之外,还有吗?

【问题讨论】:

    标签: python tensorflow keras deep-learning nlp


    【解决方案1】:

    最常用的方法是将每个词向量乘以其对应的tf_idf 分数。人们经常在学术论文中看到这种方法。你可以这样做:

    创建tfidf 分数:

    import tensorflow as tf
    import numpy as np
    import gensim.downloader as api
    from sklearn.feature_extraction.text import TfidfVectorizer
    import collections
    
    def td_idf_word2weight(text):
        print("Creating TfidfVectorizer...")
        tfidf = TfidfVectorizer(preprocessor=' '.join)
        tfidf.fit(text)
    
        # if a word was never seen - it is considered to be at least as infrequent as any of the known words
        max_idf = max(tfidf.idf_)
        return collections.defaultdict(
            lambda: max_idf,
            [(w, tfidf.idf_[i]) for w, i in tfidf.vocabulary_.items()])
    
    text = [['she let the balloon float up into the air with her hopes and dreams'],
            ['the old rusted farm equipment surrounded the house predicting its demise'],
            ['he was so preoccupied with whether or not he could that he failed to stop to consider if he should']]
    
    td_idf = td_idf_word2weight(text)
    
    text = np.concatenate(text)
    tokenizer = tf.keras.preprocessing.text.Tokenizer()
    tokenizer.fit_on_texts(text)
    text_sequences = tokenizer.texts_to_sequences(text)
    text_sequences = tf.keras.preprocessing.sequence.pad_sequences(text_sequences, padding='post')
    vocab_size = len(tokenizer.word_index) + 1
    print(td_idf.items())
    print(vocab_size)
    
    Creating TfidfVectorizer...
    dict_items([('she', 1.6931471805599454), ('let', 1.6931471805599454), ('the', 1.2876820724517808), ('balloon', 1.6931471805599454), ('float', 1.6931471805599454), ('up', 1.6931471805599454), ('into', 1.6931471805599454), ('air', 1.6931471805599454), ('with', 1.2876820724517808), ('her', 1.6931471805599454), ('hopes', 1.6931471805599454), ('and', 1.6931471805599454), ('dreams', 1.6931471805599454), ('old', 1.6931471805599454), ('rusted', 1.6931471805599454), ('farm', 1.6931471805599454), ('equipment', 1.6931471805599454), ('surrounded', 1.6931471805599454), ('house', 1.6931471805599454), ('predicting', 1.6931471805599454), ('its', 1.6931471805599454), ('demise', 1.6931471805599454), ('he', 1.6931471805599454), ('was', 1.6931471805599454), ('so', 1.6931471805599454), ('preoccupied', 1.6931471805599454), ('whether', 1.6931471805599454), ('or', 1.6931471805599454), ('not', 1.6931471805599454), ('could', 1.6931471805599454), ('that', 1.6931471805599454), ('failed', 1.6931471805599454), ('to', 1.6931471805599454), ('stop', 1.6931471805599454), ('consider', 1.6931471805599454), ('if', 1.6931471805599454), ('should', 1.6931471805599454)])
    38
    

    创建tf_idf-加权嵌入矩阵:

    model = api.load("glove-twitter-25")
    embedding_dim = 25
    weight_matrix = np.zeros((vocab_size, embedding_dim))
    for word, i in tokenizer.word_index.items():
      try:
        embedding_vector = model[word] * td_idf[word]
        weight_matrix[i] = embedding_vector 
      except KeyError:
        weight_matrix[i] = np.random.uniform(-5, 5, embedding_dim)
    print(weight_matrix.shape)
    
    (38, 25)
    

    【讨论】:

    • 有道理。我怎么没有得到。将 tf-idf 与嵌入相乘是有意义的。非常感谢。
    • 你能解释一下max_idf = max(tfidf.idf_)的逻辑吗?我的意思是OOV 不应该是最小值?
    • 这只是一种选择..看看文档..你也可以用不同的方式解决它。
    • 您在说哪些文档?我没有得到那个 lambda 部分。你能用外行的术语解释一下它是如何工作的吗?
    • 不,我一直在寻找lambda 的用法,这只是if word in dict 的捷径,否则some value 就像max 一样。谢谢
    猜你喜欢
    • 2019-07-17
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多