首页 文章

使用深度学习突出显示句子中的重要单词

提问于
浏览
1

我试图突出imdb数据集中的重要单词,这些单词最终促成了情绪分析预测 .

数据集如下:

X_train - 作为字符串的评论 .

Y_train - 0或1

现在,在使用Glove嵌入来嵌入X_train值之后,我可以将它提供给神经网络 .

现在我的问题是,我怎样才能突出显示最重要的概率词?就像deepmoji.mit.edu?

What have I tried :

  • 我尝试将输入句子分成双字节并使用1D CNN来训练它 . 后来当我们想要找到X_test的重要单词时,我们只是将big_ms中的X_test分开并找到它们的概率 . 它有效,但不准确 .

  • 我尝试使用预建的分层注意网络并成功了 . 我得到了我想要的但我无法从代码中找出每一行和概念 . 对我来说就像一个黑盒子 .

我知道神经网络是如何工作的,我可以使用numpy从头开始手动反向传播 . 我详细了解了lstm如何工作以及忘记,更新和输出门实际输出的内容 . 但我仍然无法弄清楚如何提取注意力以及如何将数据作为3D数组(我们的2D数据的时间步长是多少?)

因此,欢迎任何类型的指导

1 回答

  • 0

    这是一个注意(不是分层)的版本,但你应该能够弄清楚如何使它与层次结构一起工作 - 如果不是,我也可以提供帮助 . 诀窍是定义2个模型并使用1表示训练(模型),另一个表示提取注意值(model_with_attention_output):

    # Tensorflow 1.9; Keras 2.2.0 (latest versions)
    # should be backwards compatible upto Keras 2.0.9 and tf 1.5
    from keras.models import Model
    from keras.layers import *
    import numpy as np
    
    dictionary_size=1000
    
    def create_models():
      #Get a sequence of indexes of words as input:
      # Keras supports dynamic input lengths if you provide (None,) as the 
      #  input shape
      inp = Input((None,))
      #Embed words into vectors of size 10 each:
      # Output shape is (None,10)
      embs = Embedding(dictionary_size, 10)(inp)
      # Run LSTM on these vectors and return output on each timestep
      # Output shape is (None,5)
      lstm = LSTM(5, return_sequences=True)(embs)
      ##Attention Block
      #Transform each timestep into 1 value (attention_value) 
      # Output shape is (None,1)
      attention = TimeDistributed(Dense(1))(lstm)
      #By running softmax on axis 1 we force attention_values
      # to sum up to 1. We are effectively assigning a "weight" to each timestep
      # Output shape is still (None,1) but each value changes
      attention_vals = Softmax(axis=1)(attention)
      # Multiply the encoded timestep by the respective weight
      # I.e. we are scaling each timestep based on its weight
      # Output shape is (None,5): (None,5)*(None,1)=(None,5)
      scaled_vecs = Multiply()([lstm,attention_vals])
      # Sum up all scaled timesteps into 1 vector 
      # i.e. obtain a weighted sum of timesteps
      # Output shape is (5,) : Observe the time dimension got collapsed
      context_vector = Lambda(lambda x: K.sum(x,axis=1))(scaled_vecs)
      ##Attention Block over
      # Get the output out
      out = Dense(1,activation='sigmoid')(context_vector)
    
      model = Model(inp, out)
      model_with_attention_output = Model(inp, [out, attention_vals])
      model.compile(optimizer='adam',loss='binary_crossentropy')
      return model, model_with_attention_output
    
    model,model_with_attention_output = create_models()
    
    
    model.fit(np.array([[1,2,3]]),[1],batch_size=1)
    print ('Attention Over each word: ',model_with_attention_output.predict(np.array([[1,2,3]]),batch_size=1)[1])
    

    输出将是numpy数组,每个单词具有注意值 - 值越高,单词越重要

    编辑:您可能希望用embs替换乘法中的lstm以获得更好的解释,但这会导致更差的性能......

相关问题