Skip to content

Commit 4d66bd6

Browse files
author
Yunfan Shao
authored
Merge pull request #81 from choosewhatulike/fixMLP
update MLP
2 parents 1e1fbc0 + 8f60a4f commit 4d66bd6

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

docs/source/user/quickstart.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pre-processing data, constructing model and training model.
3636
self.enc = encoder.Conv(
3737
in_channels=300, out_channels=100, kernel_size=3)
3838
self.agg = aggregation.MaxPool()
39-
self.dec = decoder.MLP(100, num_classes=num_classes)
39+
self.dec = decoder.MLP([100, num_classes])
4040
4141
def forward(self, x):
4242
x = self.emb(x) # [N,L] -> [N,L,C]

fastNLP/modules/decoder/MLP.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
import torch.nn as nn
33
from fastNLP.modules.utils import initial_parameter
44
class MLP(nn.Module):
5-
def __init__(self, size_layer, num_class=2, activation='relu' , initial_method = None):
5+
def __init__(self, size_layer, activation='relu' , initial_method = None):
66
"""Multilayer Perceptrons as a decoder
77
8-
Args:
9-
size_layer: list of int, define the size of MLP layers
10-
num_class: int, num of class in output, should be 2 or the last layer's size
11-
activation: str or function, the activation function for hidden layers
8+
:param size_layer: list of int, define the size of MLP layers
9+
:param activation: str or function, the activation function for hidden layers
10+
11+
.. note::
12+
There is no activation function applying on output layer.
13+
1214
"""
1315
super(MLP, self).__init__()
1416
self.hiddens = nn.ModuleList()
@@ -19,13 +21,6 @@ def __init__(self, size_layer, num_class=2, activation='relu' , initial_method =
1921
else:
2022
self.hiddens.append(nn.Linear(size_layer[i-1], size_layer[i]))
2123

22-
if num_class == 2:
23-
self.out_active = nn.LogSigmoid()
24-
elif num_class == size_layer[-1]:
25-
self.out_active = nn.LogSoftmax(dim=1)
26-
else:
27-
raise ValueError("should set output num_class correctly: {}".format(num_class))
28-
2924
actives = {
3025
'relu': nn.ReLU(),
3126
'tanh': nn.Tanh()
@@ -37,17 +32,18 @@ def __init__(self, size_layer, num_class=2, activation='relu' , initial_method =
3732
else:
3833
raise ValueError("should set activation correctly: {}".format(activation))
3934
initial_parameter(self, initial_method )
35+
4036
def forward(self, x):
4137
for layer in self.hiddens:
4238
x = self.hidden_active(layer(x))
43-
x = self.out_active(self.output(x))
39+
x = self.output(x)
4440
return x
4541

4642

4743

4844
if __name__ == '__main__':
4945
net1 = MLP([5,10,5])
50-
net2 = MLP([5,10,5], 5)
46+
net2 = MLP([5,10,5], 'tanh')
5147
for net in [net1, net2]:
5248
x = torch.randn(5, 5)
5349
y = net(x)

reproduction/LSTM+self_attention_sentiment_analysis/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, args=None):
5353
self.embedding = Embedding(len(word2index) ,embeding_size , init_emb= None )
5454
self.lstm = Lstm(input_size = embeding_size,hidden_size = lstm_hidden_size ,bidirectional = True)
5555
self.attention = SelfAttention(lstm_hidden_size * 2 ,dim =attention_unit ,num_vec=attention_hops)
56-
self.mlp = MLP(size_layer=[lstm_hidden_size * 2*attention_hops ,nfc ,class_num ] ,num_class=class_num ,)
56+
self.mlp = MLP(size_layer=[lstm_hidden_size * 2*attention_hops ,nfc ,class_num ])
5757
def forward(self,x):
5858
x_emb = self.embedding(x)
5959
output = self.lstm(x_emb)

0 commit comments

Comments
 (0)