speechbrain.nnet.activations 模块

实现激活函数的库。

作者
  • Mirco Ravanelli 2020

  • Jianyuan Zhong 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。

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

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

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

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

示例

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

返回输入张量的 softmax。

参数:

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

返回:

x_act – softmax 输出。

返回类型:

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

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

  • hard (bool) – 如果为 True,返回的样本将被离散化为 one-hot 向量,但在 autograd 中将被视为软样本进行微分

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

示例

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

返回输入张量的 Gumbel softmax。

参数:

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

返回类型:

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))

参数:

beta (float) – Beta 值。

示例

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

返回 Swish 处理后的输入张量。

参数:

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

返回类型:

Swish 处理后的输出。