-
Notifications
You must be signed in to change notification settings - Fork 4
/
tdnn.py
159 lines (132 loc) · 4.41 KB
/
tdnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import tensorflow as tf
import numpy as np
def renorm_layer(
inputs,
input_dim
):
#
# original code from Kaldi's NormalizeComponent
# y = x * (sqrt(dim(x)) * target-rms) / |x|
# for details, http://kaldi-asr.org/doc/nnet-normalize-component_8h_source.html
#
sqrt_output_dim = tf.sqrt(x = float(input_dim))
norm = tf.cast(
x = tf.reciprocal(
x = tf.norm(
tensor = inputs,
ord = 2,
axis = 2
)
),
dtype = tf.float32
)
norm = tf.expand_dims(
input = norm,
axis = 2
)
# norm.shape: [batch_size, sequence_length, input_dim]
sqrt_output_dims_mat = tf.reshape(
tensor = sqrt_output_dim,
shape = [1, 1]
)
sqrt_output_dims_mat = tf.multiply(
x = norm,
y = sqrt_output_dims_mat
)
scale_value = tf.multiply(
x = inputs,
y = sqrt_output_dims_mat
)
return scale_value
def tdnn(
inputs, # [batch_size, sequence_length, feature_dims]
context : list,
output_dim : int,
layer_name : str,
input_dim : int = None,
full_context = False,
pnorm_name : str = None,
renorm_name : str = None,
weights = None,
use_bias : bool = True,
bias = None,
trainable : bool = True
):
#
# the original code from https://github.com/SiddGururani/Pytorch-TDNN
#
def check_valid_context(context : list):
assert context[0] <= context[-1], \
"Input tensor dimensionality is incorrect. Should be a 3D tensor"
def get_kernel_width(context : list, full_context : bool):
if full_context:
context = list(range(context[0], context[-1] + 1))
return len(context), context
def get_valid_steps(context : list, input_sequence_length : int):
start = 0 if context[0] >= 0 else -1 * context[0]
end = input_sequence_length if context[-1] <= 0 else input_sequence_length - context[-1]
return tf.range(start=start, limit=end)
def is_full_context(context : list):
for idx, c in enumerate(context):
try:
if c + 1 != context[idx + 1]:
return False
except IndexError:
return True
input_shape = tf.shape(inputs)
sequence_length = input_shape[1]
check_valid_context(context)
kernel_width, context = get_kernel_width(context, full_context)
valid_steps = get_valid_steps(context, sequence_length)
context = [context] * 1
context = tf.tile(input = context, multiples = [tf.size(valid_steps), 1])
valid_steps = tf.expand_dims(valid_steps, 1)
selected_indices = context + valid_steps
# features.shape: [batch, sequence_length_of_output, kernel, feats_dims]
# reshape -> [batch, sequence_length_of_output, kernel * feats_dims]
# e.g., -> [batch, length, channels]
features = tf.gather(
params = inputs,
indices = selected_indices,
axis = 1
)
f_shape = tf.shape(features)
features = tf.reshape(
tensor = features,
shape = [f_shape[0], f_shape[1], -1]
)
if not weights:
weights = tf.contrib.layers.xavier_initializer()
#regularizer = None if not trainable else \
# tf.contrib.layers.l2_regularizer(scale = 0.01)
out = tf.layers.conv1d(
inputs = features,
filters = output_dim,
kernel_size = 1,
strides = 1,
data_format = "channels_last",
use_bias = use_bias,
name = layer_name,
kernel_initializer = weights,
kernel_regularizer = None,
bias_initializer = bias,
trainable = trainable
)
if pnorm_name:
out_shape = tf.shape(out)
out = tf.reshape(
tensor = out,
shape = [out_shape[0], out_shape[1], input_dim, -1]
)
out = tf.norm(
tensor = out,
ord = 2,
axis = 3,
name = pnorm_name
)
if renorm_name:
out = renorm_layer(
inputs = out,
input_dim = input_dim
)
return out