Numpy 基于损失函数的张量尺寸失配
1: 当尝试使用批大小执行pytorch训练序列时,当nn输出和批通过MSEloss函数时,my loss函数出现错误 2: 已经尝试搜索nn填充,但这不是covnet而是自动编码器,类似的堆栈溢出问题尚未产生结果 3: NN: 列车运行方式:Numpy 基于损失函数的张量尺寸失配,numpy,machine-learning,pytorch,federated-learning,pysyft,Numpy,Machine Learning,Pytorch,Federated Learning,Pysyft,1: 当尝试使用批大小执行pytorch训练序列时,当nn输出和批通过MSEloss函数时,my loss函数出现错误 2: 已经尝试搜索nn填充,但这不是covnet而是自动编码器,类似的堆栈溢出问题尚未产生结果 3: NN: 列车运行方式: def train(net, x_train, x_opt, BATCH_SIZE, EPOCHS, input_dim): outputs = 0 mse = 0 optimizer = optim.SGD(net.parame
def train(net, x_train, x_opt, BATCH_SIZE, EPOCHS, input_dim):
outputs = 0
mse = 0
optimizer = optim.SGD(net.parameters(), lr=0.001)
loss_function = nn.MSELoss()
for epoch in range(EPOCHS):
for i in tqdm(range(0, len(x_train), BATCH_SIZE)):
batch_x = x_train[i:i + BATCH_SIZE]
# print("bx", batch_x.size())
batch_y = x_opt[i:i + BATCH_SIZE]
# print("by", batch_y.size())
net.zero_grad()
# batch_x.view(batch_y.shape[0])
outputs = net(batch_x)
# print('out', outputs)
loss = loss_function(outputs, batch_y)
loss.backward()
optimizer.step() # Does the update
print(f"Epoch: {epoch}. Loss: {loss}")
错误:
99%|█████████▉| 1452/1466 [00:02<00:00, 718.09it/s]B:\tools and software\Anaconda\envs\pysyft-pytorch\lib\site-packages\torch\nn\modules\loss.py:431: UserWarning: Using a target size (torch.Size([39, 10])) that is different to the input size (torch.Size([38, 10])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
return F.mse_loss(input, target, reduction=self.reduction)
100%|█████████▉| 1465/1466 [00:02<00:00, 718.36it/s]
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "B:\tools and software\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "B:\tools and software\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "B:/projects/openProjects/githubprojects/BotnetTrafficAnalysisFederaedLearning/anomaly-detection/pytorch_conversion.py", line 154, in <module>
input_dim=input_dim)
File "B:/projects/openProjects/githubprojects/BotnetTrafficAnalysisFederaedLearning/anomaly-detection/pytorch_conversion.py", line 64, in train
loss = loss_function(outputs, batch_y)
File "B:\tools and software\Anaconda\envs\pysyft-pytorch\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
result = self.forward(*input, **kwargs)
File "B:\tools and software\Anaconda\envs\pysyft-pytorch\lib\site-packages\torch\nn\modules\loss.py", line 431, in forward
return F.mse_loss(input, target, reduction=self.reduction)
File "B:\tools and software\Anaconda\envs\pysyft-pytorch\lib\site-packages\torch\nn\functional.py", line 2215, in mse_loss
expanded_input, expanded_target = torch.broadcast_tensors(input, target)
File "B:\tools and software\Anaconda\envs\pysyft-pytorch\lib\site-packages\torch\functional.py", line 52, in broadcast_tensors
return torch._C._VariableFunctions.broadcast_tensors(tensors)
RuntimeError: The size of tensor a (38) must match the size of tensor b (39) at non-singleton dimension 0
99%|█████████▉| 1452/1466 [00:02错误似乎表明目标和输出的批量大小不相同。您是否尝试打印目标和输出的大小。如果是,结果如何。此外,您可能希望打印模型输入的大小,以查看是否有错误。很抱歉,将此发布到回答中,我无法发表评论。在原始代码中,所做的是让TensorFlow模型返回一个numpy预测数组,然后从验证集中减去预测集。这将用于产生均方误差。然而,输出形状远小于验证集。我认为这是因为x_选项列表或其他更长的东西比x_-train大,因此无法填充相同数量的批次。您可以检查以确保您的x_-opts大小与该或您的x_-train匹配。不,没有相同的大小,但我现在可以继续。谢谢。
99%|█████████▉| 1452/1466 [00:02<00:00, 718.09it/s]B:\tools and software\Anaconda\envs\pysyft-pytorch\lib\site-packages\torch\nn\modules\loss.py:431: UserWarning: Using a target size (torch.Size([39, 10])) that is different to the input size (torch.Size([38, 10])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
return F.mse_loss(input, target, reduction=self.reduction)
100%|█████████▉| 1465/1466 [00:02<00:00, 718.36it/s]
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "B:\tools and software\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "B:\tools and software\PyCharm 2020.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "B:/projects/openProjects/githubprojects/BotnetTrafficAnalysisFederaedLearning/anomaly-detection/pytorch_conversion.py", line 154, in <module>
input_dim=input_dim)
File "B:/projects/openProjects/githubprojects/BotnetTrafficAnalysisFederaedLearning/anomaly-detection/pytorch_conversion.py", line 64, in train
loss = loss_function(outputs, batch_y)
File "B:\tools and software\Anaconda\envs\pysyft-pytorch\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
result = self.forward(*input, **kwargs)
File "B:\tools and software\Anaconda\envs\pysyft-pytorch\lib\site-packages\torch\nn\modules\loss.py", line 431, in forward
return F.mse_loss(input, target, reduction=self.reduction)
File "B:\tools and software\Anaconda\envs\pysyft-pytorch\lib\site-packages\torch\nn\functional.py", line 2215, in mse_loss
expanded_input, expanded_target = torch.broadcast_tensors(input, target)
File "B:\tools and software\Anaconda\envs\pysyft-pytorch\lib\site-packages\torch\functional.py", line 52, in broadcast_tensors
return torch._C._VariableFunctions.broadcast_tensors(tensors)
RuntimeError: The size of tensor a (38) must match the size of tensor b (39) at non-singleton dimension 0