Build your own loss function in PyTorch

Ismail_Elezi: <p>Hi all! Started today using PyTorch and it seems to me more natural than Tensorflow. However, I would need to write a customized loss function. While it would be nice to be able to write any loss function, my loss function is a bit <a href="http://specific.So" rel="nofollow noopener">specific.So</a>, I am giving it (written on torch)</p>
<pre><code>X = np.asarray([[0.6946, 0.1328], [0.6563, 0.6873], [0.8184, 0.8047], [0.8177, 0.4517], 
                [0.1673, 0.2775], [0.6919, 0.0439], [0.4659, 0.3032], [0.3481, 0.1996]], dtype=np.float32)
X = torch.from_numpy(X)
y = np.asarray((1,3,2,2,3,1,2,3), dtype=np.float32)
y = torch.from_numpy(y)

def similarity(i, j):
    ''' This function defines the similarity between vectors i and j
    inputs: i, j - vectors of the same length
             sigma - the denumerator parameter
    output: sim - similarity value (real number from 0 to 1) '''
    
    dist = torch.norm(i - j)  
    return dist

def similarity_matrix(mat):
    ''' This function creates the similarity matrix of a dataset
    input: mat - dataset in matrix format
           sigma - a paramter which defines similarity
    output: simMatrix - the similarity matrix '''
    
    a = mat.size()
    a = a[0]
    simMatrix = torch.zeros((a,a))
    for i in xrange(a):
        for j in xrange(a):
            simMatrix[i][j] = similarity(mat[i], mat[j])            
    return simMatrix 

def convert_y(y):
    n = y.size()
    n = n[0]
    converted_y = torch.zeros((n, n))
    for i in xrange(n):
        for j in xrange(n):
            if y[i] == y[j]:
                converted_y[i, j] = 1
    return converted_y

def customized_loss(X, y):
    X_similarity = similarity_matrix(X)
    association = convert_y(y)
    loss_num = torch.sum(torch.mul(X_similarity, association))
    loss_all = torch.sum(X_similarity)
    loss_denum = loss_all - loss_num
    loss = loss_num/loss_denum
    return loss

loss = customized_loss(X, y)
print(loss)
</code></pre>
<p>Now, of course, considering that I am going to use it as the final layer, of the neural net, I would need to compute the gradients of it and then use them in the backpropagation.</p>
<p>Explaining the function a bit:</p>
<p>I first transform the input data space into a kind of similarity matrix (0 it means the data being the same, the higher the number in ij-th entry, the higher is the dissimilarity). Then in order to find the intra-cluster loss, I multiply this matrix with a 0/1 matrix, where the ij-th entry is 1 if the element i and j are in the same cluster, 0 otherwise. The intra-cluster loss is find similarity, and finally, we just divide the two losses.</p>
<p>My questions are:</p>
<ol>
<li>Can this be done in PyTorch, without writing Lua code?</li>
<li>Can the gradients of this be computed in an automatic way (torch autograd)?</li>
<li>Can such a loss function be given as input in optim.SGD? (optim.X in general case where X is the optimization algorithm)</li>
</ol>
<p>Thanks for any answer, or possible hint.</p>

apaszke: <ol>
<li>Yes, you don’t have to write any Lua code when you’re using PyTorch.</li>
<li>Yes, the gradients will be computed automatically, as long as you use <code>Variable</code>s all the time (without any <code>.data</code> unpacking or numpy conversions). It won’t work in your example, because you’re doing calculation on numpy arrays.</li>
<li>Optimizers don’t need to know anything about your loss - they only need you to call <code>.backward()</code> on the loss <code>Variable</code>, so that they can see the gradient. They only need a list of <code>Variable</code>s that you want to optimize.</li>
</ol>
<p>Since the code does a lot of operations, the graph recording just the loss function would be likely much larger than that of your model. Because of this, I’d recommend you to <a href="http://pytorch.org/docs/notes/extending.html#extending-torch-autograd">write your own autograd function</a>, or think a bit more about how can you compute your similarity matrix. If you’re operating in the Euclidean space, and you rewrite the formulas, it should be possible to batch some computation. As far as I see it could be decomposed into a Gramian matrix plus some norms added to the rows and columns.</p>

Ismail_Elezi: <p>Thanks a lot!</p>
<p>I rewrote everything using Torch, so now, it should work if I use loss.backward(X, y)?</p>
<p>Advises about writing my own autograd function and/or computing the similarity more efficiently are very welcome. It is definitely something that I need to do later, but for now I need just a simple version of this working.</p>
<p>Edit - It looks that it works by rewriting the final function as:</p>
<pre><code>def customized_loss(X, y):
    X_similarity = Variable(similarity_matrix(X), requires_grad = True)
    association = Variable(convert_y(y), requires_grad = True)
    temp = torch.mul(X_similarity, association)
    loss_num = torch.sum(torch.mul(X_similarity, association))
    loss_all = torch.sum(X_similarity)
    loss_denum = loss_all - loss_num
    loss = loss_num/loss_denum
    return loss
</code></pre>
<p>All is good for now, thanks again!</p>

apaszke: <p>No, this will not work. As I said, if you want your computation to be compatible with autograd, it needs to be executed on Variables from the start to the very end. You can’t unpack the tensors and repack them in the middle, because they won’t be connected to the initial graph, and will not forward the gradient. In your snippet no grad will be sent to <code>X</code> and <code>y</code>, because the backward will end on <code>X_silimarity</code> and <code>association</code> (they are graph leaves - their <code>.creator</code> is None).</p>

apaszke: <p>If you want to operate on raw tensors, and have them wrapped in Variables in a way that ensures connectivity, you have to write a new Function. Otherwise, you have to pass in Variables to your <code>similarity_matrix</code>, but it might be very slow like that.</p>

Ismail_Elezi: <p>I think that I got lost now. Rewriting an another time the function (in probably a more readable way):</p>
<pre><code>X = Variable(torch.Tensor([[0.6946, 0.1328], 
                           [0.6563, 0.6873], 
                           [0.8184, 0.8047], 
                           [0.8177, 0.4517], 
                           [0.1673, 0.2775], 
                           [0.6919, 0.0439],
                           [0.4659, 0.3032],
                           [0.3481, 0.1996]]))

y = Variable(torch.Tensor([1.0, 3.0, 2.0, 2.0, 3.0, 1.0, 2.0, 3.0]))


def customized_loss(X, y):
           
    def similarity_matrix(mat):
        a = mat.size()
        a = a[0]
        simMatrix = Variable(torch.zeros(a,a), requires_grad = True)
        for i in xrange(a):
            for j in xrange(a):
                simMatrix[i][j] = torch.norm(mat[i] - mat[j])         
        return simMatrix 
    
    def convert_y(y):
        a = y.size()
        a = a[0]
        converted_y = Variable(torch.zeros(a,a), requires_grad = True)
        for i in xrange(n):
            for j in xrange(n):
                if y[i] == y[j]:
                    converted_y[i, j] = 1
        return converted_y

    X_similarity = similarity_matrix(X)
    association = convert_y(y)
    loss_num = torch.sum(torch.mul(X_similarity, association))
    loss_all = torch.sum(X_similarity)
    loss_denum = loss_all - loss_num
    loss = loss_num/loss_denum
    return loss

loss = customized_loss(X, y)
</code></pre>
<p>As far as I can see, everything now is done in Variables (from beginning to the end). We are giving X and y (which are variables) to the function, and then everything is done in Variables. The only other variables that I need to define is simMatrix in the similarity_matrix function, and there I am having this error:</p>
<pre><code>RuntimeError: in-place operations can be only used on variables that don't share storage with any other variables, but detected that there are 2 objects sharing it.
</code></pre>
<p>Of course, the same thing happens in convert_y function when I create the converted_y Variable.</p>
<p>And I have no clue, what is going wrong, while googling this error doesn’t show any result.</p>
<p>…</p>
<p>You already spent some time here, so thanks for that, but in case you can guide me how to fix this problem (or writing it if it is a quick fix) it would be awesome. From the pyTorch tutorial about the Variables it is not clear to me what I am doing wrong (haven’t ever used Torch). I guess that the problem is that I am implicitly creating a new Variable in the middle of the graph, but is there any way around it?</p>
<ol>
<li>
<p>So, the problem is: If I define simMatrix as Variable we have this problem with sharing storage, if we don’t define it as variable (which wouldn’t make too much sense cause we want its gradients in the backprop) then we also have an error of ‘can’t assign a Variable to a scalar value of type float’ which makes perfect sense.</p>
</li>
<li>
<p>The other problem is that it seems that I cannot compare y[i] with y[j] in convert_y function. Because they are variables they are uncomparable, while if I use y[i].data (which likely makes problems during back-prop), strangely enough it makes a Runtime error saying that ‘bool value of non-empty torch.ByteTensor objects is ambiguous’.</p>
</li>
</ol>
<p>Is there a solution around this?</p>

fmassa: <p><a class="mention" href="/u/ismail_elezi">@Ismail_Elezi</a> As <a class="mention" href="/u/apaszke">@apaszke</a> said, you can compute the similarity matrix for the L2 distance using only matrix operations.<br>
Here is an implementation for your <code>similarity_matrix</code> using only matrix operations. It can run on the GPU and is going to be significantly faster than your previous implementation.</p>
<pre><code class="lang-python"># (x - y)^2 = x^2 - 2*x*y + y^2
def similarity_matrix(mat):
    # get the product x * y
    # here, y = x.t()
    r = torch.mm(mat, mat.t())
    # get the diagonal elements
    diag = r.diag().unsqueeze(0)
    diag = diag.expand_as(r)
    # compute the distance matrix
    D = diag + diag.t() - 2*r
    return D.sqrt()
</code></pre>
<p>If you are not backpropagating through <code>y</code>, no need to wrap it all in variables, just wrap the last result.</p>

Ismail_Elezi: <p><a class="mention" href="/u/fmassa">@fmassa</a> Thanks for your solution. I definitely need to refresh my linear algebra skills. It really solves the first part.</p>
<p>About, if you are not backpropagating through y part…I am a bit confused. Essentially, the algorithm is:</p>
<ol>
<li>Build a CNN that on the final layer has 2 neurons.</li>
<li>Transform the output of those 2 neurons (a tensor of shape n x 2) to similarity matrix. Call it X.</li>
<li>Transform the labels y into a n x n tensor (where ij-th element is 1 if i and j belong to the same cluster, 0 otherwise). Call it Y.</li>
<li>Do an elementwise multiplication of X and Y.</li>
<li>Do the extra stuff, sum, some substraction etc.</li>
</ol>
<p>While I do not need to backprop through y, Y is multiplied with X (and Y comes from y), so I think that I need to backprop through Y, right?</p>
<p>Cheers!</p>

fmassa: <p><a class="mention" href="/u/ismail_elezi">@Ismail_Elezi</a> because the targets are constants, you don’t need to compute the gradients through Y. You can say that your effective target is actually Y, and not y, and Y does not require gradient (<code>requires_grad=False</code>).<br>
So all you need to do is do the operations converting y in Y without using Variables, and then wrap the resulting Y in a variable.<br>
For reference, here is an implementation of <code>convert_y</code> that does not require a <code>for loop</code>, and can be efficiently performed.</p>
<pre><code class="lang-python">def convert_y2(y):
    s = y.size(0)
    y_expand = y.unsqueeze(0).expand(s, s)
    Y = y_expand.eq(y_expand.t())
    return Y
</code></pre>

Ismail_Elezi: <p><a class="mention" href="/u/fmassa">@fmassa</a></p>
<p>You’re absolutely right.</p>
<p>About your function, it returns a ByteTensor which means that Y cannot be multiplied with X (which is a FloatTensor). There should be something that allows casting a Tensor to some other type, right?</p>

fmassa: <p>Yes, you can cast the <code>ByteTensor</code> to any other type by using the following, which is <a href="http://pytorch.org/docs/tensors.html#torch-tensor">described in the documentation</a></p>
<pre><code class="lang-python">a = torch.ByteTensor([0,1,0])
b = a.float() # converts to float
c = a.type('torch.FloatTensor') # converts to float as well
</code></pre>
<p>Possible shortcuts for the conversion are the following:</p>
<ul>
<li><code>.byte()</code></li>
<li><code>.short()</code></li>
<li><code>.char()</code></li>
<li><code>.int()</code></li>
<li><code>.long()</code></li>
<li><code>.float()</code></li>
<li><code>.double</code></li>
<li>
<code>.half()</code> # for cuda only at the moment</li>
</ul>

Ismail_Elezi: <p>Excellent! Thanks a lot!</p>
<p>I think that I still need to fully understand how these functions work (read them in details), but everything is working now. Of course, the ANN isn’t working (a lot of NANs immediately after the first iteration), but that is something that I need to investigate and see the gradients’ values.</p>

fmassa: <p><a class="mention" href="/u/ismail_elezi">@Ismail_Elezi</a> Just to be sure, the first version of snippet that I sent you had some numerical instabilities that could lead to <code>nan</code> some times, but I then fixed it a couple of minutes later.</p>

Ismail_Elezi: <p><a class="mention" href="/u/fmassa">@fmassa</a></p>
<p>I am using the version that is written on this thread, and is giving NaNs after the first iteration. Typically, when I got Nans in the past it was either an error in differentiation or a large training rate. Both of these cannot be this time because it is automatic differentiation and I am using extremely small training rates (3e-7 in Adam, while typically I use 3e-4).</p>
<p>Anyway, I will have to read the documentation to find how to print the gradients, which might give me an idea on what is going wrong. And then, if I have problems, I will make an another thread.</p>
<p>Thanks for everything!</p>

apaszke: <p>You can use hooks e.g.:</p>
<pre><code class="lang-python">x = Variable(torch.randn(5, 5), requires_grad=True)
y = Variable(torch.randn(5, 5), requires_grad=True)
z = x + y
# this will work only in Python3
z.register_hook(lambda g: print(g)) 
# if you're using Python2 do this:
# def pring_grad(g):
#     print g
# z.register_hook(print_grad)
q = z.sum()
q.backward()
</code></pre>
<p>This will print the gradient w.r.t. <code>z</code> at each backward.</p>

Ismail_Elezi: <p>Excellent, I will try it and see what is going wrong.</p>

melgor: <p>Great talk about defining the own Loss function. I would love to have a simple example of creating own loss.<br>
I’m too confused about the idea of creating the loss function in torch. As author, I do not realy get what we really need to do or which function we can use if we want to define own loss function. It would be nice to have a complete example for “Similarity-Matrix” or sth like “Triplet-Loss”.</p>
<p>About NaNs is your results: it is related to Loss Function. I was implementing sth like that in TensorFlow and I get NaNs too. Based on experiments, it look like the gradient of diagonal in similarity matrix cause NaNs. I modified to skip diagonal (so take just right upper triangle matrix, as left down is just the same).</p>

edgarriba: <p><a class="mention" href="/u/melgor">@melgor</a> You have an example of Triplet loss here<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/edgarriba/examples/blob/master/triplet/main.py#L207-L221" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/edgarriba/examples/blob/master/triplet/main.py#L207-L221" target="_blank" rel="nofollow noopener">edgarriba/examples/blob/master/triplet/main.py#L207-L221</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="207" style="counter-reset: li-counter 206 ;">
<li>"""</li>
<li>def __init__(self):</li>
<li>    super(TNet, self).__init__()</li>
<li>    self.features = nn.Sequential(</li>
<li>        nn.Conv2d(1, 32, kernel_size=7),</li>
<li>        nn.Tanh(),</li>
<li>        nn.MaxPool2d(kernel_size=2, stride=2),</li>
<li>        nn.Conv2d(32, 64, kernel_size=6),</li>
<li>        nn.Tanh()</li>
<li>    )</li>
<li>    self.classifier = nn.Sequential(</li>
<li>        nn.Linear(64 * 8 * 8, 128),</li>
<li>        nn.Tanh()</li>
<li>    )</li>
<li>
</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>I hope in some days I’ll PR it to the main repository.<br>
Until now still testing and tuning parameters since I’m not getting the same performance as in LuaTorch.</p>

Ismail_Elezi: <p><a class="mention" href="/u/apaszke">@apaszke</a></p>
<p>In your example, it just prints a Tensor of shape 5x5 with all ones in it. If I use a working example (for example the tutorial on CIFAR-10 dataset: <a href="https://github.com/pytorch/tutorials/blob/master/Deep%20Learning%20with%20PyTorch.ipynb" rel="nofollow noopener">https://github.com/pytorch/tutorials/blob/master/Deep%20Learning%20with%20PyTorch.ipynb</a>) - doing a single iteration - and I write:</p>
<p><code>loss.register_hook(lambda g: print(g))</code></p>
<p>I get:</p>
<pre><code>Variable containing:
1
[torch.FloatTensor of size 1]

Variable containing:
1
[torch.FloatTensor of size 1]
</code></pre>
<p>which isn’t very helpful. Now, on my example, if I want all the gradients which are computed on my loss function, how can I use register_hook to do so?</p>

apaszke: <p>Having a tensor filled with ones is expected in my example, because that’s the gradient w.r.t. <code>z</code>. You can register the hook on any Variable of which gradient you want to inspect. If you want <em>all gradients of everything</em> you will need to register a hook on every intermediate output.</p>