def summary(model, input_size):
def register_hook(module):<EOL><INDENT>def hook(module, input, output):<EOL><INDENT>class_name = str(module.__class__).split('<STR_LIT:.>')[-<NUM_LIT:1>].split("<STR_LIT:'>")[<NUM_LIT:0>]<EOL>module_idx = len(summary)<EOL>m_key = '<STR_LIT>' % (class_name, module_idx + <NUM_LIT:1>)<EOL>summary[m_key] = OrderedDict()<EOL>summary[m_key]['<STR_LIT>'] = list(input[<NUM_LIT:0>].size())<EOL>summary[m_key]['<STR_LIT>'][<NUM_LIT:0>] = -<NUM_LIT:1><EOL>if isinstance(output, (list, tuple)):<EOL><INDENT>summary[m_key]['<STR_LIT>'] = [[-<NUM_LIT:1>] + list(o.size())[<NUM_LIT:1>:] for o in output]<EOL><DEDENT>else:<EOL><INDENT>summary[m_key]['<STR_LIT>'] = list(output.size())<EOL>summary[m_key]['<STR_LIT>'][<NUM_LIT:0>] = -<NUM_LIT:1><EOL><DEDENT>params = <NUM_LIT:0><EOL>if hasattr(module, '<STR_LIT>') and hasattr(module.weight, '<STR_LIT:size>'):<EOL><INDENT>params += torch.prod(torch.LongTensor(list(module.weight.size())))<EOL>summary[m_key]['<STR_LIT>'] = module.weight.requires_grad<EOL><DEDENT>if hasattr(module, '<STR_LIT>') and hasattr(module.bias, '<STR_LIT:size>'):<EOL><INDENT>params += torch.prod(torch.LongTensor(list(module.bias.size())))<EOL><DEDENT>summary[m_key]['<STR_LIT>'] = params<EOL><DEDENT>if (not isinstance(module, nn.Sequential) and<EOL>not isinstance(module, nn.ModuleList) and<EOL>not (module == model)):<EOL><INDENT>hooks.append(module.register_forward_hook(hook))<EOL><DEDENT><DEDENT>if torch.cuda.is_available():<EOL><INDENT>dtype = torch.cuda.FloatTensor<EOL>model = model.cuda()<EOL><DEDENT>else:<EOL><INDENT>dtype = torch.FloatTensor<EOL>model = model.cpu()<EOL><DEDENT>if isinstance(input_size[<NUM_LIT:0>], (list, tuple)):<EOL><INDENT>x = [Variable(torch.rand(<NUM_LIT:2>, *in_size)).type(dtype) for in_size in input_size]<EOL><DEDENT>else:<EOL><INDENT>x = Variable(torch.rand(<NUM_LIT:2>, *input_size)).type(dtype)<EOL><DEDENT>summary = OrderedDict()<EOL>hooks = []<EOL>model.apply(register_hook)<EOL>model(x)<EOL>for h in hooks:<EOL><INDENT>h.remove()<EOL><DEDENT>print('<STR_LIT>')<EOL>line_new = '<STR_LIT>'.format('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>print(line_new)<EOL>print('<STR_LIT>')<EOL>total_params = <NUM_LIT:0><EOL>trainable_params = <NUM_LIT:0><EOL>for layer in summary:<EOL><INDENT>line_new = '<STR_LIT>'.format(layer, str(summary[layer]['<STR_LIT>']),<EOL>'<STR_LIT>'.format(summary[layer]['<STR_LIT>']))<EOL>total_params += summary[layer]['<STR_LIT>']<EOL>if '<STR_LIT>' in summary[layer]:<EOL><INDENT>if summary[layer]['<STR_LIT>'] == True:<EOL><INDENT>trainable_params += summary[layer]['<STR_LIT>']<EOL><DEDENT><DEDENT>print(line_new)<EOL><DEDENT>print('<STR_LIT>')<EOL>print('<STR_LIT>'.format(total_params))<EOL>print('<STR_LIT>'.format(trainable_params))<EOL>print('<STR_LIT>'.format(total_params - trainable_params))<EOL>print('<STR_LIT>')<EOL>
Print summary of the model