Paddle 使用next来迭代数据集时,Tensor形状发生改变

plicqrtu  于 2021-11-30  发布在  Java
关注(0)|答案(2)|浏览(270)

PaddlePaddle版本:2.1.2
自定义的Dataset片段:
class AudioDataset(Dataset):
..............
defgetitem(self, idx):
p = self.files[idx]
filename = os.path.basename(p)
speaker = filename.split(sep='_', maxsplit=1)[0]
label = self.encoder.transform([speaker])[0]
mcep = np.load(p)
mcep = paddle.to_tensor(mcep, dtype='float32')
mcep = paddle.unsqueeze(mcep, 0)
print(paddle.to_tensor(speakers.index(speaker), dtype='int64'))
return mcep, paddle.to_tensor(speakers.index(speaker), dtype='int64'), paddle.to_tensor(label, dtype='float32')

测试:
def data_loader(datadir: str, batch_size=4, shuffle=True, num_workers=2):
dataset = AudioDataset(datadir)
loader = DataLoader(dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers)
return loader
dloader = data_loader(config.data_dir, batch_size=config.batch_size, num_workers=config.num_workers)
data_iter = iter(dloader)
x,index,label=next(data_iter)
print('Test')
print(index)

输出:
Tensor(shape=[1], dtype=int64, place=CPUPlace, stop_gradient=True,
[3])
Tensor(shape=[1], dtype=int64, place=CPUPlace, stop_gradient=True,
[2])
Test
Tensor(shape=[1, 1], dtype=int64, place=CPUPlace, stop_gradient=True,
3)
Tensor(shape=[1], dtype=int64, place=CPUPlace, stop_gradient=True,
[1])

68de4m5k

68de4m5k1#

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

5fjcxozz

5fjcxozz2#

@abcdefghHIM 您好,to_tensor只有在做将scalar转换为Tensor时,会将其改变为shape为[1],请问是否存在这种情况呢?另外在dataset的def __getitem__函数中可以直接返回numpy,不用提前去做转换,dataloader迭代出的会自动转为Tensor

相关问题