-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
22 lines (18 loc) · 952 Bytes
/
utils.py
File metadata and controls
22 lines (18 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
def print_nonzeros(model):
nonzero = total = 0
for name, p in model.named_parameters():
tensor = p.data.cpu().numpy()
nz_count = np.count_nonzero(tensor)
total_params = np.prod(tensor.shape)
nonzero += nz_count
total += total_params
print(f'{name:20} | nonzeros = {nz_count:7} / {total_params:7} ({100 * nz_count / total_params:6.2f}%) | total_pruned = {total_params - nz_count :7} | shape = {tensor.shape}')
print(f'alive: {nonzero}, pruned : {total - nonzero}, total: {total}, Compression rate : {total/nonzero:10.2f}x ({100 * (total-nonzero) / total:6.2f}% pruned)')
def print_model_parameters(model, with_values=False):
print(f"{'Param name':20} {'Shape':30} {'Type':15}")
print('-'*70)
for name, param in model.named_parameters():
print(f'{name:20} {str(param.shape):30} {str(param.dtype):15}')
if with_values:
print(param)