Skip to content

Instantly share code, notes, and snippets.

@anna-hope
Last active April 18, 2018 14:04
Show Gist options
  • Save anna-hope/3ced6d415d0070b9564b0634a8a6d5da to your computer and use it in GitHub Desktop.
Save anna-hope/3ced6d415d0070b9564b0634a8a6d5da to your computer and use it in GitHub Desktop.
VDCNN Convolutional Block (Conneau et al. 2017)
# Convolutional block from http://aclweb.org/anthology/E17-1104
# Anton Melnikov
import numpy as np
import torch
from torch import nn
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, *,
kernel_size, stride=1):
super().__init__()
# "same" padding (preserve original temporal dimension length)
padding = int(np.floor((kernel_size - 1) / 2))
self.conv_1 = nn.Conv1d(in_channels, out_channels, kernel_size,
stride=stride, padding=padding)
self.batch_norm_1 = nn.BatchNorm1d(out_channels)
self.relu_1 = nn.ReLU()
self.conv_2 = nn.Conv1d(out_channels, out_channels,
kernel_size, stride=stride, padding=padding)
self.batch_norm_2 = nn.BatchNorm1d(out_channels)
self.relu_2 = nn.ReLU()
self.init_conv()
def init_conv(self):
nn.init.kaiming_uniform(self.conv_1.weight.data)
nn.init.kaiming_uniform(self.conv_2.weight.data)
def forward(self, X):
X = self.conv_1(X)
X = self.batch_norm_1(X)
X = self.relu_1(X)
X = self.conv_2(X)
X = self.batch_norm_2(X)
X = self.relu_2(X)
return X
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment