使用Conv1d在Python / Keras中自动过滤时间序列

Eng*_*ica 3 time-series python-3.x autoencoder keras tensorflow

它可能看起来像很多代码,但是大多数代码都是注释或格式,以使其更具可读性。

给定:
如果我定义感兴趣的变量“序列”,则如下:

# define input sequence
np.random.seed(988) 

#make numbers 1 to 100
sequence = np.arange(0,10, dtype=np.float16)

#shuffle the numbers
sequence = sequence[np.random.permutation(len(sequence))]

#augment the sequence with itself
sequence = np.tile(sequence,[15]).flatten().transpose()

#scale for Relu
sequence = (sequence - sequence.min()) / (sequence.max()-sequence.min())

sequence

# reshape input into [samples, timesteps, features]
n_in = len(sequence)
sequence = sequence.reshape((1, n_in, 1))
Run Code Online (Sandbox Code Playgroud)

问题:
如何在Keras的自动编码器中使用conv1d以合理的准确度估算此序列?

如果conv1d不适合此问题,您能告诉我编码器/解码器更合适的层类型是什么吗?

更多信息:
有关数据的要点:

  • 它是10个不同值的重复序列
  • 一个10步的滞后可以完美预测序列
  • 包含10个元素的字典应给出“预测给出的下一个”

我曾尝试对编码器和解码器部分(LSTM,密集,多层密集)的其他层进行预测,并且它们不断在0.0833的mse处碰到“墙”……这是0到1之间均匀分布的方差对我来说,一个好的自动编码器在解决这个简单问题上应该至少能够获得99.9%的准确度,因此“ mse”的准确度大大低于1%。

我无法转换conv1d,因为我弄乱了输入。关于如何使其工作似乎没有真正的好例子,而且我对这种总体架构还很陌生,对我来说并不明显。

链接:

Thi*_*ses 6

通过使用您的方法创建1000个样本的数据集,我可以使用Conv1d获得一个很好的自动编码器模型:

LEN_SEQ = 10

x = Input(shape=(n_in, 1), name="input")
h = Conv1D(filters=50, kernel_size=LEN_SEQ, activation="relu", padding='same', name='Conv1')(x)
h = MaxPooling1D(pool_size=2, name='Maxpool1')(h)
h = Conv1D(filters=150, kernel_size=LEN_SEQ, activation="relu", padding='same', name='Conv2')(h)
h = MaxPooling1D(pool_size=2,  name="Maxpool2")(h)
y = Conv1D(filters=150, kernel_size=LEN_SEQ, activation="relu", padding='same', name='conv-decode1')(h)
y = UpSampling1D(size=2, name='upsampling1')(y)
y = Conv1D(filters=50, kernel_size=LEN_SEQ, activation="relu", padding='same', name='conv-decode2')(y)
y = UpSampling1D(size=2, name='upsampling2')(y)
y = Conv1D(filters=1, kernel_size=LEN_SEQ, activation="relu", padding='same', name='conv-decode3')(y)

AutoEncoder = Model(inputs=x, outputs=y, name='AutoEncoder')

AutoEncoder.compile(optimizer='adadelta', loss='mse')

AutoEncoder.fit(sequence, sequence, batch_size=32, epochs=50)
Run Code Online (Sandbox Code Playgroud)

最后一个纪元输出:

Epoch 50/50
1000/1000 [==============================] - 4s 4ms/step - loss: 0.0104
Run Code Online (Sandbox Code Playgroud)

测试新数据:

array([[[0.5557],
        [0.8887],
        [0.778 ],
        [0.    ],
        [0.4443],
        [1.    ],
        [0.3333],
        [0.2222],
        [0.1111],
        [0.6665],
        [...]
Run Code Online (Sandbox Code Playgroud)

预测:

array([[[0.56822747],
        [0.8906583 ],
        [0.89267206],
        [0.        ],
        [0.5023574 ],
        [1.0665314 ],
        [0.37099048],
        [0.28558862],
        [0.05782872],
        [0.6886021 ],
        [...]
Run Code Online (Sandbox Code Playgroud)

一些舍入问题,但非常接近!

是您要找的东西吗?