当前位置: 首页>>代码示例>>Python>>正文


Python module._addindent方法代码示例

本文整理汇总了Python中torch.nn.modules.module._addindent方法的典型用法代码示例。如果您正苦于以下问题:Python module._addindent方法的具体用法?Python module._addindent怎么用?Python module._addindent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在torch.nn.modules.module的用法示例。


在下文中一共展示了module._addindent方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: summary

# 需要导入模块: from torch.nn.modules import module [as 别名]
# 或者: from torch.nn.modules.module import _addindent [as 别名]
def summary(model, show_weights=True, show_parameters=True):
    """
    Summarizes torch model by showing trainable parameters and weights.
    """
    tmpstr = model.__class__.__name__ + ' (\n'
    total_params = 0
    for key, module in model._modules.items():
        # if it contains layers let call it recursively to get params
        # and weights
        if type(module) in [
            th.nn.modules.container.Container,
            th.nn.modules.container.Sequential
        ]:
            modstr = summary(module)
        else:
            modstr = module.__repr__()
        modstr = _addindent(modstr, 2)

        params = sum([np.prod(p.size()) for p in module.parameters()])
        weights = tuple([tuple(p.size()) for p in module.parameters()])
        total_params += params

        tmpstr += '  (' + key + '): ' + modstr
        if show_weights:
            tmpstr += ', weights={}'.format(weights)
        if show_parameters:
            tmpstr += ', parameters={}'.format(params)
        tmpstr += '\n'

    tmpstr = tmpstr + ') Total Parameters={}'.format(total_params)
    return tmpstr 
开发者ID:ConvLab,项目名称:ConvLab,代码行数:33,代码来源:base_modules.py

示例2: torch_summarize

# 需要导入模块: from torch.nn.modules import module [as 别名]
# 或者: from torch.nn.modules.module import _addindent [as 别名]
def torch_summarize(model, show_weights=True, show_parameters=True):
    """
    Summarizes torch model by showing trainable parameters and weights.
    Taken from:
    https://stackoverflow.com/questions/42480111/model-summary-in-pytorch#42616812
    """

    tmpstr = model.__class__.__name__ + ' (\n'
    for key, module in model._modules.items():
        # if it contains layers let call it recursively to get params and weights
        if type(module) in [
            torch.nn.modules.container.Container,
            torch.nn.modules.container.Sequential
        ]:
            modstr = torch_summarize(module)
        else:
            modstr = module.__repr__()
        modstr = _addindent(modstr, 2)

        params = sum([np.prod(p.size()) for p in module.parameters()])
        weights = tuple([tuple(p.size()) for p in module.parameters()])

        tmpstr += '  (' + key + '): ' + modstr
        if show_weights:
            tmpstr += ', weights={}'.format(weights)
        if show_parameters:
            tmpstr += ', parameters={}'.format(params)
        tmpstr += '\n'

    tmpstr = tmpstr + ')'
    return tmpstr 
开发者ID:UKPLab,项目名称:e2e-nlg-challenge-2017,代码行数:33,代码来源:visualize.py

示例3: hierarchicalsummary

# 需要导入模块: from torch.nn.modules import module [as 别名]
# 或者: from torch.nn.modules.module import _addindent [as 别名]
def hierarchicalsummary(model):
    def repr(model):
        # We treat the extra repr like the sub-module, one item per line
        extra_lines = []
        extra_repr = model.extra_repr()
        # empty string will be split into list ['']
        if extra_repr:
            extra_lines = extra_repr.split('\n')
        child_lines = []
        total_params = 0
        for key, module in model._modules.items():
            mod_str, num_params = repr(module)
            mod_str = _addindent(mod_str, 2)
            child_lines.append('(' + key + '): ' + mod_str)
            total_params += num_params
        lines = extra_lines + child_lines

        for name, p in model._parameters.items():
            if p is not None:
                total_params += reduce(lambda x, y: x * y, p.shape)

        main_str = model._get_name() + '('
        if lines:
            # simple one-liner info, which most builtin Modules will use
            if len(extra_lines) == 1 and not child_lines:
                main_str += extra_lines[0]
            else:
                main_str += '\n  ' + '\n  '.join(lines) + '\n'

        main_str += ')'
        main_str += ', {:,} params'.format(total_params)
        return main_str, total_params

    string, count = repr(model)
    print(string)
    return count 
开发者ID:graykode,项目名称:modelsummary,代码行数:38,代码来源:hierarchicalsummary.py

示例4: torch_summarize

# 需要导入模块: from torch.nn.modules import module [as 别名]
# 或者: from torch.nn.modules.module import _addindent [as 别名]
def torch_summarize(model, show_weights=True, show_parameters=True):
    """Summarizes torch model by showing trainable parameters and weights."""
    tmpstr = model.__class__.__name__ + ' (\n'
    parameters = 0
    convs = 0
    for key, module in model._modules.items():
        # if it contains layers let call it recursively to get params and weights
        if type(module) in [torch.nn.modules.container.Container, torch.nn.modules.container.Sequential]:
            modstr, p, cnvs = torch_summarize(module)
            parameters += p
            convs += cnvs
        else:
            modstr = module.__repr__()
            convs += len(modstr.split('Conv2d')) - 1

        modstr = _addindent(modstr, 2)
        # if 'conv' in key:
        #     convs += 1

        params = sum([np.prod(p.size()) for p in module.parameters()])
        parameters += params
        weights = tuple([tuple(p.size()) for p in module.parameters()])

        tmpstr += '  (' + key + '): ' + modstr
        if show_weights:
            tmpstr += ', weights={}'.format(weights)
        if show_parameters:
            tmpstr += ', parameters={} / {}'.format(params, parameters)
        tmpstr += ', convs={}'.format(convs)
        tmpstr += '\n'

    tmpstr = tmpstr + ')'
    return tmpstr, parameters, convs 
开发者ID:ok1zjf,项目名称:VASNet,代码行数:35,代码来源:sys_utils.py

示例5: summary

# 需要导入模块: from torch.nn.modules import module [as 别名]
# 或者: from torch.nn.modules.module import _addindent [as 别名]
def summary(model, show_weights=True, show_parameters=True):
    """
    Summarizes torch model by showing trainable parameters and weights.
    """
    tmpstr = model.__class__.__name__ + ' (\n'
    total_params = 0
    for key, module in model._modules.items():
        # if it contains layers let call it recursively to get params
        # and weights
        if type(module) in [
            torch.nn.modules.container.Container,
            torch.nn.modules.container.Sequential
        ]:
            modstr = summary(module)
        else:
            modstr = module.__repr__()
        modstr = _addindent(modstr, 2)

        params = sum([np.prod(p.size()) for p in module.parameters()])
        weights = tuple([tuple(p.size()) for p in module.parameters()])
        total_params += params

        tmpstr += '  (' + key + '): ' + modstr
        if show_weights:
            tmpstr += ', weights={}'.format(weights)
        if show_parameters:
            tmpstr += ', parameters={}'.format(params)
        tmpstr += '\n'

    tmpstr = tmpstr + ') Total Parameters={}'.format(total_params)
    return tmpstr 
开发者ID:zengjichuan,项目名称:Topic_Disc,代码行数:33,代码来源:model_bases.py

示例6: torch_summarize

# 需要导入模块: from torch.nn.modules import module [as 别名]
# 或者: from torch.nn.modules.module import _addindent [as 别名]
def torch_summarize(model, show_weights = True, show_parameters = True, show_trainable = True):

	"""Summarizes torch model by showing trainable parameters and weights."""
	tmpstr = model.__class__.__name__ + " (\n"

	for key, module in model._modules.items():

		# If it contains layers let call it recursively to get params and weights
		if type(module) in [
			torch.nn.modules.container.Container,
			torch.nn.modules.container.Sequential
		]:
			modstr = torch_summarize(module)
		else:
			modstr = module.__repr__()

		# ====================== Extra stuff (for displaying nn.Parameter) ======================
		lst_params = []
		for name, p in module.named_parameters():
			if(type(p) == torch.nn.parameter.Parameter and "weight" not in name and "bias" not in name):
				lst_params.append("  ({}): Parameter{}".format( name, tuple(p.size()) ))

		if(lst_params):
			modstr = modstr[:-1]
			modstr += "\n".join(lst_params)
			modstr += "\n)"
		# ====================== Extra stuff (for displaying nn.Parameter) ======================

		modstr = _addindent(modstr, 2)

		weights = tuple([tuple(p.size()) for p in module.parameters()])
		params = sum([np.prod(p.size()) for p in module.parameters()])

		total_params = sum([torch.LongTensor(list(p.size())).prod() for p in module.parameters()])
		trainable_params = sum([torch.LongTensor(list(p.size())).prod() for p in module.parameters() if p.requires_grad])

		tmpstr += "  (" + key + "): " + modstr 
		if show_weights:
			tmpstr += ", weights = {}".format(weights)
		if show_parameters:
			tmpstr +=  ", parameters = {:,}".format(params)
		if show_trainable and total_params != 0 and total_params == trainable_params:
			tmpstr +=  " (Trainable)"
		tmpstr += "\n"   

	tmpstr = tmpstr + ")"
	return tmpstr


# Generates a summary of the given model 
开发者ID:almightyGOSU,项目名称:ANR,代码行数:52,代码来源:utilities.py

示例7: summary

# 需要导入模块: from torch.nn.modules import module [as 别名]
# 或者: from torch.nn.modules.module import _addindent [as 别名]
def summary(model, file=sys.stdout):
    def repr(model):
        # We treat the extra repr like the sub-module, one item per line
        extra_lines = []
        extra_repr = model.extra_repr()
        # empty string will be split into list ['']
        if extra_repr:
            extra_lines = extra_repr.split('\n')
        child_lines = []
        total_params = 0
        for key, module in model._modules.items():
            mod_str, num_params = repr(module)
            mod_str = _addindent(mod_str, 2)
            child_lines.append('(' + key + '): ' + mod_str)
            total_params += num_params
        lines = extra_lines + child_lines

        for name, p in model._parameters.items():
            if hasattr(p, 'shape'):
                total_params += reduce(lambda x, y: x * y, p.shape)

        main_str = model._get_name() + '('
        if lines:
            # simple one-liner info, which most builtin Modules will use
            if len(extra_lines) == 1 and not child_lines:
                main_str += extra_lines[0]
            else:
                main_str += '\n  ' + '\n  '.join(lines) + '\n'

        main_str += ')'
        if file is sys.stdout:
            main_str += ', \033[92m{:,}\033[0m params'.format(total_params)
        else:
            main_str += ', {:,} params'.format(total_params)
        return main_str, total_params

    string, count = repr(model)
    if file is not None:
        if isinstance(file, str):
            file = open(file, 'w')
        print(string, file=file)
        file.flush()

    return count 
开发者ID:jongwook,项目名称:onsets-and-frames,代码行数:46,代码来源:utils.py


注:本文中的torch.nn.modules.module._addindent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。