class Autoencoder2(nn.Module):
def __init__(self):
super(Autoencoder2, self).__init__()
self.encoder = nn.Sequential(nn.Conv1d(1, 16, kernel_size=51, stride=10, bias=False, padding=25),
nn.PReLU(),
nn.BatchNorm1d(16),
nn.Conv1d(16, 32, kernel_size=11, stride=3, bias=False, padding=5),
nn.PReLU(),
)
self.decoder = nn.Sequential(
nn.ConvTranspose1d(32, 16, 11, stride=3, bias=False, padding=5),
nn.PReLU(),
nn.ConvTranspose1d(16, 1, 51, stride=10, bias=False, padding=25),
nn.PReLU())
self.lstm = nn.LSTM(100, 32, 1, batch_first=True, bidirectional=False)
self.fc = nn.Sequential(nn.Linear(1024, 5))
def forward(self, x):
x_enc = self.encoder(x)
x_dec = self.decoder(x_enc)
x_lstm, (h_final, c_final) = self.lstm(x_enc)
h_final = h_final.permute(1, 2, 0)
x_lstm = x_lstm.contiguous().view(x_lstm.shape[0], -1)
x_lstm = self.fc(x_lstm)
return x_dec, x_lstm
def _train_epoch(self, epoch):
"""
Training logic for an epoch
:param epoch: Integer, current training epoch.
:return: A log that contains average loss and metric in this epoch.
"""
self.model.train()
self.train_metrics.reset()
overall_outs = []
overall_trgs = []
for batch_idx, (data, target) in enumerate(self.data_loader):
data, target = data.to(self.device), target.to(self.device)
# adding noise:
noise = np.random.normal(0, 10, data.shape)
noise = torch.from_numpy(noise).type(torch.cuda.FloatTensor).to(self.device)
noisy_data = data + noise
self.optimizer.zero_grad()
decoder_output, classifier_output = self.model(noisy_data)
criterion_recon = self.criterion[0]
criterion_class = self.criterion[1]
loss1 = criterion_recon(decoder_output, data)
loss2 = criterion_class(classifier_output, target)
loss = loss1 * self.config["recon_loss_penalty"] + loss2
loss.backward()
self.optimizer.step()
The error is in the loss calcultion
File "/tmp/pycharm_project_53/pytorch_template_AE_test/trainer/trainer.py", line 55, in _train_epoch
loss1 = criterion_recon(decoder_output, data)
ile "/tmp/pycharm_project_53/pytorch_template_AE_test/model/loss.py", line 14, in MSELoss
return loss(output, target)
File "/home/emad/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
result = self.forward(*input, **kwargs)
File "/home/emad/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 431, in forward
return F.mse_loss(input, target, reduction=self.reduction)
File "/home/emad/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 2215, in mse_loss
expanded_input, expanded_target = torch.broadcast_tensors(input, target)
File "/home/emad/.local/lib/python3.6/site-packages/torch/functional.py", line 52, in broadcast_tensors
return torch._C._VariableFunctions.broadcast_tensors(tensors)
RuntimeError: The size of tensor a (2971) must match the size of tensor b (3000) at non-singleton dimension 2