我正在尝试从头开始为我的数据集创建一个自编码器。这是一个用于特征提取的变分自编码器。我对机器学习还比较新手,我想知道如何将我的输入数据输入到自编码器中。
我的数据是时间序列数据,格式如下:
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个通道和1212个时间步长,并输出一个批次。在我写的例子中,每个批次都是相同的时间段,例如批次0是每个700个样本的前10个时间步长,批次1是每个700个样本的时间步长1:11。如果你想以某种方式混合这些数据,你应该编辑生成器。当每个批次都被测试和训练后,一个epoch就结束了。对于神经网络,一个非常简单的编码器、解码器模型足以证明概念 – 但你可能会想要用你自己的模型替换它。变量n用于确定自编码器使用多少个时间步长。
import numpy as npimport pandas as pdimport kerasfrom keras.layers import Dense, Flattenfrom tensorflow.python.client import device_lib# check for my gpu print(device_lib.list_local_devices())# make some fake data# your datadata = np.random.random((3, 1212, 700))# this is a generatordef 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 = nn = 10# basic model - replace with your ownencoder_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 trainingtraining_history = model.fit_generator(image_generator(data, n), epochs =5, initial_epoch = 0, steps_per_epoch=data.shape[2]-n, verbose=1 )