Skip to content

Instantly share code, notes, and snippets.

@alonstern
Last active April 13, 2020 12:17
Show Gist options
  • Save alonstern/81eb34558bc59b4594b18c96fd437722 to your computer and use it in GitHub Desktop.
Save alonstern/81eb34558bc59b4594b18c96fd437722 to your computer and use it in GitHub Desktop.
convolution layer
class CNNModel(nn.Module):
def __init__(self, embedding_dim, kernel_size, hidden_dim, vocab_size):
super().__init__()
self._kernel_size = kernel_size
self._hidden_dim = hidden_dim
self._word_embeddings = nn.Embedding(vocab_size, embedding_dim)
self._conv = nn.Conv2d(1, hidden_dim, kernel_size=(kernel_size, embedding_dim))
def forward(self, sample):
embeds = self._word_embeddings(sample)
# Converts the vector to a shape Conv2d can work with
conv_in = embeds.view(1, 1, len(sample), -1)
conv_out = self._conv(conv_in)
conv_out = F.relu(conv_out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment