我正在尝试为我的数据集创建一个自动编码器。它是用于特征提取的各种自动编码器。我对机器学习非常陌生,我想知道如何将输入数据提供给自动编码器。
我的数据是时间序列数据。看起来如下:
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个数据点的时间序列数据,并输出一批。在示例中,我写了批处理的每个时间段,例如,批次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
)