forked from Stonesjtu/Pytorch-NCE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric_model.py
More file actions
24 lines (17 loc) · 741 Bytes
/
Copy pathgeneric_model.py
File metadata and controls
24 lines (17 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""Main container for common language model"""
import torch
import torch.nn as nn
from utils import get_mask
class GenModel(nn.Module):
"""Container module with an encoder, a recurrent module, and a criterion (decoder and loss function)."""
def __init__(self, criterion, dropout=0.2):
super(GenModel, self).__init__()
self.criterion = criterion
def forward(self, input, target, length):
mask = get_mask(length, cut_tail=0)
# <s> is non-sense in this model, thus the loss should be
# masked manually
effective_target = target[:, 1:].contiguous()
loss = self.criterion(effective_target, input)
loss = torch.masked_select(loss, mask)
return loss.mean()