【问题标题】:Purpose of '*' in front of Python3 function [duplicate]Python3函数前面'*'的目的[重复]
【发布时间】:2021-06-04 18:39:27
【问题描述】:

我在 Python3 和 PyTorch here 中看到了 ResNet CNN 的代码如下:

def resnet_block(input_channels, num_channels, num_residuals,
                 first_block=False):
    blk = []
    for i in range(num_residuals):
        if i == 0 and not first_block:
            blk.append(Residual(input_channels, num_channels,
                                use_1x1conv=True, strides=2))
        else:
            blk.append(Residual(num_channels, num_channels))
    return blk

要添加模块,使用以下代码-

b2 = nn.Sequential(*resnet_block(64, 64, 2, first_block=True))
b3 = nn.Sequential(*resnet_block(64, 128, 2))
b4 = nn.Sequential(*resnet_block(128, 256, 2))
b5 = nn.Sequential(*resnet_block(256, 512, 2))

“*resnet_block()”是什么意思/做什么?

【问题讨论】:

标签: python python-3.x pytorch iterable iterable-unpacking


【解决方案1】:

基本上*iterable 用于将可迭代对象的项目解包为位置参数。在您的问题中,resnet_block 返回一个列表,该列表中的项目被传递给 nn.Sequential 而不是列表本身。

【讨论】:

  • 可迭代解包不仅限于位置参数;它可以在所有场景中使用:仅位置参数、仅关键字参数、位置和关键字参数的组合。
猜你喜欢
  • 2013-06-20
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多