Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add masked MSE loss #245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions loss_function.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
from torch import nn
import torch.nn.functional as F


class Tacotron2Loss(nn.Module):
def __init__(self):
super(Tacotron2Loss, self).__init__()

def forward(self, model_output, targets):
@staticmethod
def masked_l2_loss(out, target, lengths):
num_not_padded = lengths.sum() * out.size(1)
loss = F.mse_loss(out, target, reduction="sum")
loss = loss / num_not_padded
return loss

def forward(self, model_output, targets, output_lengths):
mel_target, gate_target = targets[0], targets[1]
mel_target.requires_grad = False
gate_target.requires_grad = False
gate_target = gate_target.view(-1, 1)

mel_out, mel_out_postnet, gate_out, _ = model_output
gate_out = gate_out.view(-1, 1)
mel_loss = nn.MSELoss()(mel_out, mel_target) + \
nn.MSELoss()(mel_out_postnet, mel_target)
mel_loss = self.masked_l2_loss(mel_out, mel_target, output_lengths) + \
self.masked_l2_loss(mel_out_postnet, mel_target, output_lengths)
gate_loss = nn.BCEWithLogitsLoss()(gate_out, gate_target)
return mel_loss + gate_loss
4 changes: 2 additions & 2 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def validate(model, criterion, valset, iteration, batch_size, n_gpus,
for i, batch in enumerate(val_loader):
x, y = model.parse_batch(batch)
y_pred = model(x)
loss = criterion(y_pred, y)
loss = criterion(y_pred, y, x[-1])
if distributed_run:
reduced_val_loss = reduce_tensor(loss.data, n_gpus).item()
else:
Expand Down Expand Up @@ -214,7 +214,7 @@ def train(output_directory, log_directory, checkpoint_path, warm_start, n_gpus,
x, y = model.parse_batch(batch)
y_pred = model(x)

loss = criterion(y_pred, y)
loss = criterion(y_pred, y, x[-1])
if hparams.distributed_run:
reduced_loss = reduce_tensor(loss.data, n_gpus).item()
else:
Expand Down