我正在尝试为我的数据集从头开始创建一个自动编码器。它是一种用于特征提取的变分自编码器。我是机器学习的新手,我想知道如何将我的输入数据提供给自动编码器。
我的数据是时间序列数据。如下所示:
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)。 我在哪里传递标签?
网上的例子比较简单,实际如何喂数据没有详细说明。任何示例或解释都将非常有帮助。
最佳答案
这可以使用生成器来解决。生成器获取 700 个数据点的时间序列数据,每个数据点具有 3 个 channel 和 1212 个时间步长,并输出一个批处理。 在我写的例子中,每个批处理都是相同的时间段,例如批处理 0 是 700 个样本中每个样本的前 10 个时间步长,批处理 1 是 700 个样本中每个样本的时间步长 1:11。如果你想以某种方式混合它,那么你应该编辑生成器。当每个批处理都经过测试和训练时,epoch 结束。对于神经网络来说,一个非常简单的编码器、解码器模型就足以证明这个概念——但你可能会想用你自己的模型来替换。变量 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
)
关于python - 如何将时间序列数据输入自动编码器网络进行特征提取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55920986/