Train utils
batchify(data: np.ndarray, batch_size: int, func: Callable[[np.ndarray], np.ndarray] | None = None) -> Iterator[np.ndarray]
Batchify data
. If func
is not None, then the emitted item is func(batch)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
NumPy array of items to batchify. |
required |
batch_size |
int
|
Batch size; must be between 1 and |
required |
func |
Callable[[np.ndarray], np.ndarray]
|
Optional function to apply to each emitted batch. Defaults to identity function. |
None
|
Returns:
Type | Description |
---|---|
Iterator[np.ndarray]
|
Iterator[np.ndarray]: Generator object containing batches. |
Source code in opskrift/train_utils.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
get_cosine_learning_rates(lr_min: float, lr_max: float, freq: float, num_points: int) -> list[float]
Decay the learning rate based on a cosine schedule of frequency freq
.
Returns a list of N
learning rate values in the interval [lr_min, lr_max]
.
Source code in opskrift/train_utils.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
split_data(data: list[Any], train_f: float, test_f: float, shuffle: bool = False) -> dict[str, list[Any]]
Get train / test / valid
splits from data
.
If shuffle
is True, then use a random permutation of data
.
valid
split size is given by (1 - train_f - test_f) * len(data)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
list[Any]
|
Any collection of items to be split. |
required |
train_f |
float
|
Train size factor from the entire length (must be between 0 and 1). |
required |
test_f |
float
|
Test size factor from the entire length (must be between 0 and 1). |
required |
shuffle |
bool
|
Whether to use a random permutation of |
False
|
Returns:
Type | Description |
---|---|
dict[str, list[Any]]
|
dict[str, list[Any]]: Keys are {train, test, valid}, and values are corresponding splits |
Source code in opskrift/train_utils.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|