either this.
Or, as far as I understand you want to call foo() on every iteration of every 20th epoch right?
So this:
for epoch in range(n_epochs):    #your epoch loop
    for iteration, data in enumerate(your_dataloader, 0):     #your iteration loop
        if((epoch+1)%20 == 0): 
            foo() #function gets called on every iteration of every 20th epoch

should work.
@pytorchnewbie Is this not the behavior you asked for?