Skip to content

Commit

Permalink
Fixed #60 and #61. Placing dropout in the right position and applying…
Browse files Browse the repository at this point in the history
… FullEmbeddingDropout only in training
  • Loading branch information
jrzaurin committed Nov 23, 2021
1 parent 4de81ea commit 3532a98
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ __pycache__*
Untitled*.ipynb

# data related dirs
# data/
tmp_data/
model_weights/
tmp_dir/
weights/
Expand Down
14 changes: 8 additions & 6 deletions pytorch_widedeep/models/transformers/_embeddings_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ def __init__(self, dropout: float):
self.dropout = dropout

def forward(self, X: Tensor) -> Tensor:
mask = X.new().resize_((X.size(1), 1)).bernoulli_(1 - self.dropout).expand_as(
X
) / (1 - self.dropout)
return mask * X
if self.training:
mask = X.new().resize_((X.size(1), 1)).bernoulli_(1 - self.dropout).expand_as(
X
) / (1 - self.dropout)
return mask * X
else:
return X


DropoutLayers = Union[nn.Dropout, FullEmbeddingDropout]
Expand Down Expand Up @@ -167,12 +170,11 @@ def forward(self, X: Tensor) -> Tensor:
x = torch.cat(cat_embed, 1)
else:
x = self.embed(X[:, self.cat_idx].long())
x = self.dropout(x)

if self.bias is not None:
x = x + self.bias.unsqueeze(0)

return x
return self.dropout(x)


class CatAndContEmbeddings(nn.Module):
Expand Down

0 comments on commit 3532a98

Please sign in to comment.