From b297f93537600760ad6026808fe778cf2667fac6 Mon Sep 17 00:00:00 2001 From: Ke Zhen Date: Mon, 2 Jul 2018 12:03:45 +0800 Subject: [PATCH] add conv and pooling module --- fastNLP/modules/convolution/AvgPool1d.py | 22 +++++++++++++++++++ fastNLP/modules/convolution/Conv1d.py | 28 ++++++++++++++++++++++++ fastNLP/modules/convolution/MaxPool1d.py | 23 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 fastNLP/modules/convolution/AvgPool1d.py create mode 100644 fastNLP/modules/convolution/Conv1d.py create mode 100644 fastNLP/modules/convolution/MaxPool1d.py diff --git a/fastNLP/modules/convolution/AvgPool1d.py b/fastNLP/modules/convolution/AvgPool1d.py new file mode 100644 index 00000000..c427fc9a --- /dev/null +++ b/fastNLP/modules/convolution/AvgPool1d.py @@ -0,0 +1,22 @@ +# python: 3.6 +# encoding: utf-8 + +import torch.nn as nn +# import torch.nn.functional as F + + +class AvgPool1d(nn.Module): + """1-d average pooling module.""" + + def __init__(self, kernel_size, stride=None, padding=0, + ceil_mode=False, count_include_pad=True): + super(AvgPool1d, self).__init__() + self.pool = nn.AvgPool1d( + kernel_size=kernel_size, + stride=stride, + padding=padding, + ceil_mode=ceil_mode, + count_include_pad=count_include_pad) + + def forward(self, x): + return self.pool(x) diff --git a/fastNLP/modules/convolution/Conv1d.py b/fastNLP/modules/convolution/Conv1d.py new file mode 100644 index 00000000..60554a24 --- /dev/null +++ b/fastNLP/modules/convolution/Conv1d.py @@ -0,0 +1,28 @@ +# python: 3.6 +# encoding: utf-8 + +import torch.nn as nn +# import torch.nn.functional as F + + +class Conv1d(nn.Module): + """ + Basic 1-d convolution module. + """ + + def __init__(self, in_channels, out_channels, kernel_size, + stride=1, padding=0, dilation=1, + groups=1, bias=True): + super(Conv1d, self).__init__() + self.conv = nn.Conv1d( + in_channels=in_channels, + out_channels=out_channels, + kernel_size=kernel_size, + stride=stride, + padding=padding, + dilation=dilation, + groups=groups, + bias=bias) + + def forward(self, x): + return self.conv(x) diff --git a/fastNLP/modules/convolution/MaxPool1d.py b/fastNLP/modules/convolution/MaxPool1d.py new file mode 100644 index 00000000..d1f39395 --- /dev/null +++ b/fastNLP/modules/convolution/MaxPool1d.py @@ -0,0 +1,23 @@ +# python: 3.6 +# encoding: utf-8 + +import torch.nn as nn +# import torch.nn.functional as F + + +class MaxPool1d(nn.Module): + """1-d max-pooling module.""" + + def __init__(self, kernel_size, stride=None, padding=0, + dilation=1, return_indices=False, ceil_mode=False): + super(MaxPool1d, self).__init__() + self.maxpool = nn.MaxPool1d( + kernel_size=kernel_size, + stride=stride, + padding=padding, + dilation=dilation, + return_indices=return_indices, + ceil_mode=ceil_mode) + + def forward(self, x): + return self.maxpool(x)