【发布时间】:2021-03-20 07:36:18
【问题描述】:
我试图填充一个数据集,该数据集在第一维中包含不同长度的张量。长度是不超过 13 的任意数字,我想在第一个维度的前面填充零。当我将它应用于张量时,该函数似乎工作正常,但数据集没有像我预期的那样返回 (13,128) 的形状。我得到了 (None, None) 的形状。
代码如下:
print(train_dataset_filtered.element_spec, '\n')
def pad_seq(eng, ger):
n = 13 - tf.shape(eng)[0]
paddings = tf.concat(([[n,0]], [[0,0]]), axis=0)
return tf.pad(eng, paddings), ger
print(pad_seq(tf.ones((4,128)), tf.ones((14,))), '\n')
print(train_dataset_filtered.map(pad_seq).element_spec)
输出如下:
(TensorSpec(shape=(None, 128), dtype=tf.float32, name=None), TensorSpec(shape=(14,), dtype=tf.int32, name=None))
(<tf.Tensor: id=402, shape=(13, 128), dtype=float32, numpy=
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[1., 1., 1., ..., 1., 1., 1.],
[1., 1., 1., ..., 1., 1., 1.],
[1., 1., 1., ..., 1., 1., 1.]], dtype=float32)>, <tf.Tensor: id=388, shape=(14,), dtype=float32, numpy=
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
dtype=float32)>)
(TensorSpec(shape=(None, None), dtype=tf.float32, name=None), TensorSpec(shape=(14,), dtype=tf.int32, name=None))
【问题讨论】:
标签: tensorflow2.0 tensorflow-datasets