我有一个形状为torch.Size([3, 1, 20, 20])
(batch_size x channels x height x width)
的输入张量X
和一个形状相同的目标张量y
torch.Size([3, 1, 20, 20])
现在当我使用均方误差损失时,它给了我一个警告:
Using a target size (torch.Size([3, 1, 20, 20])) that is different to the input size (torch.Size([3, 1]))
但输入形状为torch.Size([3, 1, 20, 20])
?
然后我得到错误:
RuntimeError: The size of tensor a (3) must match the size of tensor b (20) at non-singleton dimension 2
代码:
def __init__(self):
super().__init__()
self.l1 = torch.nn.Linear(20 * 20 , 1)
def forward(self, x):
return torch.relu(self.l1(x.view(x.size(0), -1)))
def training_step(self, batch, batch_nb):
X, y = batch
loss = F.mse_loss(self(X), y, reduction='mean')
return loss
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=0.02)
有人能帮帮我吗
你的输入大小为[3,1,20,20],但模型(self
)的输出大小为[3,1],因为你通过一个完全连接的层传递输入,然后重塑(view
)它。
我想提供如何解决这个问题的代码,但不清楚你的实际目标是什么。