(model, top_k=15)
| 78 | |
| 79 | |
| 80 | def count_parameters(model, top_k=15): |
| 81 | table = PrettyTable([f"Modules (only show top {top_k} modules)", "Parameters"]) |
| 82 | total_params = 0 |
| 83 | param_count = defaultdict(int) |
| 84 | |
| 85 | for name, parameter in model.named_parameters(): |
| 86 | if not parameter.requires_grad: |
| 87 | continue |
| 88 | |
| 89 | top_level_module = name.split('.')[0] |
| 90 | |
| 91 | params = parameter.numel() |
| 92 | param_count[top_level_module] += params |
| 93 | |
| 94 | total_params += params |
| 95 | |
| 96 | sorted_params = sorted(param_count.items(), key=lambda x: x[1], reverse=True) |
| 97 | for i, (module_name, param_count) in enumerate(sorted_params): |
| 98 | if i < top_k: |
| 99 | table.add_row([module_name, param_count]) |
| 100 | |
| 101 | print(table) |
| 102 | print(f"Total Trainable Params: {total_params}") |
| 103 | return total_params |
| 104 | |
| 105 | def tensor_to_pil(tensor): |
| 106 | """ |
nothing calls this directly
no outgoing calls
no test coverage detected