Callbacks in Pytorch

username: fgauthier
cooked: <p>I am new to Pytorch and I was wondering if they are any way of using callback like in keras. I know Ignite can do it but is there any other way using only Pytorch?<br>
Thanks a lot! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

username: albanD
cooked: <p>Hi,</p>
<p>Pytorch, contrary to Keras/ignite does not provide a builtin training loop. You will have to write it yourself (usually &lt;10 lines) and so you will be able to call all the functions you want in there <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: fgauthier
cooked: <p>Okay thanks. Do you have an example of what it would look like? Thank you.<br>
I already have a training loop but I want to make it more generic</p>
<pre><code class="lang-auto">for epoch in range(nb_epoch):
    train_running_loss = training_model(train_loader, net, optimizer, criterion, train_set)
    val_running_loss = eval_model(val_loader, net, criterion, val_set)
    #thats where I want to do the callbacks
    if early_stopping(patience_rn, experiment["training"]["patience"]):
            break
    update_patience_weight(net, val_running_loss, best_val_loss, patience_rn, BEST_PATH)




</code></pre>

username: albanD
cooked: <p>I would recommend you check the 60min blitz and in particular the section on neural nets there that will explain it better than I could <a href="https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html">https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html</a></p>

username: fgauthier
cooked: <p>Sorry I forgot to show you my code. I already have a training loop that work well. I wanted to add the callback in a generic way. The code is one answer before this one. Sorry !</p>

username: albanD
cooked: <p>So your training loop should just take a function as input that will be called at that line?<br>
Sorry I think I’m not understanding your question fully.</p>

username: fgauthier
cooked: <p>Sorry if I am not clear English is not my first language <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:">. I have a .json file that contains all the parameters of my experience (the net I use, the optimizer I use, the learning rate, etc.) I would like to be able to generate my callbacks from the .json file so I can decide which callbacks I want to do without having to change the program. I hope it is more clear. Thank you! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: albanD
cooked: <p>Ho.</p>
<p>So in some sense you want to be able to specify in a json file a python function to be called later.<br>
I think the simplest would be to have a file containing all the possible callbacks and the json file just contains the name of the one you want to use. Then in your function you can just call the appropriate function.</p>

username: fgauthier
cooked: <p>Okay thank you very much!</p>