如何使用lstm执行多类多输出分类 [英] How to perform multiclass multioutput classification using lstm

查看:231
本文介绍了如何使用lstm执行多类多输出分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有多类多输出分类(请参阅 https://scikit-learn.org/stable/modules/multiclass.html 了解详情)。换句话说,我的数据集如下所示。

I have multiclass multioutput classification (see https://scikit-learn.org/stable/modules/multiclass.html for details). In other words, my dataset looks as follows.

node_name, timeseries_1, timeseries_2, label_1, label_2
node1, [1.2, ...], [1.8, ...], 0, 2
node2, [1.0, ...], [1.1, ...], 1, 1
node3, [1.9, ...], [1.2, ...], 0, 3 
...
...
...

所以,我的 label_1 可能是 0 1 ,而我的 label_2 可能是 0 1 2

So, my label_1 could be either 0 or 1, whereas my label_2 could be either 0, 1, or 2.

我当前代码如下。

def create_network():
    model = Sequential()
    model.add(LSTM(200, input_shape=(16,2)))
    model.add(Dense(100))
    model.add(Dropout(0.2))
    model.add(Dense(3, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

    return model

neural_network = KerasClassifier(build_fn=create_network, epochs=100, batch_size=100, verbose=0)

k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)

scores = cross_validate(neural_network, my_features, label_data_encoded, cv=k_fold, scoring = ('accuracy', 'precision_weighted', 'recall_weighted', 'f1_weighted', 'roc_auc'))

我的问题如下。


  • 由于我有两个标签(即 label_1 label_2 ),如何使这些标签适合lstm模型?我是否需要做类似 keras.utils.to_categorical(label_1,2) keras.utils.to_categorical(label_2,3)

  • 如何更改模型以使其适合多类多输出分类

  • Since I have two labels (i.e. label_1 and label_2), how to fit these labels to lstm model? Do I have to do something like keras.utils.to_categorical(label_1, 2) and keras.utils.to_categorical(label_2, 3)?
  • How to change the model in order to make it suitable for multiclass multioutput classification?

如果需要,我很乐意提供更多详细信息。

I am happy to provide more details if needed.

推荐答案

如果我理解正确,label_1是二进制的,而label_2是一个多类的问题,因此我们需要模型具有两个带有单独损失函数的输出;

If I understand correctly, label_1 is binary, whereas label_2 is a multiclass problem, so we need the model to have two outputs with separate loss functions; binary and categorical crossentropy respectively.

但是,顺序API不允许多个输入/输出。

However, Sequential API does not allow multiple input/output.


Sequential API允许您针对大多数问题逐层创建模型。其局限性在于它不允许您创建共享图层或具有多个输入或输出的模型。

The Sequential API allows you to create models layer-by-layer for most problems. It is limited in that it does not allow you to create models that share layers or have multiple inputs or outputs.

函数API创建两个输出层,并使用所需的损失函数编译模型。

You can use the functional API to create two output layers, and compile the model with required loss functions.

X=Input(input_shape)
X=Layer(X)
'
'
'
'
out1=Dense(1, activation='sigmoid')(X)
out2=Dense(3, activation='softmax')(X)
model = Model(inputs = input, outputs = [out1,out2])
model.compile(loss = ['binary_crossentropy','categorical_crossentropy'], loss_weights = [l1,l2], ...)

model.fit(input,[label_1, label_2_toCategotical]

网络将最小化的损失将是两个损失的加权总和,分别由l1和l2加权。

The loss that the network will minimize will be the weighted sum of the 2 losses, weighted by l1 and l2.

希望这会有所帮助:)

这篇关于如何使用lstm执行多类多输出分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆