pytorch打印各层信息 发表于 2020-04-18 | 分类于 python | 热度 ℃ | 字数统计 211 字 | 阅读时长 1 分钟 在pytorch中像keras一样打印出神经网络各层的信息。 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import collectionsimport torchdef paras_summary(input_size, model): def register_hook(module): def hook(module, input, output): class_name = str(module.__class__).split('.')[-1].split("'")[0] module_idx = len(summary) m_key = '%s-%i' % (class_name, module_idx+1) summary[m_key] = collections.OrderedDict() summary[m_key]['input_shape'] = list(input[0].size()) summary[m_key]['input_shape'][0] = -1 summary[m_key]['output_shape'] = list(output.size()) summary[m_key]['output_shape'][0] = -1 params = 0 if hasattr(module, 'weight'): params += torch.prod(torch.LongTensor(list(module.weight.size()))) if module.weight.requires_grad: summary[m_key]['trainable'] = True else: summary[m_key]['trainable'] = False if hasattr(module, 'bias'): params += torch.prod(torch.LongTensor(list(module.bias.size()))) summary[m_key]['nb_params'] = params if not isinstance(module, nn.Sequential) and \ not isinstance(module, nn.ModuleList) and \ not (module == model): hooks.append(module.register_forward_hook(hook)) # check if there are multiple inputs to the network if isinstance(input_size[0], (list, tuple)): x = [torch.rand(1,*in_size) for in_size in input_size] else: x = torch.rand(1,*input_size) # create properties summary = collections.OrderedDict() hooks = [] # register hook model.apply(register_hook) # make a forward pass model(x) # remove these hooks for h in hooks: h.remove() return summary 123net = Net()input_size=[3,32,32]paras_summary(input_size,net) donate the author Donate WeChat Pay Alipay