【问题标题】:RNN with simultaneous POS tagging and sentiment classification? [closed]具有同时 POS 标记和情感分类的 RNN? [关闭]
【发布时间】:2018-05-21 19:37:16
【问题描述】:

我正在处理一个需要同时执行词性 (POS) 标记和情绪分析的问题。我正在使用 Tensorflow,正在考虑使用 Keras。

我有大量的英语句子数据集,这些句子被标记了词性标签和情绪(消极、中性、积极)。

是否可以训练循环神经网络(普通 RNN、GRU 或 LSTM)来学习同时 POS标记和情感分类?当然,在测试期间,我想输入一个句子,让 RNN 生成词性标签和情绪的预测。

我正在考虑以下 RNN 架构。我不确定是否可以使用 Tensorflow(我一直在使用)或 Keras(我现在正在学习)。我之前实现的 RNN 只完成一项任务,而不是两项。

感谢您的帮助。

【问题讨论】:

    标签: tensorflow machine-learning keras deep-learning recurrent-neural-network


    【解决方案1】:

    一个可能适用于 POS 标记的非常简单的 Keras 模型可能如下所示:

    from keras.layers import Dense, LSTM
    from keras.models import Model, Sequential
    
    
    model = Sequential()
    model.add(
        LSTM(
            hidden_layer_size,
            return_sequences=True,
            input_shape=(seq_length, nb_words),
            unroll=True
        )
    )
    model.add(Dense(nb_pos_types, activation="softmax"))
    model.compile(loss="categorical_crossentropy", optimizer="rmsprop")
    

    我假设各种参数:

    • hidden_layer_size:内部循环层的任何维度。
    • seq_length:输入序列长度。
    • nb_words:词汇量大小,用于单热编码输入,详细说明哪个单词对应哪个序列位置。
    • nb_pos_types:可能的不同 POS 标签的数量(用于 one-hot 编码标签)。

    目标是像这样修改一个简单的网络,以便它也可以对情绪进行预测(不清楚你的情绪是分数还是类别标签,但我会假设一个类别标签),因此损失函数包含对该情绪预测的惩罚项。

    有很多方法可以做到这一点,但一种常见的方法是从某个早期层“分叉”模型的新辐条,并让该辐条产生额外的预测(通常称为“多任务”或“联合任务”学习)。

    为此,我们将从Sequential 开始,但将其重命名为base_model,以明确它在为多个任务分支之前用作基础层集。然后我们将使用 Keras 的函数式语法对每个分支执行我们需要的操作,然后将它们组合在一起作为 final_model 的多个输出,其中我们可以表达每个输出的整体损失函数的一部分。

    我们可以通过以下方式修改上面的示例:

    base_model = Sequential()
    base_model.add(
        LSTM(
            hidden_layer_size,
            return_sequences=True,
            input_shape=(seq_length, nb_words),
            unroll=True
        )
    )
    
    # Get a handle to the output of the recurrent layer.
    rec_output = base_model.outputs[0]
    
    # Create a layer representing the POS prediction.
    pos_spoke = Dense(nb_pos_types, activation="softmax", 
                      name="pos")(rec_output)
    
    # Create a layer representing the sentiment prediction.
    # I assume `nb_sentiments` is the number of sentiment categories.
    sentiment_spoke = Dense(nb_sentiments, activation="softmax", 
                            name="sentiment")(rec_output)
    
    # Reunify into a single model which takes the same inputs as
    # determined for `base_model`, and provides a list of 2 outputs,
    # one for each spoke (POS and sentiment).
    final_model = Model(inputs=base_model.inputs, 
                        outputs=[pos_spoke, sentiment_spoke])
    
    # Finally, use a dictionary for the loss function to specify the
    # loss for each output, and optionally separate weights for when
    # the losses are added as a weighted sum for the total loss.    
    final_model.compile(
        optimizer='rmsprop',
        loss={'pos': 'categorical_crossentropy', 
              'sentiment': 'categorical_crossentropy'},
        loss_weights={'pos': 1.0, 'sentiment': 1.0}
    )
    

    最后,当调用final_model.fit 时,您将提供一个标签列表,其中包含与每个输出相关联的两个张量或标签数组。

    您可以阅读更多关于多输出损耗和架构的信息at the Keras docs on multi-input and multi-output models

    最后,请注意,这是一个非常简单的模型(并且可能表现不佳 - 它仅用于说明)。如果您有特定的 POS 特定或情绪特定的架构,您可以使用我们创建的辐条 pos_spokesentiment_spoke 来拥有具有更复杂网络拓扑的附加层。

    与其直接将它们定义为 Dense,不如将​​它们定义为额外的循环层,甚至可能是卷积层等,然后是一些最终的 Dense 层,其变量名称和层名称将用于输出和损失。

    还要注意此处使用return_sequences=True。这允许在序列中的每个步骤进行 POS情绪预测,即使您可能只关心最后的情绪预测。一种可能的选择是修改 sentiment_spoke 以仅对来自 rec_output 的最终序列元素进行操作,或者另一种(不太可能)的选择是为输入序列中的每个单词重复句子的整体情感标签。

    【讨论】:

    • 你不会找到很多更好的答案
    • 谢谢。如果其他人感兴趣,我的问题的适当术语是“多任务学习”。但是,使用 Keras 解决多任务学习的资源或 Stackoverflow 答案并不多。
    猜你喜欢
    • 2012-10-01
    • 2020-12-01
    • 1970-01-01
    • 2022-01-15
    • 2020-02-12
    • 2017-08-02
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多