我想在 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)
手掌心
相关分类