简体   繁体   English

tf.keras (RNN) 层在运行 model.fit() 时出现问题

[英]tf.keras (RNN) Layer issues when running model.fit()

I'm building an RNN to analyze motion capture (MoCap) data using TensorFlow, Pandas, and Keras.我正在构建一个 RNN 来使用 TensorFlow、Pandas 和 Keras 分析运动捕捉 (MoCap) 数据。

About my data:关于我的数据:

  • Data is obtained through pandas.read_csv and has a shape of (832, 165)数据通过pandas.read_csv ,形状为(832, 165)
  • Each row denotes a whole frame of data in a movement sequence (832 frames)每行表示一个运动序列中的一整帧数据(832帧)
  • Each column denotes the rotational data for a joint (165 joints total)每列表示一个关节的旋转数据(共 165 个关节)

I'm attempting to feed the data in one row at a time.我试图一次输入一行数据。 The output should be the next frame in the movement sequence.输出应该是运动序列中的下一帧。 I keep running into different types of errors when running model.fit .运行model.fit时,我不断遇到不同类型的错误。

I've attached a series of photos representing the different attempts to make the model work.我附上了一系列照片,代表了使模型工作的不同尝试。 If someone could provide some guidance as to why it's not working and how to fix I'd greatly appreciate it.如果有人可以就为什么它不起作用以及如何修复提供一些指导,我将不胜感激。

As a side note, each version of my code is different.作为旁注,我的代码的每个版本都不同。 I'm okay with using any as long as it ends up working, so when providing feedback if you could identify which version of my code you're talking about?只要它最终工作,我就可以使用任何东西,所以在提供反馈时,如果你能确定你正在谈论我的代码的哪个版本?

Uses tf.data.Dataset as input使用tf.data.Dataset作为输入

Version 1 Code / Output版本 1 代码/输出

Version 2 Code / Output版本 2 代码/输出

Version 3: [Code] [Output]版本 3: [代码] [输出]

Uses pandas arrays for input and target使用 Pandas 数组作为输入和目标

Version 4 Code / Output版本 4 代码/输出

Version 5 Code / Output版本 5 代码/输出

Using Code 4 as a basis for troubleshooting, I noticed that you are passing incompatible shapes to the layers.使用代码 4作为故障排除的基础,我注意到您将不兼容的形状传递给图层。

This line model.add(keras.layers.InputLayer(input_shape = (N_TIMESTEPS, N_FEATURES))) expects your data to have the same shape.这一行model.add(keras.layers.InputLayer(input_shape = (N_TIMESTEPS, N_FEATURES)))期望您的数据具有相同的形状。 Whereas your data have (832, 165) , which is the N_SAMPLES on the first index and the N_FEATURES , the N_TIMESTEPS is missing .而你的数据有(832, 165)这是第一指标和N_FEATURESN_SAMPLES次,在N_TIMESTEPS丢失

First, you should create a modified dataset that will generate a shape of (N_SAMPLES, N_TIMESTEPS, N_FEATURES) .首先,您应该创建一个修改后的数据集,该数据集将生成(N_SAMPLES, N_TIMESTEPS, N_FEATURES)的形状。

Here is an example to generate a dummy dataset:以下是生成虚拟数据集的示例:

data = tf.random.normal((N_SAMPLES, N_TIMESTEPS, N_FEATURES))
target = tf.random.normal((N_SAMPLES, N_TIMESTEPS, N_FEATURES))

The N_TIMESTEPS in your data is important in LSTM as it determines how many TIME_STEPS to consider per update.数据中的N_TIMESTEPS在 LSTM 中很重要,因为它决定了每次更新要考虑多少 TIME_STEPS。

Here is the complete code used to simulate successful execution in Google Colab.这是用于在Google Colab 中模拟成功执行的完整代码

%tensorflow_version 2.x  # To ensure latest Tensorflow version in Google Colab

import tensorflow as tf
import tensorflow.keras as keras

print(tf.__version__) # Tensorflow 2.2.0-rc3

BATCH_SIZE = 1
N_TIMESTEPS = 10
#Data is obtained through pandas.read_csv and has a shape of (832, 165)
#Each row denotes a whole frame of data in a movement sequence (832 frames)
#Each column denotes the rotational data for a joint (165 joints total)
# N_SAMPLES = data.values.shape[0]
# N_FEATURES = data.values.shape[1]
N_SAMPLES  = 832
N_FEATURES = 165

def get_compiled_model():
  model = keras.Sequential()
  model.add(keras.layers.InputLayer(input_shape = (N_TIMESTEPS, N_FEATURES)))
  model.add(keras.layers.LSTM(35, activation = 'relu', return_sequences = True))
  model.add(keras.layers.LSTM(35, activation = 'relu', return_sequences = True))
  model.add(keras.layers.Dense(165, activation = 'tanh'))

  model.compile(optimizer = 'adam',
                loss = 'mse',
                metrics = ['accuracy'])

  return model

model = get_compiled_model()
model.summary()

data = tf.random.normal((N_SAMPLES, N_TIMESTEPS, N_FEATURES))
target = tf.random.normal((N_SAMPLES, N_TIMESTEPS, N_FEATURES))

model.fit(data, target, epochs = 15, batch_size = BATCH_SIZE, shuffle = False)

Hope this helps you.希望这对你有帮助。

You could read more about the Tensorflow Keras Guide using RNN in this link .您可以在此链接中阅读有关使用 RNNTensorflow Keras 指南的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM