gradient¶
gradient.grad_helper¶
DerivativeOptions¶
- class experimental_experiment.gradient.grad_helper.DerivativeOptions(value)[source]¶
Options defining how to build the onnx graph of the gradients.
Zero: default option, all options are disabled
KeepYieldOp: keeps the operator YieldOp in the graph, see @see fn onnx_derivative
KeepOutputs: keeps the output of the original graph
FillGrad: does not add any output to specify the gradient of the output but assumes it is one
Loss: the function assumes the loss was added to the graph
onnx_derivative¶
- experimental_experiment.gradient.grad_helper.onnx_derivative(onx: ModelProto, weights: List[str] | None = None, inputs: List[str] | None = None, options: DerivativeOptions = DerivativeOptions.Zero, loss: str | None = None, label: str | None = None, path_name: str | None = None, verbose: int = 0) ModelProto [source]¶
Builds the gradient for an onnx graph.
- Parameters:
onx – onnx graph
weights – gradient against those weights, None for all real weights
inputs – gradient against inputs, None for all real inputs
options – options of type @see cl DerivativeOptions
loss – loss output in case a loss was added in the graph, options must be equal to DerivativeOptions.Loss
label – if loss is specified, then the label must be specified as well
path_name – if options equal to DerivativeOptions.Loss, the gradient is saved to that path
verbose – verbosity
- Returns:
onnx graph
The function calls OrtModuleGraphBuilderConfiguration from onnxruntime-training. This graph is meant to be used with @see cl OrtGradientForwardBackward and includes operator YieldOp. That’s the graph looks this way:
These operators are the outputs of the initial graph and must be replaced by the gradient of these outputs to compute the gradient of the weights and the inputs. After they are replaced, it looks this way:
The user can still compute the outputs.
The input gradient can be filled with a constant matrix filled with one and with the expected shape.
gradient.loss_helper¶
add_loss_output¶
- 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.
get_train_initializer¶
- 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.
penalty_loss_onnx¶
- 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