def get_hvp(v):
        """Get the Hessian-vector product of v and Hessian in loss_function.

    Args:
      v (vector): a (n * p,)-shaped vector we want to multiply with H.
    Returns:
      hvp (vector): a (n, p)-shaped matrix representing Hv.
    """
        v = tf.reshape(v, concat_vec.shape)
        v = tensor_utils.reshape_vector_as([el[0] for el in vec], v)
        v_hvp = calculate_influence.hvp(v,
                                        itr,
                                        loss_function,
                                        gradient_function,
                                        map_gradient_function,
                                        n_samples=approx_params['hvp_samples'])
        if approx_params['squared']:
            v_hvp = calculate_influence.hvp(
                v_hvp,
                itr,
                loss_function,
                gradient_function,
                map_gradient_function,
                n_samples=approx_params['hvp_samples'])
        return tensor_utils.flat_concat(v_hvp)
Exemplo n.º 2
0
  def hessian_vector_product(vec):
    """Takes the Hessian-vector product (HVP) of vec with model.

    Args:
      vec (vector): a (possibly batched) vector.
    Returns:
      flat_v_hvp: a (possibly batched) HVP of H(model) * vec.
    """
    weight_shaped_vector = tensor_utils.reshape_vector_as(model.weights, vec)

    v_hvp_total = 0.
    for _ in range(n_samples):
      v_hvp = ci.hvp(weight_shaped_vector, itr,
                     loss_fn, grad_fn, map_grad_fn)
      flat_v_hvp = tensor_utils.flat_concat(v_hvp)
      v_hvp_total += flat_v_hvp

    return v_hvp_total / float(n_samples)