1: при попытке выполнить обучающую последовательность pytorch с использованием размеров пакета моя функция потерь появляется с ошибкой, когда вывод nn и пакет передаются через функцию MSEloss.
2: пытались выполнить поиск примерно nn, однако это не co vnet, а скорее автоэнкодер, аналогичные проблемы с переполнением стека результатов не дали.
3: NN:
class Net(nn.Module):
def __init__(self, input_dim=10):
super().__init__()
self.fc1 = nn.Linear(input_dim, int(0.75 * input_dim))
self.fc2 = nn.Linear(int(0.75 * input_dim), int(0.5 * input_dim))
self.fc3 = nn.Linear(int(0.5 * input_dim), int(0.33 * input_dim))
self.fc4 = nn.Linear(int(0.33 * input_dim), int(0.25 * input_dim))
self.fc5 = nn.Linear(int(0.25 * input_dim), int(0.33 * input_dim))
self.fc6 = nn.Linear(int(0.33 * input_dim), int(0.5 * input_dim))
self.fc7 = nn.Linear(int(0.5 * input_dim), int(0.75 * input_dim))
self.fc8 = nn.Linear(int(0.75 * input_dim), input_dim)
def forward(self, x):
x = torch.tanh(self.fc1(x))
x = torch.tanh(self.fc2(x))
x = torch.tanh(self.fc3(x))
x = torch.tanh(self.fc4(x))
x = torch.tanh(self.fc5(x))
x = torch.tanh(self.fc6(x))
x = torch.tanh(self.fc7(x))
x = self.fc8(x)
return torch.softmax(x, dim=1)
метод поезда :
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