You can keep track of the step or iteration by using a member variable inside the Network module or the module containing the hook function.
For example, inside your Network module,
# network module
class Network(nn.Module):
def __init__(self):
self.step = -1
# while training
model = Network()
for i in range(steps):
# increment step var before forward pass
model.step += 1
output = model(input) # check self.step inside register_forward_hook
That’s an interesting approach!
Based on this, you could also create a “hook class” holding this internal parameter, which could be simpler than creating new modules: