collections中namedtuple的用法

我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成:

`p = (1, 2)`

但是,看到(1, 2),很难看出这个tuple是用来表示一个坐标的。这时,namedtuple就派上了用场。

用法:

namedtuple('名称', [属性list])

使用namedtuple表示一个坐标的例子如下:

from collections import namedtuple

Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)

print(p.x,p.y)

输出为:`1,2'

再举一个Densenet的例子:

from collections import namedtuple

DensenetParams = namedtuple('DensenetParameters', ['num_classes',
                                         'first_output_features',
                                         'layers_per_block',
                                         'growth_rate',
                                         'bc_mode',
                                         'is_training',
                                         'dropout_keep_prob'
                                         ])

default_params = DensenetParams(
        num_classes = 10,
        first_output_features = 24,
        layers_per_block = 12,
        growth_rate = 12,
        bc_mode = False,
        is_training = True,
        dropout_keep_prob = 0.8,
        )

print(default_params.num_classes)

输出为:10

posted @ 2019-08-03 09:07  珠峰上吹泡泡  阅读(5867)  评论(0编辑  收藏  举报