deepdow.explain module

Collection of tools for explaining trained models.

gradient_wrt_input(model, target_weights, initial_guess, n_iter=100, mask=None, lr=0.1, verbose=True, device=None, dtype=None)[source]

Find input tensor such that the model produces an allocation close to the target one.

Parameters
  • model (torch.Module) – Network that predicts weight allocation given feature tensor.

  • target_weights (torch.Tensor) – Vector of targeted asset weights of shape (n_assets,).

  • initial_guess (torch.Tensor) – Initial feature tensor serving as the starting point for the optimization. The shape is (n_channels, lookback, n_assets) - the sample dimension is not included.

  • n_iter (int) – Number of iterations of the gradients descent (or other) algorithm.

  • mask (None or torch.Tensor) – If specified, then boolean torch.Tensor of the same shape as initial_guess than one can elementwise choose what parts of the inputs to optimize (True) and which keep the same as the initial guess (False).

  • lr (float) – Learning rate for the optimizer.

  • verbose (bool) – If True, then verbosity activated.

  • dtype (None or torch.dtype) – Dtype to be used. If specified, casts all used tensors.

  • device (None or torch.device) – Device to be used. If specified, casts all used tensors.

Returns

  • result (torch.Tensor) – Feature tensor of the same shape as initial_guess that is mapped by the network (hopefully) close to target_weights.

  • hist (list) – List of losses per iteration.