弹道预测的编解码器 [英] Encoder-Decoder for Trajectory Prediction
问题描述
我需要使用编码器-解码器结构来预测2D轨迹.由于几乎所有可用的教程都与NLP有关(具有稀疏向量),因此我不确定如何使解决方案适应连续数据.
I need to use encoder-decoder structure to predict 2D trajectories. As almost all available tutorials are related to NLP -with sparse vectors-, I couldn't be sure about how to adapt the solutions to a continuous data.
除了我对序列到序列模型的无知之外,embedding
单词处理过程使我更加困惑.我有一个数据集,其中包含3,000,000个样本,每个样本具有x-y
坐标(-1,1)和125
个观测值,这意味着每个样本的形状为(125, 2)
.我以为我可以将其视为125个单词,其中已经嵌入了二维单词,但是此
In addition to my ignorance in seqence-to-sequence models, embedding
process for words confused me more. I have a dataset that consists of 3,000,000 samples each having x-y
coordinates (-1, 1) with 125
observations, which means the shape of each sample is (125, 2)
. I thought I could think of this as 125 words with 2 dimensional already embedded words, but the encoder and the decoder in this Keras Tutorial expect 3D arrays as (num_pairs, max_english_sentence_length, num_english_characters)
.
我怀疑是否需要使用此模型分别训练每个样本(125, 2)
,就像Google的搜索栏只写一个字一样.
I doubt I need to train each sample (125, 2)
separately with this model, as the way Google's search bar does with only one word written.
据我了解,编码器是many-to-one
型模型,而解码器是one-to-many
型模型.我需要获取一个内存状态c
和一个隐藏状态h
作为vectors(?).然后,我应该将这些向量用作解码器的输入,并提取与我确定的编码器输出一样多的(x,y)形状的预测.
As far as I understood, an encoder is many-to-one
type model and a decoder is one-to-many
type model. I need to get a memory state c
and a hiddenstate h
as vectors(?). Then I should use those vectors as input to decoder and extract predictions in the shape of (x,y) as many as I determine as encoder output.
如果有人能在我的数据集形状上提供一个编码器-解码器LSTM体系结构的示例,尤其是在编码器-解码器输入和输出所需的尺寸方面,尤其是在Keras模型上,我将非常感激.
I'd be so thankful if someone could give an example of an encoder-decoder LSTM architecture over the shape of my dataset, especially in terms of dimensions required for encoder-decoder inputs and outputs, particulary on Keras model if possible.
推荐答案
我假设您要与前125个时间步一起预测50个时间步(作为示例).我为您提供了时间序列的最基本的编码器-解码器结构,但是可以进行改进(使用 Luong Attention 例如).
I assume you want to forecast 50 time steps with the 125 previous ones (as an example). I give you the most basic Encoder-Decoder Structure for time Series but it can be improved (with Luong Attention for instance).
from tensorflow.keras import layers,models
input_timesteps=125
input_features=2
output_timesteps=50
output_features=2
units=100
#Input
encoder_inputs = layers.Input(shape=(input_timesteps,input_features))
#Encoder
encoder = layers.LSTM(units, return_state=True, return_sequences=False)
encoder_outputs, state_h, state_c = encoder(encoder_inputs) # because return_sequences=False => encoder_outputs=state_h
#Decoder
decoder = layers.RepeatVector(output_timesteps)(state_h)
decoder_lstm = layers.LSTM(units, return_sequences=True, return_state=False)
decoder = decoder_lstm(decoder, initial_state=[state_h, state_c])
#Output
out = layers.TimeDistributed(Dense(output_features))(decoder)
model = models.Model(encoder_inputs, out)
所以这里的核心思想是:
So the core idea here is :
- 将时间序列编码为两种状态:
state_h
和state_c
.检查此以了解LSTM单元的工作. - 重复
state_h
您要预测的时间步数 - 使用LSTM对初始状态进行解码,该初始状态由编码器计算
- 使用密集层来调整每个时间步所需的要素数量
- Encode the time series into two states :
state_h
andstate_c
. Check this to understand the work of LSTM cells. - Repeat
state_h
the number of time steps you want to forecast - Decode using an LSTM with initial states calculated by the encoder
- Use a Dense layer to shape the number of needed features for each time steps
我建议您测试我们的建筑并使用model.summary()
和tf.keras.utils.plot_model(mode,show_shapes=True)
对其进行可视化.对于摘要,它可以为您提供良好的表示形式:
I advise you to test our achtecture and visualize them with model.summary()
and tf.keras.utils.plot_model(mode,show_shapes=True)
. It gives you good representations like, for the summary :
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_5 (InputLayer) [(None, 125, 2)] 0
__________________________________________________________________________________________________
lstm_8 (LSTM) [(None, 100), (None, 41200 input_5[0][0]
__________________________________________________________________________________________________
repeat_vector_4 (RepeatVector) (None, 50, 100) 0 lstm_8[0][1]
__________________________________________________________________________________________________
lstm_9 (LSTM) (None, 50, 100) 80400 repeat_vector_4[0][0]
lstm_8[0][1]
lstm_8[0][2]
__________________________________________________________________________________________________
time_distributed_4 (TimeDistrib (None, 50, 2) 202 lstm_9[0][0]
==================================================================================================
Total params: 121,802
Trainable params: 121,802
Non-trainable params: 0
__________________________________________________________________________________________________
并绘制模型:
这篇关于弹道预测的编解码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!