【问题标题】:Get the probability of a word for text classification with LSTM in Keras在 Keras 中使用 LSTM 获取单词用于文本分类的概率
【发布时间】:2020-07-15 21:54:22
【问题描述】:

我正在使用带有 Keras 的 LSTM 进行情感分类,我想获得 LSTM 分配给句子中每个单词的概率,以便知道哪些单词更具代表性。

例如,对于下面的句子:

“这片风景美妙而平静”

我认为将句子分类为正面的最具代表性的词是“精彩”和“平静”词。

如何获得 LSTM 分配给每个单词的概率?

lstm_layer = layers.LSTM(size)(embedding_layer)

output_layer1 = layers.Dense(50, activation=activation)(lstm_layer)
output_layer1 = layers.Dropout(0.25)(output_layer1)
output_layer2 = layers.Dense(1, activation="sigmoid")(output_layer1)

model = models.Model(inputs=input_layer, outputs=output_layer2)
model.compile(optimizer=optimizer, loss='binary_crossentropy')

谢谢

【问题讨论】:

    标签: keras classification lstm analysis


    【解决方案1】:

    您可以从最后一层(使用 softmax 的密集层)获得概率。示例模型:

    import keras
    import keras.layers as L
    
    # instantiate sequential model
    model = keras.models.Sequential()
    
    # define input layer
    model.add(L.InputLayer([None], dtype='int32'))
    
    # define embedding layer for dictionary size of 'len(all_words)' and 50 features/units
    model.add(L.Embedding(len(all_words), 50))
    
    # define fully-connected RNN with 64 output units. Crucially: we return the outputs of the RNN for every time step instead of just the last time step
    model.add(L.SimpleRNN(64, return_sequences=True))
    
    # define dense layer of 'len(all_words)' outputs and softmax activation
    # this will produce a vector of size len(all_words)
    stepwise_dense = L.Dense(len(all_words), activation='softmax')
    
    # The TimeDistributed layer adds a time dimension to the Dense layer so that it applies across the time dimension for every batch
    # That is, TimeDistributed applies the Dense layer to each time-step (input word) independently. Without it, the Dense layer would apply only once to all of the time-steps concatenated.
    # So, for the given time step (input word), each element 'i' in the output vector is the probability of the ith word from the target dictionary
    stepwise_dense = L.TimeDistributed(stepwise_dense)
    model.add(stepwise_dense)
    

    然后,编译并拟合(训练)您的模型:

    model.compile('adam','categorical_crossentropy')
    
    model.fit_generator(generate_batches(train_data),len(train_data)/BATCH_SIZE,
                        callbacks=[EvaluateAccuracy()], epochs=5,)
    

    最后 - 只需使用预测函数来获得概率:

    model.predict(input_to_your_network)
    

    为了清楚起见,softmax 层的第 i 个输出单元表示第 i 类的预测概率(also see here)

    【讨论】:

    • 非常感谢。您展示的示例是获取句子中每个单词或完整句子的概率?你能告诉我如何调整我的代码吗?我在我的问题中写了它。
    • 刚刚在我的代码中添加了澄清 cmets。 IE。对于每个时间步(给定句子中的输入单词),我的答案中的模型将计算一个概率向量。该向量中的每个概率 i 表示输入单词在目标词典中出现 ith 个单词的可能性。
    • 我修改了代码,但出现以下错误:AssertionError
    • 这是我的代码: emb_layer = SpatialDropout1D(0.3)(embedding_layer) lstm_layer = LSTM(size, return_sequences = True)(embedding_layer) output_layer = TimeDistributed(layers.Dense(len(word_index) + 1, activation="softmax"))(lstm_layer) model = models.Model(inputs=input_layer, outputs=output_layer) model.compile(opt=opt, loss='categorical_crossentrophy')
    • 您好,我回答了您发布的问题...“如何获得 LSTM 分配给每个单词的概率?”。因此,接下来的步骤是专门对模型进行故障排除,然后使用 model.predict 来获取概率
    猜你喜欢
    • 2019-02-21
    • 2018-11-02
    • 2019-10-25
    • 1970-01-01
    • 2019-05-26
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    相关资源
    最近更新 更多