len() 与 .size(0) 循环遍历 DataLoader 样本时
len() vs .size(0) when looping through DataLoader samples
我在 github 上看到了这个(来自 here 的片段):
(...)
for epoch in range(round):
for i, data in enumerate(dataloader, 0):
############################
# (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
###########################
# train with real
netD.zero_grad()
real_cpu = data[0].to(device)
batch_size = real_cpu.size(0)
label = torch.full((batch_size,), real_label, device=device)
(...)
将batch_size = real_cpu.size(0)
替换为batch_size = len(data[0])
会产生相同的效果吗? (或者至少用 batch_size = len(real_cpu)
?)我问的原因是 iirc 官方 PyTorch 教程在循环 for (X, y) in dataloader:
等过程中显示训练进度时合并了 len(X)
,所以我想知道如果这两种方法等效于显示 'current' 批次中 'samples' 的数量。
如果处理批量大小是第一个维度的数据,那么您可以将 real_cpu.size(0)
与 len(real_cpu)
或 len(data[0])
互换。
但是,当使用某些模型(如 LSTM)时,您可以在第二个维度上设置批量大小,在这种情况下,您不能使用 len
,而是 real_cpu.size(1)
,例如
我在 github 上看到了这个(来自 here 的片段):
(...)
for epoch in range(round):
for i, data in enumerate(dataloader, 0):
############################
# (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
###########################
# train with real
netD.zero_grad()
real_cpu = data[0].to(device)
batch_size = real_cpu.size(0)
label = torch.full((batch_size,), real_label, device=device)
(...)
将batch_size = real_cpu.size(0)
替换为batch_size = len(data[0])
会产生相同的效果吗? (或者至少用 batch_size = len(real_cpu)
?)我问的原因是 iirc 官方 PyTorch 教程在循环 for (X, y) in dataloader:
等过程中显示训练进度时合并了 len(X)
,所以我想知道如果这两种方法等效于显示 'current' 批次中 'samples' 的数量。
如果处理批量大小是第一个维度的数据,那么您可以将 real_cpu.size(0)
与 len(real_cpu)
或 len(data[0])
互换。
但是,当使用某些模型(如 LSTM)时,您可以在第二个维度上设置批量大小,在这种情况下,您不能使用 len
,而是 real_cpu.size(1)
,例如