数据加载程序顺序返回多个值,而不是列表或元组
def __init__():
def __len__():
def __getitem__(self, idx):
cat_cols = (self.cat_cols.values.astype(np.float32))
cont_cols = (self.cont_cols.values.astype(np.float32))
label = (self.label.astype(np.int32))
return (cont_cols[idx], cat_cols[idx], label[idx])
当我在上一类中使用数据加载程序时,我将cont_cols,cat_cols和标签作为索引0、1和2的输出。而我希望它们在一起。我尝试将返回值作为字典,但后来有索引问题。
我必须读取数据加载器的输出,因为
dl = DataLoader(dataset[0], batch_size = 1)
for i, data in enumerate(dl):
if i == 0:
cont = data
if i == 1:
cat = data
if i == 2:
label = data
目前的输出为
for i, data in enumerate(dl):
print(i, data)
0
量表([[[[3.2800E+02,4.8000e+01,1.0000e+03,1.4069e+03,4.6613e+05,5.3300e+04,5.3300e 0.0000E+00,5.0000e+00,1.0000e+00,1.0000e+00,2.0000e+00,7.1610e+04, 6.5100E+03,1.3020E+04,5.2080E+04,2.0040E+03]])
1张量([[2. 2.,1。,1。,1。,4。,4。,4。,2。,17.,0.,0.,2。 ,3。,0。,4。,4。,1。,2。,2,, 2.,10。,1。,1.]])
2张量([1],dtype = type = thech.int32)
我想要的是通过数据[0],数据[1]和数据[2]访问输出数据加载器只会给我退回数据[0]。它首先返回cont_cols,然后再返回cat_cols,然后标记。
def __init__():
def __len__():
def __getitem__(self, idx):
cat_cols = (self.cat_cols.values.astype(np.float32))
cont_cols = (self.cont_cols.values.astype(np.float32))
label = (self.label.astype(np.int32))
return (cont_cols[idx], cat_cols[idx], label[idx])
When I used the dataloader in the above class, I get the cont_cols, cat_cols and label as outputs with index 0, 1 and 2. Whereas I want them together. I have tried returning values as dictionary but then I have indexing issues.
I have to read the output of dataloader as
dl = DataLoader(dataset[0], batch_size = 1)
for i, data in enumerate(dl):
if i == 0:
cont = data
if i == 1:
cat = data
if i == 2:
label = data
Currently my output for
for i, data in enumerate(dl):
print(i, data)
is
0 tensor([[3.2800e+02, 4.8000e+01, 1.0000e+03, 1.4069e+03, 4.6613e+05, 5.3300e+04,
0.0000e+00, 5.0000e+00, 1.0000e+00, 1.0000e+00, 2.0000e+00, 7.1610e+04,
6.5100e+03, 1.3020e+04, 5.2080e+04, 2.0040e+03]])
1 tensor([[ 2., 1., 1., 4., 2., 17., 0., 2., 3., 0., 4., 4., 1., 2.,
2., 10., 1.]])
2 tensor([1], dtype=torch.int32)
What I want is the output to be accessed by data[0], data[1] and data[2] but the dataloader gives me back only data[0]. It is returning the cont_cols first, then cat_cols and then label.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您在这里感到困惑,您的数据集确实可以返回 tuple ,但您必须以不同的方式处理它。
您的数据集定义为:
然后定义数据集和数据加载程序。请注意,您不应在此处提供
数据集[0]
,而是dataset
:然后在循环中访问您的数据加载程序内容:
I think you got confused here, your dataset can indeed return tuples but you have to handle it differently.
Your dataset is defined as:
Then you define your dataset and data loader. Note, you should not provide
dataset[0]
here, but insteaddataset
:Then access your dataloader content in a loop: