【问题标题】:How to feed time series data into an autoencoder network for feature extraction?如何将时间序列数据输入自动编码器网络以进行特征提取?
【发布时间】:2019-09-19 02:50:36
【问题描述】:

我正在尝试为我的数据集从头开始创建自动编码器。它是一种用于特征提取的变分自动编码器。我对机器学习很陌生,我想知道如何将我的输入数据提供给自动编码器。

我的数据是时间序列数据。如下所示:

array([[[  10,   0,   10, ..., 10,   0,   0],
        ...,

        [  0,   12,   32, ...,  2,  2,  2]],

         [[ 0,  3,  7, ...,  7,  3,  0],
        .....
        [ 0,  2,  3, ...,  3,  4,  6]],

       [[1, 3, 1, ..., 0, 10, 2],
        ...,

        [2, 11, 12, ..., 1, 1, 8]]], dtype=int64)

它是一堆数组,形状是(3, 1212, 700)。 我在哪里传递标签?

网上的例子很简单,没有详细说明如何在现实中提供数据。任何示例或解释都会非常有帮助。

【问题讨论】:

  • 您的模型有内部存储器吗?还是只是通过滚动输入和输出将输入映射到输出?
  • 你想预测什么?是下一个时间步的值吗?
  • 您好,没有内存。并且正在使用这个模型来提取特征,我不想预测。这是可能的吧?从理论上讲,自动编码器将输入映射到潜在空间,从而减少了特征。所以我们只得到有用的功能。
  • 我的错误,是的,您可以使用自动编码器来制作特征提取器,我可以快速确认一件事:您有 3 个通道,每个通道有 1212 个时间数据点,总共有 700 个样本?
  • 是的,这是正确的! 700 个样本,它是 700 列,时间范围从 0.00 秒开始。

标签: python arrays machine-learning artificial-intelligence autoencoder


【解决方案1】:

这可以使用生成器来解决。生成器获取 700 个数据点的时间序列数据,每个数据点有 3 个通道和 1212 个时间步长,并输出一个批次。 在我编写的示例中,批次都是相同的时间段,例如批次 0 是 700 个样本中每个样本的前 10 个时间步长,批次 1 是 700 个样本中每个样本的时间步长 1:11。如果你想以某种方式混合它,那么你应该编辑生成器。当每个批次都经过测试和训练时,这个时期就结束了。对于神经网络来说,一个非常简单的编码器、解码器模型就足以证明这个概念——但你可能会想用你自己的模型来替换。变量 n 用于确定自动编码器使用了多少时间步。

import numpy as np
import pandas as pd
import keras
from keras.layers import Dense, Flatten
from tensorflow.python.client import device_lib
# check for my gpu 
print(device_lib.list_local_devices())


# make some fake data

# your data
data = np.random.random((3, 1212, 700))

# this is a generator
def image_generator(data, n):
    start = 0
    end = n
    while end < data.shape[1] -1:
        last_n_steps = data[:,start:end].T
        yield (last_n_steps, last_n_steps)
        start +=1
        end +=1
        # the generator MUST loop
        if end == data.shape[1] -1:
            start = 0
            end = n

n = 10
# basic model - replace with your own
encoder_input = Input(shape = (n,3), name = "encoder_input")
fc = Flatten()(encoder_input)
fc = Dense(100, activation='relu',name = "fc1")(fc)
encoder_output = Dense(5, activation='sigmoid',name = "encoder_output")(fc)

encoder = Model(encoder_input,encoder_output)

decoder_input = Input(shape = encoder.layers[-1].output_shape[1:], name = "decoder_input")
fc = Dense(100, activation='relu',name = "fc2")(decoder_input)
output = Dense(5, activation='sigmoid',name = "output")(fc)

decoder = Model(decoder_input,output)

combined_model_input = Input(shape = (n,3), name = "combined_model_input")
autoencoder = Model(combined_model_input, decoder(encoder(combined_model_input)))

model = Model(input_layer,output_layer)
model.compile(optimizer="adam", loss='mean_squared_error')
print(model.summary())

#and training

training_history = model.fit_generator(image_generator(data, n),
                    epochs =5,
                    initial_epoch = 0,
                    steps_per_epoch=data.shape[2]-n,
                    verbose=1
                   )

【讨论】:

  • 太棒了,这就是我想要的!运行模型后将更新我的答案..但再次感谢:)
  • 所以总结一下......对于特征提取,我们不需要数据集中的标签,对吧?
  • 我将自动编码器模型拆分为编码器和解码器,生成器将 (last_n_steps, last_n_steps) 生成为 (input, output)。因此,自动编码器被训练以提供与输入匹配的输出。在训练期间,将训练两个模型:“编码器”、“解码器”,稍后您可以只使用“编码器”模型进行特征提取。这个答案实际上并不是关于如何构建自动编码器的教程,但基本上 encoder_output 层决定了提取的特征数量。
猜你喜欢
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 2012-01-12
  • 2023-02-22
  • 1970-01-01
相关资源
最近更新 更多