forked from Snowdar/asv-subtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpooling.py
574 lines (463 loc) · 23.9 KB
/
pooling.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# -*- coding:utf-8 -*-
# Copyright xmuspeech (Author: Snowdar 2019-05-29 2020-06-10)
import numpy as np
import torch
import torch.nn.functional as F
from libs.support.utils import to_device
from .components import *
## Pooling ✿
class StatisticsPooling(torch.nn.Module):
""" An usual mean [+ stddev] poolling layer"""
def __init__(self, input_dim, stddev=True, unbiased=False, eps=1.0e-10):
super(StatisticsPooling, self).__init__()
self.stddev = stddev
self.input_dim = input_dim
if self.stddev :
self.output_dim = 2 * input_dim
else :
self.output_dim = input_dim
self.eps = eps
# Used for unbiased estimate of stddev
self.unbiased = unbiased
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
# Get the num of frames
counts = inputs.shape[2]
mean = inputs.sum(dim=2, keepdim=True) / counts
if self.stddev :
if self.unbiased and counts > 1:
counts = counts - 1
# The sqrt (as follows) is deprecated because it results in Nan problem.
# std = torch.unsqueeze(torch.sqrt(torch.sum((inputs - mean)**2, dim=2) / counts), dim=2)
# There is a eps to solve this problem.
# Another method: Var is equal to std in "cat" way, actually. So, just use Var directly.
var = torch.sum((inputs - mean)**2, dim=2, keepdim=True) / counts
std = torch.sqrt(var.clamp(min=self.eps))
return torch.cat((mean, std), dim=1)
else:
return mean
def get_output_dim(self):
return self.output_dim
def extra_repr(self):
return '{input_dim}, {output_dim}, stddev={stddev}, unbiased={unbiased}, eps={eps}'.format(**self.__dict__)
@classmethod
def thop_count(self, m, x, y):
pass
# To do
# x = x[0]
# kernel_ops = torch.zeros(m.weight.size()[2:]).numel() # Kw x Kh
# bias_ops = 1 if m.bias is not None else 0
# # N x Cout x H x W x (Cin x Kw x Kh + bias)
# total_ops = y.nelement() * (m.input_dim * kernel_ops + bias_ops)
# m.total_ops += torch.DoubleTensor([int(total_ops)])
class FreeStatisticsPooling(torch.nn.Module):
""" An usual mean [+ stddev] poolling layer"""
def __init__(self, stddev=True, unbiased=False, eps=1.0e-10):
super(FreeStatisticsPooling, self).__init__()
self.stddev = stddev
self.eps = eps
# Used for unbiased estimate of stddev
self.unbiased = unbiased
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
inputs = inputs.reshape(inputs.shape[0], -1, inputs.shape[len(inputs.shape)-1])
# Get the num of frames
counts = inputs.shape[2]
mean = inputs.sum(dim=2, keepdim=True) / counts
if self.stddev :
if self.unbiased and counts > 1:
counts = counts - 1
# The sqrt (as follows) is deprecated because it results in Nan problem.
# std = torch.unsqueeze(torch.sqrt(torch.sum((inputs - mean)**2, dim=2) / counts), dim=2)
# There is a eps to solve this problem.
# Another method: Var is equal to std in "cat" way, actually. So, just use Var directly.
var = torch.sum((inputs - mean)**2, dim=2, keepdim=True) / counts
std = torch.sqrt(var.clamp(min=self.eps))
return torch.cat((mean, std), dim=1)
else:
return mean
class LDEPooling(torch.nn.Module):
"""A novel learnable dictionary encoding layer.
Reference: Weicheng Cai, etc., "A NOVEL LEARNABLE DICTIONARY ENCODING LAYER FOR END-TO-END
LANGUAGE IDENTIFICATION", icassp, 2018
"""
def __init__(self, input_dim, c_num=64, eps=1.0e-10):
super(LDEPooling, self).__init__()
self.input_dim = input_dim
self.output_dim = input_dim * c_num
self.eps = eps
self.mu = torch.nn.Parameter(torch.randn(input_dim, c_num))
self.s = torch.nn.Parameter(torch.ones(c_num))
self.softmax_for_w = torch.nn.Softmax(dim=3)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
r = inputs.transpose(1,2).unsqueeze(3) - self.mu
# Make sure beta=self.s**2+self.eps > 0
w = self.softmax_for_w(- (self.s**2 + self.eps) * torch.sum(r**2, dim=2, keepdim=True))
e = torch.mean(w * r, dim=1)
return e.reshape(-1, self.output_dim, 1)
def get_output_dim(self):
return self.output_dim
## Xi-vector pooling (softplus_prec)
class xivec_stdinit_softplus2_prec_pooling(torch.nn.Module):
def __init__(self, input_dim, hidden_size=256, context=[0], stddev=False, train_mean=True, train_prec=True):
super(xivec_stdinit_softplus2_prec_pooling, self).__init__()
self.input_dim = input_dim
self.stddev = stddev
if self.stddev:
self.output_dim = 2 * input_dim
else:
self.output_dim = input_dim
self.prior_mean = torch.nn.Parameter(torch.zeros(1, input_dim), requires_grad=train_mean)
self.prior_logprec = torch.nn.Parameter(torch.zeros(1, input_dim), requires_grad=train_prec)
self.softmax = torch.nn.Softmax(dim=2)
# Log-precision estimator
self.lin1_relu_bn = ReluBatchNormTdnnLayer(input_dim, hidden_size, context)
self.lin2 = TdnnAffine(hidden_size, input_dim, context=context)
self.softplus2 = torch.nn.Softplus(beta=1, threshold=20)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
feat = inputs
# Log-precision estimator
logprec = self.softplus2(self.lin2(self.lin1_relu_bn(feat))) # frame precision estimate
logprec = 2.0*torch.log(logprec) # Square and take log before softmax
### Gaussian Posterior Inference
### Option 1: a_o (prior_mean-phi) included in variance
weight_attn = self.softmax(torch.cat((logprec, self.prior_logprec.repeat(logprec.shape[0], 1).unsqueeze(dim=2)), 2))
# Posterior precision
# Ls = torch.sum(torch.exp(torch.cat((logprec, self.prior_logprec.repeat(logprec.shape[0], 1).unsqueeze(dim=2)), 2)), dim=2)
# Posterior mean
phi = torch.sum(torch.cat((feat, self.prior_mean.repeat(feat.shape[0], 1).unsqueeze(dim=2)), 2) * weight_attn, dim=2)
if self.stddev:
sigma2 = torch.sum(torch.cat((feat, self.prior_mean.repeat(feat.shape[0], 1).unsqueeze(dim=2)), 2).pow(2) * weight_attn, dim=2)
sigma = torch.sqrt(torch.clamp(sigma2 - phi ** 2, min=1.0e-10))
return torch.cat((phi, sigma), dim=1).unsqueeze(dim=2)
else:
return phi.unsqueeze(dim=2)
def get_output_dim(self):
return self.output_dim
# Attention-based
class AttentionAlphaComponent(torch.nn.Module):
"""Compute the alpha with attention module.
alpha = softmax(v'·f(w·x + b) + k) or softmax(v'·x + k)
where f is relu here and bias could be lost.
Support:
1. Single or Multi-head attention
2. One affine or two affine
3. Share weight (last affine = vector) or un-shared weight (last affine = matrix)
4. Self-attention or time context attention (supported by context parameter of TdnnAffine)
5. Different temperatures for different heads.
"""
def __init__(self, input_dim, num_head=1, split_input=True, share=True, affine_layers=2,
hidden_size=64, context=[0], bias=True, temperature=False, fixed=True):
super(AttentionAlphaComponent, self).__init__()
assert num_head >= 1
# Multi-head case.
if num_head > 1:
if split_input:
# Make sure fatures/planes with input_dim dims could be splited to num_head parts.
print("input_dim:",input_dim)
assert input_dim % num_head == 0
if temperature:
if fixed:
t_list = []
for i in range(num_head):
t_list.append([[max(1, (i // 2) * 5)]])
# shape [1, num_head, 1, 1]
self.register_buffer('t', torch.tensor([t_list]))
else:
# Different heads have different temperature.
# Use 1 + self.t**2 in forward to make sure temperature >= 1.
self.t = torch.nn.Parameter(torch.zeros(1, num_head, 1, 1))
self.input_dim = input_dim
self.num_head = num_head
self.split_input = split_input
self.share = share
self.temperature = temperature
self.fixed = fixed
if share:
# weight: [input_dim, 1] or [input_dim, hidden_size] -> [hidden_size, 1]
final_dim = 1
elif split_input:
# weight: [input_dim, input_dim // num_head] or [input_dim, hidden_size] -> [hidden_size, input_dim // num_head]
final_dim = input_dim // num_head
else:
# weight: [input_dim, input_dim] or [input_dim, hidden_size] -> [hidden_size, input_dim]
final_dim = input_dim
first_groups = 1
last_groups = 1
if affine_layers == 1:
last_affine_input_dim = input_dim
# (x, 1) for global case and (x, h) for split case.
if num_head > 1 and split_input:
last_groups = num_head
self.relu_affine = False
elif affine_layers == 2:
last_affine_input_dim = hidden_size * num_head
if num_head > 1:
# (1, h) for global case and (h, h) for split case.
last_groups = num_head
if split_input:
first_groups = num_head
# Add a relu-affine with affine_layers=2.
self.relu_affine = True
self.first_affine = TdnnAffine(input_dim, last_affine_input_dim, context=context, bias=bias, groups=first_groups)
self.relu = torch.nn.ReLU(inplace=True)
else:
raise ValueError("Expected 1 or 2 affine layers, but got {}.",format(affine_layers))
self.last_affine = TdnnAffine(last_affine_input_dim, final_dim * num_head, context=context, bias=bias, groups=last_groups)
# Dim=2 means to apply softmax in different frames-index (batch is a 3-dim tensor in this case).
self.softmax = torch.nn.Softmax(dim=2)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
if self.temperature:
batch_size = inputs.shape[0]
chunk_size = inputs.shape[2]
x = inputs
if self.relu_affine:
x = self.relu(self.first_affine(x))
if self.num_head > 1 and self.temperature:
if self.fixed:
t = self.t
else:
t = 1 + self.t**2
x = self.last_affine(x).reshape(batch_size, self.num_head, -1, chunk_size) / t
return self.softmax(x.reshape(batch_size, -1, chunk_size))
else:
return self.softmax(self.last_affine(x))
class AttentiveStatisticsPooling(torch.nn.Module):
""" An attentive statistics pooling.
Reference: Okabe, Koji, Takafumi Koshinaka, and Koichi Shinoda. 2018. "Attentive Statistics Pooling
for Deep Speaker Embedding." ArXiv Preprint ArXiv:1803.10963.
"""
def __init__(self, input_dim, affine_layers=2, hidden_size=64, context=[0], stddev=True, stddev_attention=True, eps=1.0e-10):
super(AttentiveStatisticsPooling, self).__init__()
self.stddev = stddev
self.input_dim = input_dim
if self.stddev :
self.output_dim = 2 * input_dim
else :
self.output_dim = input_dim
self.eps = eps
self.stddev_attention = stddev_attention
self.attention = AttentionAlphaComponent(input_dim, num_head=1, share=True, affine_layers=affine_layers,
hidden_size=hidden_size, context=context)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
alpha = self.attention(inputs)
# Weight avarage
mean = torch.sum(alpha * inputs, dim=2, keepdim=True)
if self.stddev :
if self.stddev_attention:
var = torch.sum(alpha * inputs**2, dim=2, keepdim=True) - mean**2
std = torch.sqrt(var.clamp(min=self.eps))
else:
var = torch.mean((inputs - mean)**2, dim=2, keepdim=True)
std = torch.sqrt(var.clamp(min=self.eps))
return torch.cat((mean, std), dim=1)
else :
return mean
def get_output_dim(self):
return self.output_dim
class MultiHeadAttentionPooling(torch.nn.Module):
"""Implement multi-head attention pooling based on AttentionAlphaComponent.
Reference: Safari, Pooyan, and Javier Hernando. 2019. “Self Multi-Head Attention for Speaker
Recognition.” ArXiv Preprint ArXiv:1906.09890.
Note, in this paper, affine_layers is default to 1, and final_dim is 1 which means the weights are shared.
"""
def __init__(self, input_dim, stddev=True, stddev_attention=True, num_head=4, share=True, affine_layers=1, **options):
super(MultiHeadAttentionPooling, self).__init__()
self.input_dim = input_dim
self.stddev = stddev
self.stddev_attention = stddev_attention
self.num_head = num_head
if self.stddev :
self.output_dim = 2 * input_dim
else :
self.output_dim = input_dim
if "split_input" in options.keys():
if not options["split_input"]:
raise ValueError("split_input==False is not valid for this MultiHeadAttentionPooling.")
options.pop("split_input")
# In this pooling, the special point is that inputs will be splited.
self.attention = AttentionAlphaComponent(input_dim, num_head=num_head, split_input=True, share=share,
affine_layers=affine_layers, bias=False, **options)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
batch_size = inputs.shape[0]
chunk_size = inputs.shape[2] # a.k.a total frames
# alpha: [batch, weight, frames]
# When using the conv1d to implement the multi-multiple of multi-head, we can get
# the weight distribution of multi-head: [h11, h12, h13, h21, h22, h23, ..., hn1, hn2, ...]
# So, just reshape it to split different heads.
alpha = self.attention(inputs)
# In sharing weight case, the shape of alpha is [batch, head, 1, frames] and [batch, head, splited-features, frames]
# for another case.
# inputs: [batch, head, splited-features, frames]
after_mul = alpha.reshape(batch_size, self.num_head, -1, chunk_size) * \
inputs.reshape(batch_size, self.num_head, -1, chunk_size)
# After multi-multipling alpha and inputs for multi-head case, the mean could be got by reshaping back.
mean = torch.sum(after_mul.reshape(batch_size, -1, chunk_size), dim=2, keepdim=True)
if self.stddev :
if self.stddev_attention:
after_mul_2 = alpha.reshape(batch_size, self.num_head, -1, chunk_size) * \
inputs.reshape(batch_size, self.num_head, -1, chunk_size)**2
var = torch.sum(after_mul_2.reshape(batch_size, -1, chunk_size), dim=2, keepdim=True) - mean**2
std = torch.sqrt(var.clamp(min=1.0e-10))
else:
var = torch.mean((inputs - mean)**2, dim=2, keepdim=True)
std = torch.sqrt(var.clamp(min=1.0e-10))
return torch.cat((mean, std), dim=1)
else :
return mean
def get_output_dim(self):
return self.output_dim
class GlobalMultiHeadAttentionPooling(torch.nn.Module):
"""Implement global multi-head attention pooling based on AttentionAlphaComponent.
Reference: Zhiming Wang, Kaisheng Yao, Xiaolong Li, Shuo Fang. "MULTI-RESOLUTION MULTI-HEAD
ATTENTION IN DEEP SPEAKER EMBEDDING." ICASSP, 2020.
It is not equivalent to multi-head attention pooling even when
input_dim of global multi-head = 1/num_head * input_dim of multi-head.
"""
def __init__(self, input_dim, stddev=True, stddev_attention=True, num_head=4, share=True, affine_layers=2, **options):
super(GlobalMultiHeadAttentionPooling, self).__init__()
self.input_dim = input_dim
self.num_head = num_head
self.stddev = stddev
self.stddev_attention = stddev_attention
if self.stddev :
self.output_dim = 2 * input_dim
else :
self.output_dim = input_dim
if "split_input" in options.keys():
if options["split_input"]:
raise ValueError("split_input==True is not valid for GlobalMultiHeadAttentionPooling.")
options.pop("split_input")
if "temperature" in options.keys():
if options["temperature"]:
raise ValueError("temperature==True is not valid for GlobalMultiHeadAttentionPooling.")
options.pop("temperature")
# In this pooling, the special point is that all (global) features of inputs will be used.
self.attention = AttentionAlphaComponent(input_dim, num_head=num_head, split_input=False, share=share,
temperature=False, affine_layers=affine_layers, bias=True, **options)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
batch_size = inputs.shape[0]
chunk_size = inputs.shape[2] # a.k.a total frames
# alpha: [batch, weight, frames]
# When using the conv1d to implement the multi-multiple of multi-head, we can get
# the weight distribution of multi-head: [h11, h12, h13, h21, h22, h23, ..., hn1, hn2, ...]
# So, just reshape it to split different heads.
alpha = self.attention(inputs)
# In sharing weight case, the shape of alpha is [batch, head, 1, frames] and [batch, head, all-features, frames]
# for another case.
# inputs: [batch, 1, all-features, frames]
after_mul = alpha.reshape(batch_size, self.num_head, -1, chunk_size) * \
inputs.reshape(batch_size, 1, -1, chunk_size)
# After multi-multipling alpha and inputs for multi-head case, the mean could be got by reshaping back.
mean = torch.sum(after_mul.reshape(batch_size, -1, chunk_size), dim=2, keepdim=True)
if self.stddev :
if self.stddev_attention:
after_mul_2 = alpha.reshape(batch_size, self.num_head, -1, chunk_size) * \
inputs.reshape(batch_size, 1, -1, chunk_size)**2
var = torch.sum(after_mul_2.reshape(batch_size, -1, chunk_size), dim=2, keepdim=True) - mean**2
std = torch.sqrt(var.clamp(min=1.0e-10))
else:
var = torch.mean((inputs - mean)**2, dim=2, keepdim=True)
std = torch.sqrt(var.clamp(min=1.0e-10))
return torch.cat((mean, std), dim=1)
else :
return mean
def get_output_dim(self):
return self.output_dim * self.num_head
class MultiResolutionMultiHeadAttentionPooling(torch.nn.Module):
"""Implement multi-resolution global multi-head attention pooling based on AttentionAlphaComponent.
Reference: Zhiming Wang, Kaisheng Yao, Xiaolong Li, Shuo Fang. "MULTI-RESOLUTION MULTI-HEAD
ATTENTION IN DEEP SPEAKER EMBEDDING." ICASSP, 2020.
"""
def __init__(self, input_dim, stddev=True, stddev_attention=True, num_head=4, share=True, affine_layers=2, **options):
super(MultiResolutionMultiHeadAttentionPooling, self).__init__()
self.input_dim = input_dim
self.num_head = num_head
self.stddev = stddev
self.stddev_attention = stddev_attention
if self.stddev :
self.output_dim = 2 * input_dim
else :
self.output_dim = input_dim
if "split_input" in options.keys():
if options["split_input"]:
raise ValueError("split_input==True is not valid for MultiResolutionMultiHeadAttentionPooling.")
options.pop("split_input")
if "temperature" in options.keys():
if not options["temperature"]:
raise ValueError("temperature==False is not valid for MultiResolutionMultiHeadAttentionPooling.")
options.pop("temperature")
# In this pooling, the special point is that all (global) features of inputs will be used and
# the temperature will be added.
self.attention = AttentionAlphaComponent(input_dim, num_head=num_head, split_input=False, temperature=True,
share=share, affine_layers=affine_layers, bias=True, **options)
def forward(self, inputs):
"""
@inputs: a 3-dimensional tensor (a batch), including [samples-index, frames-dim-index, frames-index]
"""
assert len(inputs.shape) == 3
assert inputs.shape[1] == self.input_dim
batch_size = inputs.shape[0]
chunk_size = inputs.shape[2] # a.k.a total frames
# alpha: [batch, weight, frames]
# When using the conv1d to implement the multi-multiple of multi-head, we can get
# the weight distribution of multi-head: [h11, h12, h13, h21, h22, h23, ..., hn1, hn2, ...]
# So, just reshape it to split different heads.
alpha = self.attention(inputs)
# In sharing weight case, the shape of alpha is [batch, head, 1, frames] and [batch, head, all-features, frames]
# for another case.
# inputs: [batch, 1, all-features, frames]
after_mul = alpha.reshape(batch_size, self.num_head, -1, chunk_size) * \
inputs.reshape(batch_size, 1, -1, chunk_size)
# After multi-multipling alpha and inputs for multi-head case, the mean could be got by reshaping back.
mean = torch.sum(after_mul.reshape(batch_size, -1, chunk_size), dim=2, keepdim=True)
if self.stddev :
if self.stddev_attention:
after_mul_2 = alpha.reshape(batch_size, self.num_head, -1, chunk_size) * \
inputs.reshape(batch_size, 1, -1, chunk_size)**2
var = torch.sum(after_mul_2.reshape(batch_size, -1, chunk_size), dim=2, keepdim=True) - mean**2
std = torch.sqrt(var.clamp(min=1.0e-10))
else:
var = torch.mean((inputs - mean)**2, dim=2, keepdim=True)
std = torch.sqrt(var.clamp(min=1.0e-10))
return torch.cat((mean, std), dim=1)
else :
return mean
def get_output_dim(self):
return self.output_dim * self.num_head