|
@@ -2,13 +2,15 @@ import torch |
|
|
import torch.nn as nn |
|
|
import torch.nn as nn |
|
|
from fastNLP.modules.utils import initial_parameter |
|
|
from fastNLP.modules.utils import initial_parameter |
|
|
class MLP(nn.Module): |
|
|
class MLP(nn.Module): |
|
|
def __init__(self, size_layer, num_class=2, activation='relu' , initial_method = None): |
|
|
|
|
|
|
|
|
def __init__(self, size_layer, activation='relu' , initial_method = None): |
|
|
"""Multilayer Perceptrons as a decoder |
|
|
"""Multilayer Perceptrons as a decoder |
|
|
|
|
|
|
|
|
Args: |
|
|
|
|
|
size_layer: list of int, define the size of MLP layers |
|
|
|
|
|
num_class: int, num of class in output, should be 2 or the last layer's size |
|
|
|
|
|
activation: str or function, the activation function for hidden layers |
|
|
|
|
|
|
|
|
:param size_layer: list of int, define the size of MLP layers |
|
|
|
|
|
:param activation: str or function, the activation function for hidden layers |
|
|
|
|
|
|
|
|
|
|
|
.. note:: |
|
|
|
|
|
There is no activation function applying on output layer. |
|
|
|
|
|
|
|
|
""" |
|
|
""" |
|
|
super(MLP, self).__init__() |
|
|
super(MLP, self).__init__() |
|
|
self.hiddens = nn.ModuleList() |
|
|
self.hiddens = nn.ModuleList() |
|
@@ -19,13 +21,6 @@ class MLP(nn.Module): |
|
|
else: |
|
|
else: |
|
|
self.hiddens.append(nn.Linear(size_layer[i-1], size_layer[i])) |
|
|
self.hiddens.append(nn.Linear(size_layer[i-1], size_layer[i])) |
|
|
|
|
|
|
|
|
if num_class == 2: |
|
|
|
|
|
self.out_active = nn.LogSigmoid() |
|
|
|
|
|
elif num_class == size_layer[-1]: |
|
|
|
|
|
self.out_active = nn.LogSoftmax(dim=1) |
|
|
|
|
|
else: |
|
|
|
|
|
raise ValueError("should set output num_class correctly: {}".format(num_class)) |
|
|
|
|
|
|
|
|
|
|
|
actives = { |
|
|
actives = { |
|
|
'relu': nn.ReLU(), |
|
|
'relu': nn.ReLU(), |
|
|
'tanh': nn.Tanh() |
|
|
'tanh': nn.Tanh() |
|
@@ -37,17 +32,18 @@ class MLP(nn.Module): |
|
|
else: |
|
|
else: |
|
|
raise ValueError("should set activation correctly: {}".format(activation)) |
|
|
raise ValueError("should set activation correctly: {}".format(activation)) |
|
|
initial_parameter(self, initial_method ) |
|
|
initial_parameter(self, initial_method ) |
|
|
|
|
|
|
|
|
def forward(self, x): |
|
|
def forward(self, x): |
|
|
for layer in self.hiddens: |
|
|
for layer in self.hiddens: |
|
|
x = self.hidden_active(layer(x)) |
|
|
x = self.hidden_active(layer(x)) |
|
|
x = self.out_active(self.output(x)) |
|
|
|
|
|
|
|
|
x = self.output(x) |
|
|
return x |
|
|
return x |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
net1 = MLP([5,10,5]) |
|
|
net1 = MLP([5,10,5]) |
|
|
net2 = MLP([5,10,5], 5) |
|
|
|
|
|
|
|
|
net2 = MLP([5,10,5], 'tanh') |
|
|
for net in [net1, net2]: |
|
|
for net in [net1, net2]: |
|
|
x = torch.randn(5, 5) |
|
|
x = torch.randn(5, 5) |
|
|
y = net(x) |
|
|
y = net(x) |
|
|