|
| 1 | +import torch.nn as nn |
| 2 | +import torch.nn.functional as F |
| 3 | +import torch.nn.init as init |
| 4 | +from torch.autograd import Variable |
| 5 | +import numpy as np |
| 6 | + |
| 7 | + |
| 8 | +# network |
| 9 | +class RELUNet(nn.Module): |
| 10 | + def __init__(self, n_inner_layers, input_dim, hidden_dim, output_dim, dropout=0, batchnorm=True): |
| 11 | + |
| 12 | + super(RELUNet, self).__init__() |
| 13 | + |
| 14 | + self.input_dim = input_dim |
| 15 | + self.output_dim = output_dim |
| 16 | + self.hidden_dim = hidden_dim |
| 17 | + self.dropout = dropout |
| 18 | + self.batchnorm = batchnorm |
| 19 | + self.n_inner_layers = n_inner_layers |
| 20 | + |
| 21 | + # FC layers |
| 22 | + self.fc_in = nn.Linear(input_dim, hidden_dim) |
| 23 | + # Hacky way to set inner layers. Ensures they are all converted to |
| 24 | + for k in range(n_inner_layers): |
| 25 | + setattr(self, "fc_%s" % k, nn.Linear(hidden_dim, hidden_dim)) |
| 26 | + self.fc_out = nn.Linear(hidden_dim, output_dim) |
| 27 | + |
| 28 | + # BN layers |
| 29 | + self.bn_in = nn.BatchNorm1d(hidden_dim) |
| 30 | + |
| 31 | + for k in range(n_inner_layers): |
| 32 | + setattr(self, "bn_%s" % k, nn.BatchNorm1d(hidden_dim)) |
| 33 | + |
| 34 | + # Initialize weights specifically for relu |
| 35 | + |
| 36 | + # First layer |
| 37 | + init.normal(self.fc_in.weight, std=2. / np.sqrt(np.float32(self.input_dim))) |
| 38 | + init.constant(self.fc_in.bias, 0.) |
| 39 | + |
| 40 | + # Inner layers |
| 41 | + for i in range(self.n_inner_layers): |
| 42 | + init.normal(getattr(self, "fc_%s" % i).weight, std=2. / np.sqrt(np.float32(self.hidden_dim))) |
| 43 | + init.constant(getattr(self, "fc_%s" % i).bias, 0.) |
| 44 | + |
| 45 | + # Last layer |
| 46 | + init.normal(self.fc_out.weight, std=2. / np.sqrt(np.float32(self.hidden_dim))) |
| 47 | + init.constant(self.fc_out.bias, 0.) |
| 48 | + |
| 49 | + def forward(self, x, training=False): |
| 50 | + |
| 51 | + # First layer |
| 52 | + x = self.fc_in(x) |
| 53 | + if self.batchnorm: |
| 54 | + x = self.bn_in(x) |
| 55 | + x = F.relu(x) |
| 56 | + if self.dropout > 0: |
| 57 | + x = F.dropout(x, p=self.dropout, training=training) |
| 58 | + |
| 59 | + # Inner layers |
| 60 | + for i in range(self.n_inner_layers): |
| 61 | + x = getattr(self, "fc_%s" % i)(x) |
| 62 | + if self.batchnorm: |
| 63 | + x = getattr(self, "bn_%s" % i)(x) |
| 64 | + x = F.relu(x) |
| 65 | + if self.dropout > 0: |
| 66 | + x = F.dropout(x, p=self.dropout, training=training) |
| 67 | + |
| 68 | + # Output layers |
| 69 | + x = self.fc_out(x) |
| 70 | + |
| 71 | + return x |
| 72 | + |
| 73 | + |
| 74 | +def alpha_dropout(input, p=0.5, training=False): |
| 75 | + """Applies alpha dropout to the input. |
| 76 | +
|
| 77 | + See :class:`~torch.nn.AlphaDropout` for details. |
| 78 | +
|
| 79 | + Args: |
| 80 | + p (float, optional): the drop probability |
| 81 | + training (bool, optional): switch between training and evaluation mode |
| 82 | + """ |
| 83 | + if p < 0 or p > 1: |
| 84 | + raise ValueError("dropout probability has to be between 0 and 1, " |
| 85 | + "but got {}".format(p)) |
| 86 | + |
| 87 | + if p == 0 or not training: |
| 88 | + return input |
| 89 | + |
| 90 | + alpha = -1.7580993408473766 |
| 91 | + keep_prob = 1 - p |
| 92 | + # TODO avoid casting to byte after resize |
| 93 | + noise = input.data.new().resize_(input.size()) |
| 94 | + noise.bernoulli_(p) |
| 95 | + noise = Variable(noise.byte()) |
| 96 | + |
| 97 | + output = input.masked_fill(noise, alpha) |
| 98 | + |
| 99 | + a = (keep_prob + alpha ** 2 * keep_prob * (1 - keep_prob)) ** (-0.5) |
| 100 | + b = -a * alpha * (1 - keep_prob) |
| 101 | + |
| 102 | + return output.mul_(a).add_(b) |
| 103 | + |
| 104 | + |
| 105 | +def selu(x): |
| 106 | + alpha = 1.6732632423543772848170429916717 |
| 107 | + scale = 1.0507009873554804934193349852946 |
| 108 | + return scale * F.elu(x, alpha) |
| 109 | + |
| 110 | + |
| 111 | +class SELUNet(nn.Module): |
| 112 | + def __init__(self, n_inner_layers, input_dim, hidden_dim, output_dim, dropout=0.05): |
| 113 | + |
| 114 | + super(SELUNet, self).__init__() |
| 115 | + |
| 116 | + self.dropout = dropout |
| 117 | + |
| 118 | + self.input_dim = input_dim |
| 119 | + self.output_dim = output_dim |
| 120 | + self.hidden_dim = hidden_dim |
| 121 | + self.n_inner_layers = n_inner_layers |
| 122 | + self.fc_in = nn.Linear(input_dim, hidden_dim) |
| 123 | + for k in range(n_inner_layers): |
| 124 | + setattr(self, "fc_%s" % k, nn.Linear(hidden_dim, hidden_dim)) |
| 125 | + self.fc_out = nn.Linear(hidden_dim, output_dim) |
| 126 | + |
| 127 | + # Initialize weights specifically for selu |
| 128 | + |
| 129 | + # First layer |
| 130 | + init.normal(self.fc_in.weight, std=1. / np.sqrt(np.float32(self.input_dim))) |
| 131 | + init.constant(self.fc_in.bias, 0.) |
| 132 | + |
| 133 | + # Inner layers |
| 134 | + for i in range(self.n_inner_layers): |
| 135 | + init.normal(getattr(self, "fc_%s" % i).weight, std=1. / np.sqrt(np.float32(self.hidden_dim))) |
| 136 | + init.constant(getattr(self, "fc_%s" % i).bias, 0.) |
| 137 | + |
| 138 | + # Last layer |
| 139 | + init.normal(self.fc_out.weight, std=1. / np.sqrt(np.float32(self.hidden_dim))) |
| 140 | + init.constant(self.fc_out.bias, 0.) |
| 141 | + |
| 142 | + def forward(self, x, training=False): |
| 143 | + |
| 144 | + # First layer |
| 145 | + x = self.fc_in(x) |
| 146 | + x = selu(x) |
| 147 | + if self.dropout > 0: |
| 148 | + x = alpha_dropout(x, p=self.dropout, training=training) |
| 149 | + |
| 150 | + # Inner layers |
| 151 | + for i in range(self.n_inner_layers): |
| 152 | + x = getattr(self, "fc_%s" % i)(x) |
| 153 | + x = selu(x) |
| 154 | + if self.dropout > 0: |
| 155 | + x = alpha_dropout(x, p=self.dropout, training=training) |
| 156 | + |
| 157 | + # Output layers |
| 158 | + x = self.fc_out(x) |
| 159 | + |
| 160 | + return x |
0 commit comments