speechbrain.nnet.containers 模块
用于实现不同神经模块的级联(序列)的库。
- Authors
彼得·普兰廷加 2020
摘要
类:
使用快捷连接连接一系列块。 |
|
可以在前向方法中接受 |
|
该类实现了对torch.nn.ModuleList的封装,并提供了一个forward()方法,用于顺序前向传播所有层。 |
|
一个模块序列,可能在构建时推断形状。 |
参考
- class speechbrain.nnet.containers.Sequential(*layers, input_shape=None, **named_layers)[source]
基础:
ModuleDict一系列模块,可能在构建时推断形状。
如果层是带有名称传递的,则可以使用点符号引用它们。
- Parameters:
Example
>>> inputs = torch.rand(10, 40, 50) >>> model = Sequential(input_shape=inputs.shape) >>> model.append(Linear, n_neurons=100, layer_name="layer1") >>> model.append(Linear, n_neurons=200, layer_name="layer2") >>> outputs = model(inputs) >>> outputs.shape torch.Size([10, 40, 200]) >>> outputs = model.layer1(inputs) >>> outputs.shape torch.Size([10, 40, 100])
- class speechbrain.nnet.containers.LengthsCapableSequential(*args, **kwargs)[source]
基础类:
Sequential顺序模型可以在前向方法中接受
lengths。这对于包含RNN的顺序模型非常有用,在这些模型中避免填充非常重要,或者对于一些特征归一化层也是如此。
不幸的是,这个模块无法进行即时编译,因为编译器无法提前知道是否会传递长度参数,而且某些层不接受长度参数。
- class speechbrain.nnet.containers.ModuleList(*layers)[source]
基础:
Module该类实现了对torch.nn.ModuleList的封装,并提供了一个forward()方法,用于顺序前向传播所有层。 对于一些使用SpeechBrain旧版Sequential类实现的预训练模型,用户可以使用该类来加载这些预训练模型。
- Parameters:
*layers (torch class) – 要放入ModuleList中的Torch对象。
- class speechbrain.nnet.containers.ConnectBlocks(input_shape, shortcut_type='residual', shortcut_projection=False, shortcut_combine_fn=<built-in method add of type object>)[source]
基础:
Module使用快捷连接连接一系列块。
注意:所有快捷方式都从第一个块的输出开始,因为第一个块可能会显著改变形状。
- Parameters:
input_shape (tuple) – 形状
shortcut_type (str) – 其中之一: * “residual” - 第一个块的输出传递给最终输出, * “dense” - 每个块的输入来自所有先前的块, * “skip” - 每个块的输出传递给最终输出。
shortcut_projection (bool) – 仅在传递了
shortcut_type时有效。是否在合并输出之前向快捷连接添加一个线性投影层,以处理不同的大小。shortcut_combine_fn (str 或 function) – 可以是一个预定义的函数(如“add”、“sub”、“mul”、“div”、“avg”、“cat”之一),也可以是一个用户定义的函数,该函数接收快捷方式和下一个输入,并将它们组合起来,同时如果需要在函数内部初始化参数,还可以使用
init_params。
Example
>>> inputs = torch.rand(10, 100, 20) >>> model = ConnectBlocks( ... input_shape=inputs.shape, shortcut_projection=True ... ) >>> model.append(Linear, n_neurons=10) >>> model.append(Linear, n_neurons=10, end_of_block=True) >>> model.append(Linear, n_neurons=10) >>> model.append(Linear, n_neurons=10, end_of_block=True) >>> outputs = model(inputs) >>> outputs.shape torch.Size([10, 100, 10])