experimental_experiment.gradient.loss_helper¶
- experimental_experiment.gradient.loss_helper.add_loss_output(onx: ModelProto, score_name: str = 'squared_error', loss_name: str = 'loss', label_name: str = 'label', weight_name: str | None = None, penalty: Dict[str, float] | None = None, output_index: int | None = None, **kwargs: Dict[str, Any] | None) ModelProto [source]¶
Modifies an ONNX graph to add operators to score and allow training.
- Parameters:
onx – onx graph
score_name – name of the score
loss_name – name of the output loss
label_name – name of the label input
weight_name – None or any value to consider weight while computing loss
penalty – dictionary similar to the following one { weight_name: {‘l1’: alpha, ‘l2’: beta} } or { weight_name: beta}, it adds a L1 and/or L2 penalty to one input or initializer, penalty =
output_index – the output used to compute the loss, if None, the function assumes there is only one output, it must be specified if there are more than 1, it can be an integer or a string (output name)
kwargs – additional arguments for losses (see below)
- Returns:
modified graph
Possible values for score_name:
‘squared_error’ or ‘l2’: or if weight_name is not None
‘absolute_error’ or ‘l1’: or if weight_name is not None
‘elastic’: mixture of losses, kwargs must define l1_weight and l2_weight, undefined, default value are 0.5
- ‘log’: log loss ,
this only works for a binary classification where yp is the predicted probability, yt is the expected probability. yt is expected to be binary, yp is a matrix with two columns, the sum on every line is 1.
Next example shows the loss with L1 and L2 loss.
Next example shows how to add a L2 loss with L1 and L2 penalties on the coefficients.
- experimental_experiment.gradient.loss_helper.get_train_initializer(onx: ModelProto)[source]¶
Returns the list of initializers to train.
- Returns:
dictionary {name: (value, tensor)}
The function walk through the list of initializers and returns all tensors with elements from types float or double.
- experimental_experiment.gradient.loss_helper.penalty_loss_onnx(name: str, dtype: Any, l1: float | None = None, l2: float | None = None, existing_names: List[str] | None = None)[source]¶
Returns onnx nodes to compute where and .
- Parameters:
name – name of weights
dtype – numpy dtype
l1 – coefficient for L1 norm
l2 – coefficient for L2 norm
existing_names – names already taken in the ONNX graph
- Returns:
initializer, nodes