speechbrain.nnet.activations 模块

实现激活函数的库。

Authors
  • Mirco Ravanelli 2020

  • 钟建元 2020

摘要

类:

GumbelSoftmax

从Gumbel-Softmax分布中采样并可选地离散化。

Softmax

计算2D、3D或4D输入张量的softmax。

Swish

该类实现了来自https://arxiv.org/pdf/2005.03191.pdf的Swish激活函数

参考

class speechbrain.nnet.activations.Softmax(apply_log=False, dim=-1, reshape=True, dtype=torch.float32)[source]

基础:Module

计算2D、3D或4D输入张量的softmax。

Parameters:
  • apply_log (bool) – 是否在softmax之前应用log函数。

  • dim (int) – 如果应用softmax的维度。

  • reshape (bool) – 是否应用重塑(默认为true)

  • dtype (torch.dtype) – 输出张量的数据类型

Example

>>> classifier = Softmax()
>>> inputs = torch.rand(10, 50, 40)
>>> output = classifier(inputs)
>>> output.shape
torch.Size([10, 50, 40])
forward(x)[source]

返回输入张量的softmax。

Parameters:

x (torch.Tensor) – 输入张量。

Returns:

x_act – softmax 输出。

Return type:

torch.Tensor

class speechbrain.nnet.activations.GumbelSoftmax(tau, hard=False, apply_log=False)[source]

基础:Module

从Gumbel-Softmax分布中采样,并可选择离散化。

参考:https://arxiv.org/abs/1611.00712, https://arxiv.org/abs/1611.01144

Parameters:
  • tau (float) – 非负标量温度

  • hard (bool) – 如果为True,返回的样本将被离散化为one-hot向量,但在自动求导时将被视为软样本

  • apply_log (bool) – 如果为True,返回softmax输出的对数。

Example

>>> x = torch.randn((8, 40, 120))
>>> act = GumbelSoftmax(0.8, True)
>>> x = act(x)
forward(x)[source]

返回输入张量的Gumbel softmax。

Parameters:

x (torch.Tensor) – 输入张量。

Return type:

Gumbel softmax 输出。

class speechbrain.nnet.activations.Swish(beta: float = 1.0)[source]

基础:Module

该类实现了来自https://arxiv.org/pdf/2005.03191.pdf的Swish激活函数

给定输入 x。Swish(x) = x / (1 + exp(beta * x))

Parameters:

beta (float) – Beta 值。

Example

>>> x = torch.randn((8, 40, 120))
>>> act = Swish()
>>> x = act(x)
forward(x)[source]

返回经过Swish激活函数处理的输入张量。

Parameters:

x (torch.Tensor) – 输入张量。

Return type:

经过swish激活函数处理后的输出。