我的 ADAM 算法在哪里产生了这个尺寸错误?
Posted
技术标签:
【中文标题】我的 ADAM 算法在哪里产生了这个尺寸错误?【英文标题】:Where is this dimension error getting generated in my ADAM algorithm? 【发布时间】:2019-03-04 11:26:03 【问题描述】:我正在这个 BRATS Unet 中处理 main.py
https://github.com/pykao/Modified-3D-UNet-Pytorch/blob/master/main.py
# create your optimizer
print ("Creating Optimizer")
##optimizer = optim.adam(net.parameteres(), lr=)
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
print ("Created! \n")
trainloader = torch.utils.data.DataLoader(train_idx, batch_size=2, shuffle=True)
testloader = torch.utils.data.DataLoader(test_idx, batch_size=2, shuffle=False)
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
print("inside for")
# get the inputs THIS ERRORS OUT
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = model(inputs)
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
我得到这个输出:
Creating Optimizer
Created!
inside for
Traceback (most recent call last):
File "main.py", line 109, in <module>
outputs = model(inputs)
File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/mnt/c/Users/MAHE/Modified Unet3D Master -TestRun/model.py", line 99, in forward
out = self.conv3d_c1_1(x)
File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 448, in forward
self.padding, self.dilation, self.groups)
RuntimeError: Expected 5-dimensional input for 5-dimensional weight [16, 4, 3, 3, 3], but got 0-dimensional input of size [] instead
我对 PyTorch 不熟悉,所以 trainloader、testloader 可能用错了。当你帮助我时,请假设我知道的不多。谢谢。
新错误:
Traceback (most recent call last):
File "/mnt/c/Users/MAHE/Modified Unet3D Master -TestRun/main.py", line 91, in <module>
for id, info in enumerate(trainloader,0):
File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 615, in __next__
batch = self.collate_fn([self.dataset[i] for i in indices])
File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 615, in <listcomp>
batch = self.collate_fn([self.dataset[i] for i in indices])
KeyError: 0
【问题讨论】:
在outputs = model(inputs)
之前检查inputs
张量的size()
,由于某种原因,您的网络似乎得到了一个空的[]
输入而不是5-D 张量。
【参考方案1】:
您应该将数据集传递给数据加载器 API。因此,将 train_data 和 test_data 而不是 train_idx 和 test_idx 传递给 torch.utils.data.DataLoader。
【讨论】:
Traceback (most recent call last): File "/mnt/c/Users/MAHE/Modified Unet3D Master -TestRun/main.py", line 91, in <module> for id, info in enumerate(trainloader,0): File "/home/MAHEUNIX/anaconda3/lib/python3.6/site- packages/torch/utils/data/dataloader.py", line 615, in __next__ batch = self.collate_fn([self.dataset[i] for i in indices]) File "/home/MAHEUNIX/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 615, in <listcomp> batch = self.collate_fn([self.dataset[i] for i in indices]) KeyError: 0
我照你说的做了,得到了这个以上是关于我的 ADAM 算法在哪里产生了这个尺寸错误?的主要内容,如果未能解决你的问题,请参考以下文章
Sklearn:异质特征的FeatureUnion与管道中的分类器产生不兼容的行尺寸错误