I have the same question, but in an even simpler case.  How to get the loss comparing 2 batches of strings…
This works fine…
    def test_cosine_embedding_loss(self):
        seq = Variable(torch.randn(1, 5).double())
        model = nn.Linear(5, 5).double()
        target = Variable(seq.data.clone())
        output = model(seq)
        criterion = torch.nn.CosineEmbeddingLoss()
        mask = Variable(torch.ones(1), requires_grad=False)
        loss = criterion(output, target, mask)
        print(loss)
        loss.backward()

But this fails…
    def test_cosine_embedding_loss(self):
        seq = Variable(torch.randn(1, 1, 5).double())
        model = nn.Linear(5, 5).double()
        target = Variable(seq.data.clone())
        output = model(seq)
        criterion = torch.nn.CosineEmbeddingLoss()
        mask = Variable(torch.ones(1, 1), requires_grad=False)
        loss = criterion(output, target, mask)
        print(loss)
        loss.backward()

Error

RuntimeError: inconsistent tensor size, expected src [1 x 5] and mask [1 x 1] to have the same number of elements, but got 5 and 1 elements respectively at d:\pytorch\pytorch\torch\lib\th\generic/THTensorMath.c:197
Ok… so then. like the error says do…
    def test_cosine_embedding_loss(self):
        seq = Variable(torch.randn(1, 1, 5).double())
        model = nn.Linear(5, 5).double()
        target = Variable(seq.data.clone())
        output = model(seq)
        criterion = torch.nn.CosineEmbeddingLoss()
        mask = Variable(torch.ones(1, 5), requires_grad=False)
        loss = criterion(output, target, mask)
        print(loss)
        loss.backward()

And we get…
RuntimeError: The expanded size of the tensor (1) must match the existing size (5) at non-singleton dimension 1. at d:\pytorch\pytorch\torch\lib\th\generic/THTensor.c:309
There’s not a lot in the documenation that says what the expected dims of the 3rd parameter is supposed to be…  Anyone got this working?