|
|
|
@ -22,8 +22,6 @@ __all__ = [
|
|
|
|
|
|
|
|
|
|
def summary(layer: nn.Layer, print_func=print):
|
|
|
|
|
num_params = num_elements = 0
|
|
|
|
|
if print_func:
|
|
|
|
|
print_func(f"{layer.__class__.__name__} summary:")
|
|
|
|
|
for name, param in layer.state_dict().items():
|
|
|
|
|
if print_func:
|
|
|
|
|
print_func(
|
|
|
|
@ -31,9 +29,7 @@ def summary(layer: nn.Layer, print_func=print):
|
|
|
|
|
num_elements += np.prod(param.shape)
|
|
|
|
|
num_params += 1
|
|
|
|
|
if print_func:
|
|
|
|
|
print_func(
|
|
|
|
|
f"{layer.__class__.__name__} has {num_params} parameters, {num_elements} elements."
|
|
|
|
|
)
|
|
|
|
|
print_func(f"Total parameters: {num_params}, {num_elements} elements.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gradient_norm(layer: nn.Layer):
|
|
|
|
@ -45,25 +41,6 @@ def gradient_norm(layer: nn.Layer):
|
|
|
|
|
return grad_norm_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def recursively_remove_weight_norm(layer: nn.Layer):
|
|
|
|
|
for layer in layer.sublayers():
|
|
|
|
|
try:
|
|
|
|
|
nn.utils.remove_weight_norm(layer)
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
# ther is not weight norm hoom in this layer
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def freeze(layer: nn.Layer):
|
|
|
|
|
for param in layer.parameters():
|
|
|
|
|
param.trainable = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def unfreeze(layer: nn.Layer):
|
|
|
|
|
for param in layer.parameters():
|
|
|
|
|
param.trainable = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_grads(model, print_func=print):
|
|
|
|
|
if print_func is None:
|
|
|
|
|
return
|
|
|
|
@ -75,12 +52,32 @@ def print_grads(model, print_func=print):
|
|
|
|
|
def print_params(model, print_func=print):
|
|
|
|
|
if print_func is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
total = 0.0
|
|
|
|
|
num_params = 0.0
|
|
|
|
|
for n, p in model.named_parameters():
|
|
|
|
|
msg = f"param: {n}: shape: {p.shape} stop_grad: {p.stop_gradient}"
|
|
|
|
|
msg = f"{n} | {p.shape} | {np.prod(p.shape)} | {not p.stop_gradient}"
|
|
|
|
|
total += np.prod(p.shape)
|
|
|
|
|
num_params += 1
|
|
|
|
|
if print_func:
|
|
|
|
|
print_func(msg)
|
|
|
|
|
if print_func:
|
|
|
|
|
print_func(f"Total parameters: {total}!")
|
|
|
|
|
print_func(f"Total parameters: {num_params}, {total} elements.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def recursively_remove_weight_norm(layer: nn.Layer):
|
|
|
|
|
for layer in layer.sublayers():
|
|
|
|
|
try:
|
|
|
|
|
nn.utils.remove_weight_norm(layer)
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
# ther is not weight norm hoom in this layer
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def freeze(layer: nn.Layer):
|
|
|
|
|
for param in layer.parameters():
|
|
|
|
|
param.trainable = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def unfreeze(layer: nn.Layer):
|
|
|
|
|
for param in layer.parameters():
|
|
|
|
|
param.trainable = True
|
|
|
|
|