Callbacks in Pytorch

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?
Thanks a lot! :smiley:


Pytorch, contrary to Keras/ignite does not provide a builtin training loop. You will have to write it yourself (usually <10 lines) and so you will be able to call all the functions you want in there :slight_smile:

1 Like

Okay thanks. Do you have an example of what it would look like? Thank you.
I already have a training loop but I want to make it more generic

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"]):
    update_patience_weight(net, val_running_loss, best_val_loss, patience_rn, BEST_PATH)

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

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 !

So your training loop should just take a function as input that will be called at that line?
Sorry I think Iā€™m not understanding your question fully.

1 Like

Sorry if I am not clear English is not my first language :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! :slight_smile:


So in some sense you want to be able to specify in a json file a python function to be called later.
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.

1 Like

Okay thank you very much!