Keras中可变序列长度的序列到序列分类

我想在 TensorFlow/Keras 中训练 LSTM 或 GRU 网络,以根据来自运动传感器(加速度计和陀螺仪)的输入连续识别用户是否正在行走。我有 50 个输入序列,长度从 581 到 5629 个时间步长不等,6 个特征和 50 个相应的布尔值输出序列。我的问题是我不知道如何将训练数据提供给 fit() 方法。


我大致知道我需要做什么:我想训练 5 个批次,每个批次 10 个序列,对于每个批次,我必须填充除最长序列之外的所有序列,以便所有 10 个序列具有相同的长度并应用屏蔽。我只是不知道如何构建数据结构。我知道我可以制作一个大小为 (50,5629,6) 的大 3D 张量并且可以工作,但是它非常缓慢,所以我真的想让每批的序列长度尽可能小。


这是代码中的问题:


import tensorflow as tf

import numpy as np


# Load data from file

x_list, y_list = loadSequences("train.csv")


# x_list is now a list of arrays (n,6) of float64, where n is the timesteps

# and 6 is the number of features, sorted by increasing sequence lengths.

# y_list is a list of arrays (n,1) of Boolean.


x_train = # WHAT DO I WRITE HERE?

y_train = # AND HERE?


model = tf.keras.models.Sequential([

            tf.keras.layers.Masking(),

            tf.keras.layers.LSTM(32, return_sequences=True),

            tf.keras.layers.Dense(2, activation=tf.nn.softmax)

        ])

model.compile(optimizer='adam',

            loss='sparse_categorical_crossentropy',

            metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=10, epochs=100)


翻过高山走不出你
浏览 270回答 2
2回答

手掌心

如果它对某人有帮助,以下是我最终实施解决方案的方式:import tensorflow as tfimport numpy as np# Load data from filex_list, y_list = loadSequences("train.csv")# x_list is now a list of arrays (m,n) of float64, where m is the timesteps# and n is the number of features.# y_list is a list of arrays (m,1) of Boolean.assert len(x_list) == len(y_list)num_sequences = len(x_list)num_features = len(x_list[0][0])batch_size = 10batches_per_epoch = 5assert batch_size * batches_per_epoch == num_sequencesdef train_generator():    # Sort by length so the number of timesteps in each batch is minimized    x_list.sort(key=len)    y_list.sort(key=len)    # Generate batches    while True:        for b in range(batches_per_epoch):            longest_index = (b + 1) * batch_size - 1            timesteps = len(x_list[longest_index])            x_train = np.zeros((batch_size, timesteps, num_features))            y_train = np.zeros((batch_size, timesteps, 1))            for i in range(batch_size):                li = b * batch_size + i                x_train[i, 0:len(x_list[li]), :] = x_list[li]                y_train[i, 0:len(y_list[li]), 0] = y_list[li]            yield x_train, y_trainmodel = tf.keras.models.Sequential([            tf.keras.layers.Masking(mask_value=0., input_shape=(None,num_features)),            tf.keras.layers.LSTM(32, return_sequences=True),            tf.keras.layers.Dense(2, activation=tf.nn.softmax)        ])model.compile(optimizer='adam',            loss='sparse_categorical_crossentropy',            metrics=['accuracy'])model.fit_generator(train_generator(), steps_per_epoch=batches_per_epoch, epochs=100)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python