I would like to partially reset a Variable, for a specific batch index.
At the moment I am using an in-place operation, which works “fine”.
def selective_zero(s, new):
    for b, reset in enumerate(new):
        if reset:
            for state_layer in s:
                state_layer.data[b].zero_()

selective_zero(state, y[t + 1] != y[t])

In order to complete this, I was thinking to register a hook in order to zero the gradient correspondingly as well.
I am now thinking whether returning a new state Variable multiplied with an appropriate mask, instead, would have the combined effect I am actually after. This, though, would involve a larger amount of computations. Hmm…
Something about this:
for state_layer in s:
    state_layer.data[b].zero_()
    state_layer.register_hook(lambda grad: grad[b].zero_())