我有一个LSTM数据集。一些标签在末尾包含NaN,不能向后填充(因为它们后面没有值),向前填充它们没有任何意义(因为标签timestamp将在“更近的将来”被弃用-timestamp (=missing value locatoin)与其acutal timeindex相比)
那么:有没有办法屏蔽标签集(/Output-set)中的NaN值?(因为sample_weights只用于输入数据,就像看起来那样)。
发布于 2021-04-03 06:35:16
您可以通过Keras屏蔽层完成数据屏蔽:https://keras.io/api/layers/core_layers/masking/。
在遮罩层之后并支持遮罩的层( LSTM层支持)将跳过所有特征都等于该步骤的遮罩值的采样/步骤。
相反,可以通过以下方式使用遮罩层:在生成遮罩层时,首先将NaN值转换为0(默认遮罩值)或另一个值(如果明确指定为遮罩值):
from tensorflow.keras import layers
import numpy as np
# your example timesteps
ex_data = np.array([0.123, 0.437, 0.891, np.nan, 1.497, 1.1])
# reshape your example timesteps into a 3D matrix (1 sample x 6 timesteps x 1 feature per timestep)
data = np.reshape(ex_data, (1, 6, 1))
# set NaN values to 0, which is the default masking value
data[np.isnan(data)] = 0
masked_data = layers.Masking()(data)
print(masked_data._keras_mask)返回:
tf.Tensor([[ True True True False True True]], shape=(1, 6), dtype=bool)https://stackoverflow.com/questions/64255533
复制相似问题