Skip to content

Commit 1b80565

Browse files
boshiningljshou
authored andcommitted
Dev/shining (#75)
* add text matching compression & delete regression * modify the Tutorial.md * modify the Tutorial.md * add teacher model name * modify tutorial.md * fix transform params2tensors problem * add softmax output layer for slot tagging * add slot_tagging metrics * modify make word emb matrix * Delete dev.tsv * Delete test.tsv * Delete train.tsv * delate conll data * Update Contributing.md * Update tools * Update README.md * Update Contributing.md * Update README.md * Update autotest.sh * update get_results.py * fix sequence tagging workflow * add model type judgement for optimizer * delete full atis dataset and unuseful config filee * add slot_tagging sample data * fix load embedding slow problem * fix Char embedding CNN problem * add lower token when load embedding matrix * add word level length for char emb * Update Conv * Add ARCI & ARCII module and Modify Conv block * Update to the same as master * update Linear layer * Add block - Calculate Distance of Two Vectors * update tutorial_zh_CN
1 parent 5e86455 commit 1b80565

File tree

13 files changed

+758
-9
lines changed

13 files changed

+758
-9
lines changed

Tutorial_zh_CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ Question answer matching is a crucial subtask of the question answering problem,
289289
CNN (NeuronBlocks) | 0.747
290290
BiLSTM (NeuronBlocks) | 0.767
291291
BiLSTM+Attn (NeuronBlocks) | 0.754
292+
[ARC-I](https://arxiv.org/abs/1503.03244) (NeuronBlocks) | 0.7508
293+
[ARC-II](https://arxiv.org/abs/1503.03244) (NeuronBlocks) | 0.7612
292294
[MatchPyramid](https://arxiv.org/abs/1602.06359) (NeuronBlocks) | 0.763
293295
BiLSTM+Match Attention (NeuronBlocks) | 0.786
294296

block_zoo/Conv.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ def inference(self):
5252

5353
self.output_dim = [-1]
5454
if self.input_dims[0][1] != -1:
55-
self.output_dim.append((self.input_dims[0][1] - self.window_size) // self.stride + 1)
55+
if self.padding_type == 'SAME':
56+
self.output_dim.append(self.input_dims[0][1])
57+
else:
58+
self.output_dim.append((self.input_dims[0][1] - self.window_size) // self.stride + 1)
5659
else:
5760
self.output_dim.append(-1)
5861
self.output_dim.append(self.output_channel_num)

block_zoo/Linear.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def default(self):
3232
self.activation = 'PReLU'
3333
self.last_hidden_activation = True
3434
self.last_hidden_softmax = False
35+
self.keep_dim = True # for exmaple if the output shape is [?, len, 1]. you want to squeeze it, set keep_dim=False, the the output shape is [?, len]
3536

3637
@DocInherit
3738
def declare(self):
@@ -42,10 +43,16 @@ def declare(self):
4243
def inference(self):
4344
if isinstance(self.hidden_dim, int):
4445
self.output_dim = copy.deepcopy(self.input_dims[0])
45-
self.output_dim[-1] = self.hidden_dim
46+
if not self.keep_dim and self.hidden_dim == 1:
47+
self.output_dim.pop()
48+
else:
49+
self.output_dim[-1] = self.hidden_dim
4650
elif isinstance(self.hidden_dim, list):
4751
self.output_dim = copy.deepcopy(self.input_dims[0])
48-
self.output_dim[-1] = self.hidden_dim[-1]
52+
if not self.keep_dim and self.hidden_dim[-1] == 1:
53+
self.output_dim.pop()
54+
else:
55+
self.output_dim[-1] = self.hidden_dim[-1]
4956

5057
super(LinearConf, self).inference() # PUT THIS LINE AT THE END OF inference()
5158

@@ -87,6 +94,7 @@ class Linear(BaseLayer):
8794
def __init__(self, layer_conf):
8895

8996
super(Linear, self).__init__(layer_conf)
97+
self.layer_conf = layer_conf
9098

9199
if layer_conf.input_ranks[0] == 3 and layer_conf.batch_norm is True:
92100
layer_conf.batch_norm = False
@@ -139,6 +147,8 @@ def forward(self, string, string_len=None):
139147
masks = masks.to(device)
140148
string = string * masks
141149
string_out = self.linear(string.float())
150+
if not self.layer_conf.keep_dim:
151+
string_out = torch.squeeze(string_out, -1)
142152
return string_out, string_len
143153

144154

block_zoo/Pooling1D.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT license.
3+
4+
import torch
5+
import torch.nn as nn
6+
import torch.nn.functional as F
7+
8+
import numpy as np
9+
10+
from block_zoo.BaseLayer import BaseLayer, BaseConf
11+
from utils.DocInherit import DocInherit
12+
13+
14+
class Pooling1DConf(BaseConf):
15+
"""
16+
17+
Args:
18+
pool_type (str): 'max' or 'mean', default is 'max'.
19+
stride (int): which axis to conduct pooling, default is 1.
20+
padding (int): implicit zero paddings on both sides of the input. Can be a single number or a tuple (padH, padW). Default: 0
21+
window_size (int): the size of the pooling
22+
23+
"""
24+
25+
def __init__(self, **kwargs):
26+
super(Pooling1DConf, self).__init__(**kwargs)
27+
28+
@DocInherit
29+
def default(self):
30+
self.pool_type = 'max' # Supported: ['max', mean']
31+
self.stride = 1
32+
self.padding = 0
33+
self.window_size = 3
34+
35+
@DocInherit
36+
def declare(self):
37+
self.num_of_inputs = 1
38+
self.input_ranks = [3]
39+
40+
41+
@DocInherit
42+
def inference(self):
43+
44+
self.output_dim = [self.input_dims[0][0]]
45+
if self.input_dims[0][1] != -1:
46+
self.output_dim.append(
47+
(self.input_dims[0][1] + 2 * self.padding - self.window_size) // self.stride + 1)
48+
else:
49+
self.output_dim.append(-1)
50+
51+
self.output_dim.append(self.input_dims[0][-1])
52+
# DON'T MODIFY THIS
53+
self.output_rank = len(self.output_dim)
54+
55+
@DocInherit
56+
def verify(self):
57+
super(Pooling1DConf, self).verify()
58+
59+
necessary_attrs_for_user = ['pool_type']
60+
for attr in necessary_attrs_for_user:
61+
self.add_attr_exist_assertion_for_user(attr)
62+
63+
self.add_attr_value_assertion('pool_type', ['max', 'mean'])
64+
65+
assert self.output_dim[
66+
-1] != -1, "The shape of input is %s , and the input channel number of pooling should not be -1." % (
67+
str(self.input_dims[0]))
68+
69+
70+
class Pooling1D(BaseLayer):
71+
""" Pooling layer
72+
73+
Args:
74+
layer_conf (PoolingConf): configuration of a layer
75+
"""
76+
77+
def __init__(self, layer_conf):
78+
super(Pooling1D, self).__init__(layer_conf)
79+
self.pool = None
80+
if layer_conf.pool_type == "max":
81+
self.pool = nn.MaxPool1d(kernel_size=layer_conf.window_size, stride=layer_conf.stride,
82+
padding=layer_conf.padding)
83+
elif layer_conf.pool_type == "mean":
84+
self.pool = nn.AvgPool1d(kernel_size=layer_conf.window_size, stride=layer_conf.stride,
85+
padding=layer_conf.padding)
86+
87+
def forward(self, string, string_len=None):
88+
""" process inputs
89+
90+
Args:
91+
string (Tensor): tensor with shape: [batch_size, length, feature_dim]
92+
string_len (Tensor): [batch_size], default is None.
93+
94+
Returns:
95+
Tensor: Pooling result of string
96+
97+
"""
98+
99+
string = string.permute([0, 2, 1]).contiguous()
100+
string = self.pool(string)
101+
string = string.permute([0, 2, 1]).contiguous()
102+
return string, string_len
103+
104+

block_zoo/Pooling2D.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class Pooling2DConf(BaseConf):
1919
stride (int): which axis to conduct pooling, default is 1.
2020
padding (int): implicit zero paddings on both sides of the input. Can be a single number or a tuple (padH, padW). Default: 0
2121
window_size (int): the size of the pooling
22-
activation (string): activation functions, e.g. ReLU
2322
2423
"""
2524
def __init__(self, **kwargs):

block_zoo/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from .Dropout import Dropout, DropoutConf
1717

1818
from .Conv2D import Conv2D, Conv2DConf
19+
from .Pooling1D import Pooling1D, Pooling1DConf
1920
from .Pooling2D import Pooling2D, Pooling2DConf
2021

2122
from .embedding import CNNCharEmbedding, CNNCharEmbeddingConf
23+
from .embedding import LSTMCharEmbedding, LSTMCharEmbeddingConf
2224

2325
from .CRF import CRFConf, CRF
2426

@@ -51,4 +53,7 @@
5153

5254
from .normalizations import LayerNorm, LayerNormConf
5355

54-
from .HighwayLinear import HighwayLinear, HighwayLinearConf
56+
from .HighwayLinear import HighwayLinear, HighwayLinearConf
57+
58+
from .Gating import Gating, GatingConf
59+
from .HistogramMapping import HistogramMapping, HistogramMappingConf

block_zoo/embedding/LSTMCharEmbedding.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def forward(self, string):
118118
'input_ranks': [3],
119119
'use_gpu': True
120120
}
121-
layer_conf = CNNCharEmbeddingConf(**conf)
121+
layer_conf = LSTMCharEmbeddingConf(**conf)
122122

123123
# make a fake input: [bs, seq_len, char num in words]
124124
# assume in this batch, the padded sentence length is 3 and the each word has 5 chars, including padding 0.
@@ -135,4 +135,3 @@ def forward(self, string):
135135
print(output)
136136

137137

138-

block_zoo/op/CalculateDistance.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT license.
3+
4+
import torch
5+
import torch.nn as nn
6+
import torch.nn.functional as F
7+
import logging
8+
9+
from ..BaseLayer import BaseConf, BaseLayer
10+
from utils.DocInherit import DocInherit
11+
from utils.exceptions import ConfigurationError
12+
import copy
13+
14+
15+
class CalculateDistanceConf(BaseConf):
16+
""" Configuration of CalculateDistance Layer
17+
18+
Args:
19+
operations (list): a subset of ["cos", "euclidean", "manhattan", "chebyshev"].
20+
"""
21+
22+
# init the args
23+
def __init__(self, **kwargs):
24+
super(CalculateDistanceConf, self).__init__(**kwargs)
25+
26+
# set default params
27+
@DocInherit
28+
def default(self):
29+
self.operations = ["cos", "euclidean", "manhattan", "chebyshev"]
30+
31+
@DocInherit
32+
def declare(self):
33+
self.num_of_inputs = 2
34+
self.input_ranks = [2]
35+
36+
@DocInherit
37+
def inference(self):
38+
self.output_dim = copy.deepcopy(self.input_dims[0])
39+
self.output_dim[-1] = 1
40+
41+
super(CalculateDistanceConf, self).inference()
42+
43+
@DocInherit
44+
def verify(self):
45+
super(CalculateDistanceConf, self).verify()
46+
47+
assert len(self.input_dims) == 2, "Operation requires that there should be two inputs"
48+
49+
# to check if the ranks of all the inputs are equal
50+
rank_equal_flag = True
51+
for i in range(len(self.input_ranks)):
52+
if self.input_ranks[i] != self.input_ranks[0] or self.input_ranks[i] != 2:
53+
rank_equal_flag = False
54+
break
55+
if rank_equal_flag == False:
56+
raise ConfigurationError("For layer CalculateDistance, the ranks of each inputs should be equal and 2!")
57+
58+
59+
class CalculateDistance(BaseLayer):
60+
""" CalculateDistance layer to calculate the distance of sequences(2D representation)
61+
62+
Args:
63+
layer_conf (CalculateDistanceConf): configuration of a layer
64+
"""
65+
66+
def __init__(self, layer_conf):
67+
super(CalculateDistance, self).__init__(layer_conf)
68+
self.layer_conf = layer_conf
69+
70+
71+
def forward(self, x, x_len, y, y_len):
72+
"""
73+
74+
Args:
75+
x: [batch_size, dim]
76+
x_len: [batch_size]
77+
y: [batch_size, dim]
78+
y_len: [batch_size]
79+
Returns:
80+
Tensor: [batch_size, 1], None
81+
82+
"""
83+
84+
batch_size = x.size()[0]
85+
if "cos" in self.layer_conf.operations:
86+
result = F.cosine_similarity(x , y)
87+
elif "euclidean" in self.layer_conf.operations:
88+
result = torch.sqrt(torch.sum((x-y)**2, dim=1))
89+
elif "manhattan" in self.layer_conf.operations:
90+
result = torch.sum(torch.abs((x - y)), dim=1)
91+
elif "chebyshev" in self.layer_conf.operations:
92+
result = torch.abs((x - y)).max(dim=1)
93+
else:
94+
raise ConfigurationError("This operation is not supported!")
95+
96+
result = result.view(batch_size, 1)
97+
return result, None

block_zoo/op/Combination.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ def inference(self):
4747
self.output_dim[-1] += int(np.mean([input_dim[-1] for input_dim in self.input_dims])) # difference operation requires dimension of all the inputs should be equal
4848
if "dot_multiply" in self.operations:
4949
self.output_dim[-1] += int(np.mean([input_dim[-1] for input_dim in self.input_dims])) # dot_multiply operation requires dimension of all the inputs should be equal
50-
5150
super(CombinationConf, self).inference()
5251

5352
@DocInherit

block_zoo/op/Expand_plus.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT license.
3+
4+
# Come from http://www.hangli-hl.com/uploads/3/1/6/8/3168008/hu-etal-nips2014.pdf [ARC-II]
5+
6+
import torch
7+
import torch.nn as nn
8+
import copy
9+
10+
from block_zoo.BaseLayer import BaseLayer, BaseConf
11+
from utils.DocInherit import DocInherit
12+
from utils.exceptions import ConfigurationError
13+
14+
class Expand_plusConf(BaseConf):
15+
"""Configuration for Expand_plus layer
16+
17+
"""
18+
def __init__(self, **kwargs):
19+
super(Expand_plusConf, self).__init__(**kwargs)
20+
21+
@DocInherit
22+
def default(self):
23+
self.operation = 'Plus'
24+
25+
@DocInherit
26+
def declare(self):
27+
self.num_of_inputs = 2
28+
self.input_ranks = [3, 3]
29+
30+
@DocInherit
31+
def inference(self):
32+
self.output_dim = copy.deepcopy(self.input_dims[0])
33+
if self.input_dims[0][1] == -1 or self.input_dims[1][1] == -1:
34+
raise ConfigurationError("For Expand_plus layer, the sequence length should be fixed")
35+
self.output_dim.insert(2, self.input_dims[1][1]) # y_len
36+
super(Expand_plusConf, self).inference() # PUT THIS LINE AT THE END OF inference()
37+
38+
@DocInherit
39+
def verify(self):
40+
super(Expand_plusConf, self).verify()
41+
42+
43+
class Expand_plus(BaseLayer):
44+
""" Expand_plus layer
45+
Given sequences X and Y, put X and Y expand_dim, and then add.
46+
47+
Args:
48+
layer_conf (Expand_plusConf): configuration of a layer
49+
50+
"""
51+
def __init__(self, layer_conf):
52+
53+
super(Expand_plus, self).__init__(layer_conf)
54+
assert layer_conf.input_dims[0][-1] == layer_conf.input_dims[1][-1]
55+
56+
57+
def forward(self, x, x_len, y, y_len):
58+
"""
59+
60+
Args:
61+
x: [batch_size, x_max_len, dim].
62+
x_len: [batch_size], default is None.
63+
y: [batch_size, y_max_len, dim].
64+
y_len: [batch_size], default is None.
65+
66+
Returns:
67+
output: batch_size, x_max_len, y_max_len, dim].
68+
69+
"""
70+
71+
x_new = torch.stack([x]*y.size()[1], 2) # [batch_size, x_max_len, y_max_len, dim]
72+
y_new = torch.stack([y]*x.size()[1], 1) # [batch_size, x_max_len, y_max_len, dim]
73+
74+
return x_new + y_new, None
75+
76+

0 commit comments

Comments
 (0)