-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22680d4
commit 56d61f3
Showing
4 changed files
with
139 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
from torch.autograd import Variable | ||
|
||
from functools import reduce | ||
from operator import __add__ | ||
|
||
class ConvGRUCell(nn.Module): | ||
|
||
def __init__(self, | ||
input_channel, | ||
output_channel, | ||
kernel, | ||
activation=nn.Tanh(), | ||
normalize=True): | ||
super(ConvGRUCell, self).__init__() | ||
self._input_channel = input_channel | ||
self._output_channel = output_channel | ||
self._kernel = kernel | ||
self._activation = activation | ||
self._normalize = normalize | ||
self._feature_axis = 1 | ||
|
||
# Internal parameters used to reproduce Tensorflow "Same" padding. | ||
# For some reasons, padding dimensions are reversed wrt kernel sizes, | ||
# first comes width then height in the 2D case. | ||
#conv_padding = reduce(__add__, | ||
#[(k // 2 + (k - 2 * (k // 2)) - 1, k // 2) for k in self._kernel[::-1]]) | ||
#pad = nn.ZeroPad2d(conv_padding) | ||
self.gate_conv = nn.Conv2d(self.input_channel, self.input_channel, self._kernel,padding='same') | ||
self.conv2d = nn.Conv2d(self.input_channel, self.output_channel, self._kernel,padding='same') | ||
|
||
self.reset_gate_norm = nn.InstanceNorm2d(input_channel_number,affine=True) | ||
self.update_gate_norm = nn.InstanceNorm2d(input_channel_number,affine=True) | ||
|
||
self.output_norm = nn.GroupNorm(1, self._input_channel, 1e-5, True) | ||
|
||
|
||
def forward(self,x,h): | ||
# x shape = (B,D,H,W) | ||
inputs = Variable(torch.cat((x,h),self._feature_axis)) | ||
gate_conv = self.gate_conv(inputs) | ||
reset_gate, update_gate = torch.split(gate_conv, gate_conv.shape[1] // 2, self._feature_axis) | ||
|
||
reset_gate = self.reset_gate_norm(reset_gate) | ||
update_gate = self.reset_gate_norm(update_gate) | ||
|
||
reset_gate = torch.sigmoid(reset_gate) | ||
reset_gate = torch.sigmoid(update_gate) | ||
|
||
inputs = Variable(torch.cat((x,reset_gate * h),self._feature_axis)) | ||
|
||
conv = self.conv2d(inputs) | ||
conv = self.output_norm(conv) | ||
|
||
y = self._activation(conv) | ||
|
||
output = update_gate * h + (1-update_gate) * y | ||
|
||
return Variable(output),Variable(output) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters