You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

双向LSTM补全轨迹缺失位置:数值缺失标记方案问询

问题描述

我尝试用双向LSTM补全物体轨迹中缺失的空间位置[x, y, z]。训练时用mask_value = -1掩盖轨迹中的部分已知位置,以未掩盖的完整轨迹作为目标,思路类似掩码语言建模(填空任务,但这里是填充缺失数值)。但当前模型既没关注被掩盖的值,也没学会在输入未掩盖时复制对应值。想知道针对数值数据,有没有能明确指定需要预测目标的「缺失标记」方案?

示例数据

df_input = pd.DataFrame({'X': {0: 0.7819923262212789, 1: 0.8459722182552086, 2: -1.0, 3: 0.8694600744527876, 4: 0.854969390572484, 5: 0.801757542867478, 6: 0.741300353222318, 7: 0.8235697352260694, 8: -1.0, 9: 0.7546104557615212, 10: 0.7499357344834469, 11: 0.7829158454961754}, 
                         'Y': {0: 0.8626673097793318, 1: 0.8375979741951041, 2: -1.0, 3: 0.7829012420113346, 4: 0.8166525985771135, 5: 0.8517183166525982, 6: 0.8004341010490774, 7: 0.8551790666827446, 8: -1.0, 9: 0.823200289400699, 10: 0.8178704931870248, 11: 0.8507657060171225}, 
                         'Z': {0: 0.5208070617906684, 1: 0.4317360235393022, 2: -1.0, 3: 0.5188734762505255, 4: 0.5005464480874317, 5: 0.5905842791088692, 6: 0.4405632618747371, 7: 0.405380411937789, 8: -1.0, 9: 0.5386717108028583, 10: 0.4439680538041194, 11: 0.4710382513661201}})

df_target = pd.DataFrame({'X': {0: 0.7819923262212789, 1: 0.8459722182552086, 2: 0.8341664048441855, 3: 0.8694600744527876, 4: 0.854969390572484, 5: 0.801757542867478, 6: 0.741300353222318, 7: 0.8235697352260694, 8: 0.8037283520417406, 9: 0.7546104557615212, 10: 0.7499357344834469, 11: 0.7829158454961754}, 
                          'Y': {0: 0.8626673097793318, 1: 0.8375979741951041, 2: 0.8172555167008316, 3: 0.7829012420113346, 4: 0.8166525985771135, 5: 0.8517183166525982, 6: 0.8004341010490774, 7: 0.8551790666827446, 8: 0.8476546484987337, 9: 0.823200289400699, 10: 0.8178704931870248, 11: 0.8507657060171225}, 
                          'Z': {0: 0.5208070617906684, 1: 0.4317360235393022, 2: 0.4711223203026479, 3: 0.5188734762505255, 4: 0.5005464480874317, 5: 0.5905842791088692, 6: 0.4405632618747371, 7: 0.405380411937789, 8: 0.4345102984447247, 9: 0.5386717108028583, 10: 0.4439680538041194, 11: 0.4710382513661201}})

原示例模型

import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow.keras as K

from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Bidirectional
from keras.layers import Dense
from keras.layers import TimeDistributed

# define model
n_timesteps = 12
batch_size = 1
mask_value = -1
ndim_per_time_input = 3 # only 3 readout per input (x, y, z)
ndim_per_time_output = 3 # only 3 readout per output (x, y, z)

# reformat input
X = df_input.values
Y = df_target.values
X = X.reshape(1, n_timesteps, ndim_per_time_input)
Y = Y.reshape(1, n_timesteps, ndim_per_time_output)

#### define LSTM ####
model = Sequential()

## masking layer
input_shape = (n_timesteps, ndim_per_time_input)

## input layer
model.add(Bidirectional(LSTM(20, 
                             return_sequences = True,
                             dropout = 0.2), input_shape=input_shape, merge_mode='ave'))

## output layer
model.add(TimeDistributed(Dense(ndim_per_time_output, activation = 'linear')))

## loss function
model.compile(loss='mean_squared_error', optimizer = 'adam') 

# train LSTM
for epoch in range(1000):
    model.fit(X, Y, epochs = 1, batch_size = batch_size, verbose=2)

解决方案

1. 显式添加掩码标识特征

给输入数据新增一个维度,明确标记每个时间步的每个坐标是否需要预测:

  • 标记值1表示该位置是被掩码的,需要模型预测
  • 标记值0表示该位置是已知的,模型应直接复制输入值

这样模型能清晰识别需要处理的目标位置,避免混淆。

2. 使用掩码加权损失函数

不要用全局均方误差(MSE),而是只计算被掩码位置的损失,让模型专注于需要预测的部分,同时鼓励模型在非掩码位置复制输入值。

3. 正确集成Keras Masking层

原模型没有添加Masking层,导致LSTM会处理-1这个掩码值,干扰模型学习。添加Masking层后,LSTM会自动忽略掩码值对应的时间步输入。

4. 增加训练数据多样性

原模型仅用1条样本训练,无法泛化。需要生成大量带随机掩码的样本,让模型学习不同的轨迹补全模式。


修改后的完整代码
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Bidirectional, Dense, TimeDistributed, Masking

# 配置参数
n_timesteps = 12
batch_size = 1
mask_value = -1
ndim_coords = 3  # x, y, z

# 处理输入数据,添加掩码标识
df_input = pd.DataFrame({'X': {0: 0.7819923262212789, 1: 0.8459722182552086, 2: -1.0, 3: 0.8694600744527876, 4: 0.854969390572484, 5: 0.801757542867478, 6: 0.741300353222318, 7: 0.8235697352260694, 8: -1.0, 9: 0.7546104557615212, 10: 0.7499357344834469, 11: 0.7829158454961754}, 
                         'Y': {0: 0.8626673097793318, 1: 0.8375979741951041, 2: -1.0, 3: 0.7829012420113346, 4: 0.8166525985771135, 5: 0.8517183166525982, 6: 0.8004341010490774, 7: 0.8551790666827446, 8: -1.0, 9: 0.823200289400699, 10: 0.8178704931870248, 11: 0.8507657060171225}, 
                         'Z': {0: 0.5208070617906684, 1: 0.4317360235393022, 2: -1.0, 3: 0.5188734762505255, 4: 0.5005464480874317, 5: 0.5905842791088692, 6: 0.4405632618747371, 7: 0.405380411937789, 8: -1.0, 9: 0.5386717108028583, 10: 0.4439680538041194, 11: 0.4710382513661201}})

df_target = pd.DataFrame({'X': {0: 0.7819923262212789, 1: 0.8459722182552086, 2: 0.8341664048441855, 3: 0.8694600744527876, 4: 0.854969390572484, 5: 0.801757542867478, 6: 0.741300353222318, 7: 0.8235697352260694, 8: 0.8037283520417406, 9: 0.7546104557615212, 10: 0.7499357344834469, 11: 0.7829158454961754}, 
                          'Y': {0: 0.8626673097793318, 1: 0.8375979741951041, 2: 0.8172555167008316, 3: 0.7829012420113346, 4: 0.8166525985771135, 5: 0.8517183166525982, 6: 0.8004341010490774, 7: 0.8551790666827446, 8: 0.8476546484987337, 9: 0.823200289400699, 10: 0.8178704931870248, 11: 0.8507657060171225}, 
                          'Z': {0: 0.5208070617906684, 1: 0.4317360235393022, 2: 0.4711223203026479, 3: 0.5188734762505255, 4: 0.5005464480874317, 5: 0.5905842791088692, 6: 0.4405632618747371, 7: 0.405380411937789, 8: 0.4345102984447247, 9: 0.5386717108028583, 10: 0.4439680538041194, 11: 0.4710382513661201}})

# 生成掩码标识:1表示需要预测,0表示已知
mask_indicator = (df_input == mask_value).astype(int).values
# 合并坐标数据
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 01:10:28