Skip to content

Instantly share code, notes, and snippets.

@MaximumEntropy
Created October 24, 2017 23:27
Show Gist options
  • Save MaximumEntropy/76a236035abeb1ad07089d0e3224ac42 to your computer and use it in GitHub Desktop.
Save MaximumEntropy/76a236035abeb1ad07089d0e3224ac42 to your computer and use it in GitHub Desktop.
class DilatedConvSentenceEncoder(nn.Module):
"""A Sentence Encoder with Dilated Convs."""
def __init__(
self, input_dim=512, hidden_dim=4096, n_layers=7,
dropout=0.5, batch_first=True
):
"""Initialize params."""
super(DilatedConvSentenceEncoder, self).__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.dropout = dropout
# 512 x 128
self.conv1 = nn.Conv1d(
in_channels=input_dim, out_channels=hidden_dim // 4,
kernel_size=4, stride=2, padding=12, dilation=8, bias=False
)
# self.bn1 = nn.BatchNorm1d(num_features=input_dim * 2)
# 1024 x 64
self.conv2 = nn.Conv1d(
in_channels=hidden_dim // 4, out_channels=hidden_dim // 2,
kernel_size=4, stride=2, padding=9, dilation=6, bias=False
)
# self.bn2 = nn.BatchNorm1d(num_features=input_dim * 4)
# 2048 x 32
self.conv3 = nn.Conv1d(
in_channels=hidden_dim // 2, out_channels=hidden_dim,
kernel_size=4, stride=2, padding=6, dilation=4, bias=False
)
# self.bn3 = nn.BatchNorm1d(num_features=input_dim * 8)
# 4096 x 16
self.conv4 = nn.Conv1d(
in_channels=hidden_dim, out_channels=hidden_dim,
kernel_size=4, stride=2, padding=3, dilation=2, bias=False
)
# self.bn4 = nn.BatchNorm1d(num_features=input_dim * 8)
# 4096 x 8
self.conv5 = nn.Conv1d(
in_channels=hidden_dim, out_channels=hidden_dim,
kernel_size=4, stride=2, padding=3, dilation=2, bias=False
)
# self.bn5 = nn.BatchNorm1d(num_features=input_dim * 8)
# 4096 x 4
self.conv6 = nn.Conv1d(
in_channels=hidden_dim, out_channels=hidden_dim,
kernel_size=4, stride=2, padding=3, dilation=2, bias=False
)
# self.bn6 = nn.BatchNorm1d(num_features=input_dim * 8)
# 4096 x 2
self.conv7 = nn.Conv1d(
in_channels=hidden_dim, out_channels=hidden_dim,
kernel_size=4, stride=2, padding=3, dilation=2, bias=False
)
# self.bn7 = nn.BatchNorm1d(num_features=input_dim * 8)
def forward(self, input):
x = input.transpose(1, 2)
x = F.relu(F.dropout(self.conv1(x), p=self.dropout, training=self.training))
x = F.relu(F.dropout(self.conv2(x), p=self.dropout, training=self.training))
x = F.relu(F.dropout(self.conv3(x), p=self.dropout, training=self.training))
x = F.relu(F.dropout(self.conv4(x), p=self.dropout, training=self.training))
x = F.relu(F.dropout(self.conv5(x), p=self.dropout, training=self.training))
x = F.relu(F.dropout(self.conv6(x), p=self.dropout, training=self.training))
return F.relu(F.dropout(self.conv7(x), p=self.dropout, training=self.training)).squeeze()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment