所以我有一个我制作的 Dataset 类,它接受一个 3D numpy 数组和形状长度pack_padded_sequence
:
class MyDataset(data.Dataset):
def __init__(self, dataset, data_shape):
self.dataset = dataset
self.transform = MyToTensor(data_shape)
我创建了自己的 ToTensor 类:
class MyToTensor(object):
def __init__(self, data_shape):
self.data_shape = data_shape
def __call__(self, data):
data = torch.from_numpy(data)
return rnn.pack_padded_sequence(data, lengths=self.data_shape, batch_first=True)
但是由于某种原因,当print(list(MyDataset(dataset, data_shape)))
我得到一个正常的张量对象返回而没有删除填充时。
有关我的输入的更多信息,dataset
是一个按顺序排列的 3D numpy 数组:batch size, sequence length, features
并且 data_shape 是一个列表,其大小batch_size
与表示序列长度的数字相匹配。
序列长度也是从最高序列到最低序列大小的顺序
我的输入示例:
[[[0 0.33000001311302185 1]
[0 0.4300000071525574 1]
[0 0.3799999952316284 1]
...
[0 0.33000001311302185 1]
[0 0.28999999165534973 1]
[0 0.33000001311302185 1]]
[[6 0.800000011920929 3]
[5 0.7300000190734863 3]
[7 0.8199999928474426 3]
...
[4 0.699999988079071 3]
[5 0.7799999713897705 3]
[5 0.7799999713897705 3]]
[[3 1.0 5]
[3 1.0 5]
[3 1.0 5]
...
[3 1.0 5]
[3 1.0 5]
[3 1.0 5]]
...
[[4.0 0.7599999904632568 3.0]
[6.0 0.8100000023841858 3.0]
[6.0 1.0 3.0]
...
[nan nan nan]
[nan nan nan]
[nan nan nan]]
[[8.0 1.0 0.0]
[8.0 0.9100000262260437 0.0]
[9.0 1.0 0.0]
...
[nan nan nan]
[nan nan nan]
[nan nan nan]]
[[5.0 1.0 1.0]
[4.0 1.0 1.0]
[4.0 1.0 1.0]
...
[nan nan nan]
[nan nan nan]
[nan nan nan]]]
以及对应的data_shape:
(235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 235, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 232, 18, 18, 18)