main.bend checks
raw source on the hub · import mlbend-preprocess@0.1.0.0/main.bend as Main
mlbend-preprocess: the transformers of scikit-learn, over bend-ml's matrix.
import mlbend-preprocess@0.1.0.0/main.bend as P
A transformer takes a matrix and gives a matrix of the same shape. The
matrix is bend-ml's: Mat<r, c> from bend-ml-tensor, the one
bend-ml's autodiff takes, so the estimators that follow can be handed
to a model without a copy. MLBend writes no matrix of its own.
The standardiser is bend-ml's own arithmetic: a column-wise
normalisation is a transpose, then Mat.layernorm (which centres a
row and scales it by its own standard deviation), then a transpose
back. The min-max scaler and the normalisers use Vec.dot,
Vec.scale and Mat.to_rows.
What is proved: the number of rows of a one-hot matrix, and the shape of every result (the type says it, and the checker verifies it in every operation). What is tested: the numbers, against NumPy and scikit-learn (reference/test_preprocess.py). F32 is not a real number, so a value is never proved, only tested.
2 imports
import Base import bend-ml-tensor@0.1.0.0/main.bend as T
Laws
law one_hot_count_length provedsource · line 31 · raw
@ys:List<&2, Nat> -> {one_hot_count_of(ys) == List.length(&2, Nat, ys) : Nat}LAW (one_hot_count_length): the one-hot matrix has one row for every label.
Types
type Bounds source · line 36 · raw
Data
The extremes of every column: the smallest and the largest.
Bounds@mins:List<&2, F32> -> @maxs:List<&2, F32> -> Bounds
Definitions
def bounds_max_go source · line 40 · raw
@maxs:List<&2, F32> -> @row:List<&2, F32> -> @+acc:List<&2, F32> -> List<&2, F32>
The largest of two rows, cell by cell.
def bounds_max source · line 52 · raw
@maxs:List<&2, F32> -> @row:List<&2, F32> -> List<&2, F32>
The largest of two rows, cell by cell.
def bounds_min_go source · line 56 · raw
@mins:List<&2, F32> -> @row:List<&2, F32> -> @+acc:List<&2, F32> -> List<&2, F32>
The smallest of two rows, cell by cell.
def bounds_min source · line 68 · raw
@mins:List<&2, F32> -> @+row:List<&2, F32> -> List<&2, F32>
The smallest of two rows, cell by cell.
def bounds_of_go source · line 72 · raw
@+rows:List<&2, List<&2, F32>> -> @+mins:List<&2, F32> -> @+maxs:List<&2, F32> -> Bounds
The extremes of every column, row by row.
def bounds_of_rows source · line 81 · raw
@+rows:List<&2, List<&2, F32>> -> Bounds
The extremes of every column, row by row.
def bounds_of source · line 89 · raw
@+r:Nat -> @+c:Nat -> @a:0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c> -> Bounds
The extremes of every column of a matrix.
def l1_finish_go source · line 93 · raw
@acc:List<&2, F32> -> @+k:F32 -> @+out:List<&2, F32> -> List<&2, F32>
The row, divided, cell by cell.
def l1_finish source · line 101 · raw
@acc:List<&2, F32> -> @total:F32 -> List<&2, F32>
The row, divided.
def l1_row_go source · line 105 · raw
@+row:List<&2, F32> -> @+acc:List<&2, F32> -> @+total:F32 -> List<&2, F32>
One row divided by the sum of its absolute values, cell by cell.
def l1_row source · line 113 · raw
@+row:List<&2, F32> -> List<&2, F32>
One row divided by the sum of its absolute values.
def l1_normalize_go source · line 117 · raw
@+rows:List<&2, List<&2, F32>> -> @+acc:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>
The rows of a matrix, each divided by the sum of its absolute values.
def mat_of_ source · line 126 · raw
@+r:Nat -> @+c:Nat -> @m:Maybe<&2, 0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c>> -> 0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c>
A matrix of r rows of c numbers, when there is one.
def mat_of source · line 135 · raw
@+r:Nat -> @+c:Nat -> @+rows:List<&2, List<&2, F32>> -> 0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c>
A matrix of r rows of c numbers. When the rows do not fit, the matrix is empty: a shape that does not hold is not a matrix.
def l1_normalize source · line 140 · raw
@+r:Nat -> @+c:Nat -> @a:0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c> -> 0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c>
The L1 normaliser: every row divided by the sum of its absolute values.
def l2_finish_go source · line 144 · raw
@acc:List<&2, F32> -> @+norm:F32 -> @+out:List<&2, F32> -> List<&2, F32>
The row, divided, cell by cell.
def l2_norm_go source · line 152 · raw
@acc:List<&2, F32> -> @+acc_sum:F32 -> F32
The length of a row, cell by cell.
def l2_norm source · line 160 · raw
@acc:List<&2, F32> -> F32
The length of a row.
def l2_finish source · line 164 · raw
@+acc:List<&2, F32> -> List<&2, F32>
The row, divided by its length.
def l2_row_go source · line 168 · raw
@+row:List<&2, F32> -> @+acc:List<&2, F32> -> List<&2, F32>
One row divided by the length of its arrow, cell by cell.
def l2_row source · line 176 · raw
@+row:List<&2, F32> -> List<&2, F32>
One row divided by the length of its arrow.
def l2_normalize_go source · line 180 · raw
@+rows:List<&2, List<&2, F32>> -> @+acc:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>
The rows of a matrix, each divided by the length of its arrow.
def l2_normalize source · line 189 · raw
@+r:Nat -> @+c:Nat -> @+a:0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c> -> 0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c>
The L2 normaliser: every row divided by the length of its arrow.
def scale_row_go source · line 193 · raw
@+row:List<&2, F32> -> @mins:List<&2, F32> -> @maxs:List<&2, F32> -> @+acc:List<&2, F32> -> List<&2, F32>
One row scaled column by column, cell by cell.
def scale_row source · line 212 · raw
@+row:List<&2, F32> -> @mins:List<&2, F32> -> @maxs:List<&2, F32> -> List<&2, F32>
One row scaled column by column.
def scaled_go source · line 216 · raw
@+rows:List<&2, List<&2, F32>> -> @+mins:List<&2, F32> -> @+maxs:List<&2, F32> -> @+acc:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>
A matrix scaled column by column, row by row.
def scaled_of source · line 226 · raw
@+r:Nat -> @+c:Nat -> @a:0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c> -> @+b:Bounds -> List<&2, List<&2, F32>>
A matrix scaled column by column.
def scaled source · line 232 · raw
@+r:Nat -> @+c:Nat -> @a:0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c> -> @+b:Bounds -> List<&2, List<&2, F32>>
A matrix scaled column by column: every cell is (x - min) / (max - min).
def min_max source · line 236 · raw
@+r:Nat -> @+c:Nat -> @+a:0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c> -> 0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c>
The min-max scaler: every column mapped onto [0, 1].
def replace_at_go source · line 240 · raw
@+xs:List<&2, F32> -> @+n:Nat -> @+v:F32 -> @+acc:List<&2, F32> -> List<&2, F32>
A list with its nth element replaced, element by element.
def replace_at source · line 252 · raw
@+xs:List<&2, F32> -> @n:Nat -> @v:F32 -> List<&2, F32>
A list with its nth element replaced.
def one_hot_row source · line 256 · raw
@+label:Nat -> @k:Nat -> List<&2, F32>
One row of a one-hot matrix: a zero in every column but the label's.
def one_hot_rows_go source · line 260 · raw
@+labels:List<&2, Nat> -> @+k:Nat -> @+acc:List<&2, List<&2, F32>> -> List<&2, List<&2, F32>>
The rows of a one-hot matrix, label by label.
def one_hot_rows source · line 269 · raw
@+labels:List<&2, Nat> -> @+k:Nat -> List<&2, List<&2, F32>>
The rows of a one-hot matrix, one for every label.
def one_hot source · line 275 · raw
@+k:Nat -> @+n:Nat -> @labels:List<&2, Nat> -> Maybe<&2, 0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<n, k>>
The one-hot encoding: a row of k numbers for every label, with a one in the column of the label. It is Nothing when there are not exactly n labels.
def one_hot_count_go source · line 279 · raw
@+labels:List<&2, Nat> -> @+acc:Nat -> Nat
The number of rows of a one-hot matrix, counted.
def one_hot_count_of source · line 287 · raw
@ys:List<&2, Nat> -> Nat
The number of rows of a one-hot matrix.
def standardize source · line 303 · raw
@+r:Nat -> @+c:Nat -> @a:0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c> -> 0xf9837737d2c3f58ae1d0c5df42255584/main.Mat<r, c>