Custom loss function based on external library

username: adm
cooked: <p>How to write a model / loss function where the loss is calculated in external tool.</p>
<p>I wrote a psedocode to illustrate what I want to achieve:</p>
<pre><code class="lang-auto">model = CustomModel().to(device) # Lets say 1000 inputs and 10 outputs

optimizer = optim.Adam(model.parameters(), lr=0.01)

inputs = next(iter(trainloader))

model_out = model(inputs) # 10 values that will be used to calculate loss

save_outs_to_file('model_out.txt', model_out)

proc = subprocess.Popen(["./calculate_loss", "-i", "model_out.txt"], stdout=subprocess.PIPE)
loss = proc.stdout.read()

# What to do next with this loss? 
# How to perform backprop?
# ... .backward()

optimizer.step()
</code></pre>

username: ptrblck
cooked: <p>Since you are leaving PyTorch, you would have to write a custom <code>autograd.Function</code>, as described <a href="https://pytorch.org/docs/stable/notes/extending.html">here</a>, and also implement the <code>backward</code> pass manually.<br>
Autograd won’t be able to track the operations in the other process.</p>

username: AladdinPerzon
cooked: <p>For what kind of applications would you need external tool to calculate the loss <a class="mention" href="/u/adm">@adm</a> ? Never encountered this kind of problem, just curious to know <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: adm
cooked: <p>Thank you for your help. As far as I understand I will not be able to solve this in such form as I have to provide it with forward and backward function (what means, I have to perform all operations the external tool is doing so the pytorch will be able to compute the gradient). The problem is that I do not know what exactly the external tool does.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="adm" data-post="4" data-topic="76166">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/adm/48/12127_2.png" class="avatar"> adm:</div>
<blockquote>
<p>The problem is that I do not know what exactly the external tool does.</p>
</blockquote>
</aside>
<p>Than I doubt it’ll be possible to use it inside your forward pass. <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=12" title=":confused:" class="emoji" alt=":confused:" loading="lazy" width="20" height="20"><br>
Would it be possible to use an alternative, “open”, approach using a different library, so that you could at least see, which operations are performed?</p>

username: adm
cooked: <p>Actually what came into my mind is that I can calculate the gradient numerically. I know the range of possible values and all are integers, so the epsilon will be 1. The question is how can I join it with the pytorch pipe? Let’s say, there is a model of X layers that outputs 10 parameters. Now I want to put this custom numerical loss on these 10 parameters.</p>

username: ptrblck
cooked: <p>You could define a custom <code>autograd.Function</code> and calculate the gradient in the <code>backward</code> method manually as described <a href="https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_custom_function.html">here</a>.</p>

username: Zhenxing-Dong
cooked: <p>Have you solved this problem?I am also faced  with this problem recently.If you have some ideas, i am happy you share with me.Thank you! <img src="https://discuss.pytorch.org/images/emoji/apple/grin.png?v=9" title=":grin:" class="emoji" alt=":grin:"></p>