utils
Utility functions for the Landscaper package.
generate_random_orthogonal_directions(model, n=3)
Generates n random orthogonal directions in the parameter space of the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
The model for which to generate directions. |
required |
n
|
int
|
Number of orthogonal directions to generate. Defaults to 3. |
3
|
Returns:
Type | Description |
---|---|
list[list[Tensor]]
|
list[list[torch.Tensor]]: A list of n lists, each containing tensors representing an orthogonal direction. |
Source code in src/landscaper/utils.py
group_add(params, update, alpha=1)
Adds the update to the parameters with a scaling factor alpha.
Params = params + update*alpha
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
list[Tensor]
|
List of parameters. |
required |
update
|
list[Tensor]
|
List of updates. |
required |
alpha
|
float
|
Scaling factor. Defaults to 1. |
1
|
Returns:
Type | Description |
---|---|
list[Tensor]
|
list[torch.Tensor]: Updated list of parameters. |
Source code in src/landscaper/utils.py
group_product(xs, ys)
Computes the dot product of two lists of tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xs
|
list[Tensor]
|
List of tensors. |
required |
ys
|
list[Tensor]
|
List of tensors. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The sum of the element-wise products of the tensors in xs and ys. |
Source code in src/landscaper/utils.py
load_landscape(fp)
Loads a loss landscape from an .npz file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fp
|
str
|
Path to the file. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tuple |
(ArrayLike, ArrayLike)
|
loss, coordinates. |
Raises:
Type | Description |
---|---|
ValueError
|
Thrown if file is invalid. |
Source code in src/landscaper/utils.py
normalization(v)
Normalization of a list of vectors v.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v
|
list[Tensor]
|
List of tensors to normalize. |
required |
Returns:
Type | Description |
---|---|
list[Tensor]
|
list[torch.Tensor]: Normalized list of tensors. |
Source code in src/landscaper/utils.py
orthnormal(w, v_list)
Make vector w orthogonal to each vector in v_list and normalize the output w.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
w
|
list[Tensor]
|
The vector to be made orthogonal. |
required |
v_list
|
list[Tensor]
|
List of vectors to which w should be made orthogonal. |
required |
Returns:
Type | Description |
---|---|
list[Tensor]
|
list[torch.Tensor]: The orthogonalized and normalized vector w. |