There’s no way to do it directly, but here’s a relatively low effort way to do this (just use cosine similarity instead of distance):

  
    
    
    How to write custom loss function in terms of pairwise distances of outputs? autograd
  
  
    Hi there, I am trying to implement a loss function that depends on pairwise distances of output data.  Specifically I have neural network f which take X as input and the output is $Y = {y_i}_{i=1}^N$.  My error function has following form: 
$E = | d_{ij} - c_{ij} | ^2$ 
where d_{ij} is distance between y_i and y_j, and c_{ij} is the target value for this distance. 
I know how to write “vectorized” loss function like MSE, softmax which would take a complete vector to compute the loss.  But in my …