当我遇到这个问题时,我正在做一些数据科学工作,我试图为监督任务创建一个模型,其中输入和输出都是可变长度。
下面是一个关于输入和输出的示例:
Input[0]: [2.42, 0.43, -5.2, -54.9]
Output[0]: ['class1', 'class3', 'class12']
问题是,在我拥有的数据集中,输入和输出是可变长度的,所以为了能够使用整个数据集,我需要找到一种编码这些数据的方法。
首先,我对output类进行编码,并添加填充以等于数据集中的所有output长度,(假设是length=(6,)
):
Encoded_output[0]: [1, 3, 12, 0, 0, 0]
但是我不能找出编码输入的方法,因为,由于原始输入数据是浮点数,我不能创建编码并添加填充。我不知道我还有什么其他的选择,我想听听你是如何解决这个问题的。
解决这个问题的方法是:
- 找出变量数据的最大长度
- 找出每个训练实例的真实长度。
从这两个东西,你可以创建一个掩码,并让你的网络计算零梯度的东西,你想忽略。
:
import tensorflow as tf
# pretend the longest data instance we will have is 5
MAX_SEQ_LEN = 5
# batch of indices indicating true length of each instance
idxs = tf.constant([3, 2, 1])
# batch of variable-length data
rt = tf.ragged.constant(
[
[0.234, 0.123, 0.654],
[0.111, 0.222],
[0.777],
], dtype=tf.float32)
t = rt.to_tensor(shape=[3, MAX_SEQ_LEN])
print(t)
# tf.Tensor(
# [[0.234 0.123 0.654 0. 0. ]
# [0.111 0.222 0. 0. 0. ]
# [0.777 0. 0. 0. 0. ]], shape=(3, 5), dtype=float32)
# use indices to create a boolean mask. We can use this mask in
# layers in our network to ignore gradients
mask = tf.sequence_mask(idxs, MAX_SEQ_LEN)
print(mask)
# <tf.Tensor: shape=(3, 5), dtype=bool, numpy=
# array([[ True, True, True, False, False],
# [ True, True, False, False, False],
# [ True, False, False, False, False]])>
这个用例通常出现在rnn中。你可以看到在call()
方法中有一个mask
选项,你可以为可变长度的时间序列数据传递一个二进制掩码。