简体   繁体   English

具有更多功能/类别的LSTM

[英]LSTM with more features / classes

How can I use more than one feature/class as input/output on a LSTM using Sequential from Keras Models in Python? 如何使用Python的Keras模型中的Sequential在LSTM上使用多个功能/类作为输入/输出?

To be more specific, I would like to use as input and output to the network: [FeatureA][FeatureB][FeatureC]. 更具体地说,我想用作网络的输入和输出:[FeatureA] [FeatureB] [FeatureC]。 FeatureA is a categorial class with 100 different possible values indicating the sensor which collected the data; FeatureA是一个类别类,具有100个可能的值,指示传感器收集了数据; FeatureB is a on/off indicator, being 0 or 1; FeatureB是开/关指示器,为0或1; FeatureC is a categorial class too having 5 unique values. FeatureC也是一个类别类,也具有5个唯一值。

Data Example: 数据示例:

 1. 40,1,5
 2. 58,1,2
 3. 57,1,5
 4. 40,0,1
 5. 57,1,4
 6. 23,0,3

When using the raw data and loss='categorical_crossentropy' on model.compile, the loss is over than 10.0. 当使用原始数据并且在model.compile上使用loss ='categorical_crossentropy'时,损耗超过10.0。

When normalisating the data to values between 0-1 and using mean_squared_error on loss, it gets an average of 0.27 on loss. 将数据归一化为0-1之间的值,并使用均值损失平方误差(mean_squared_error)时,均值损失平均为0.27。 But when testing it on prediction, the results does not makes any sense. 但是,当对预测进行测试时,结果毫无意义。

Any suggestions here or tutorials I could consult? 有什么建议或我可以参考的教程吗? Thanks in advance. 提前致谢。

  1. You need to convert FeatureC to a binary category. 您需要将FeatureC转换为二进制类别。 Mean squared error is for regression and as best I can tell you are trying to predict which class a certain combination of sensors and states belongs to. 均方误差用于回归分析,据我所知,您正在尝试预测传感器和状态的某种组合所属的类别。 Since there's 5 possible classes you can kind of think that you're trying to predict if the class is Red, Green, Blue, Yellow, or Purple. 由于有5种可能的类别,您可以认为您正在尝试预测该类别是红色,绿色,蓝色,黄色还是紫色。 Right now those are represented by numbers but for a regression you model is going to be predicting values like 3.24 which doesn't make sense. 现在,这些由数字表示,但是对于回归模型,您将要预测3.24之类的值,这是没有意义的。

    In effect you're converting values of FeatureC to 5 columns of binary values. 实际上,您正在将FeatureC的值转换为5列的二进制值。 Since it seems like the classes are exclusive there should be a single 1 and the rest of the columns of a row would be 0s. 由于类似乎是互斥的,因此应该有一个1,而行的其余列为0。 So if the first row is 'red' it would be [1, 0, 0, 0, 0] 因此,如果第一行为“红色”,则为[1、0、0、0、0]

  2. For best results you should also convert FeatureA to binary categorical features. 为了获得最佳结果,您还应该将FeatureA转换为二进制分类特征。 For the same reason as above, sensor 80 is not 4x more than sensor 20, but instead a different entity. 由于与上述相同的原因,传感器80不比传感器20多4倍,而是一个不同的实体。

  3. The last layer of your model should be of softmax type with 5 neurons. 模型的最后一层应为带有5个神经元的softmax类型。 Basically your model is going to try to be predicting the probability of each class, in the end. 基本上,您的模型将最终尝试预测每个类别的概率。

It looks like you are working with a dataframe since there's an index. 由于有索引,因此您似乎正在使用数据框。 Therefore I would try: 因此,我会尝试:

import keras
import numpy as np
import pandas as pd # assume that this has probably already been done though

feature_a = data.loc[:, "FeatureA"].values  # pull out feature A
labels = data.loc[:, "FeatureC"].values   # y are the labels that  we're trying to predict
feature_b = data.loc[:, "FeatureB"].values  # pull out B to make things more clear later

# make sure that feature_b.shape = (rows, 1) otherwise reset the shape
# so hstack works later
feature_b = feature_b.reshape(feature_b.shape[0], 1)

labels -= 1  # subtract 1 from all labels values to zero base (0 - 4)
y = keras.utils.to_categorical(labels)
# labels.shape should be (rows, 5)

# convert 1-100 to binary columns
# zero base again
feature_a -= 1  

# Before: feature_a.shape=(rows, 1)
feature_a_data = keras.utils.to_categorical(feature_a)
# After: feature_a_data.shape=(rows, 100)

data = np.hstack([feature_a_data, feature_b])
# data.shape should be (rows, 101)
# y.shape should be (rows, 5)

Now you're ready to train/test split and so on. 现在,您可以训练/测试拆分等等。

Here's something to look at which has multi-class prediction: 以下是具有多类别预测的内容:

https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM