繁体   English   中英

在tensorflow中使用1D CNN对可变长度的时间序列进行分类

[英]Classification of time series of variable lengths using 1D CNN in tensorflow

我有一个由不同长度的时间序列组成的数据集。 例如,考虑这个

ts1 = np.random.rand(230, 4)
ts2 = np.random.rand(12309, 4)

我有 200 个数组列表形式的序列

input_x = [ts1, ts2, ..., ts200]

这些时间序列的标签为 1(如果好)和 0(如果不好)。 因此我的标签将类似于

labels = [0, 0, 1, 0, 1, ....] 

我正在构建一个 keras 模型,如下所示:

model = keras.Sequential([
keras.layers.Conv1D(64, 3, activation='relu', input_shape=(None, 4)),
keras.layers.MaxPool1D(3), 
keras.layers.Conv1D(160, 10, activation='relu'),
keras.layers.GlobalAveragePooling1D(),
keras.layers.Dropout(0.5),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(2, activation='softmax')

])

第一个卷积层的输入形状中的 4 对应于每个时间序列中的列数,该数是恒定的(将其视为有 4 个传感器返回不同操作的测量值)。 目标是对时间序列的好坏(0 或 1)进行分类,但是我无法弄清楚如何使用 keras 进行训练。

运行这条线

model.fit(input_x, labels, epochs=5, batch_size=1)

返回错误

Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 200 arrays

即使使用 np.array(input_x) 也会出错。 我怎样才能用可变长度的序列训练这个模型? 我知道填充是一种选择,但这不是我想要的。 另外,我不想使用带有滑动窗口的 RNN。 我真的在寻找一种使用 1D CNN 的解决方案,该解决方案适用于可变长度的序列。 任何帮助将不胜感激!

在处理时间序列时,您希望将 NN 的输入定义为(batch_size, sequence_length, features)

在您的情况下input_shape=(sequence_length, 4,)这对应于input_shape=(sequence_length, 4,) 您必须决定要处理的最大序列长度,以便进行训练和生成预测。

NN 的输入也需要是形状(batch_size、sequence_length、features)。

暂无
暂无

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

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