def repackage_hidden(h):
"""Wraps hidden states in new Variables, to detach them from their history."""
if type(h) == Variable:
return Variable(h.data)
else:
return tuple(repackage_hidden(v) for v in h)
A general practice. You want to cut out the history of the outputs, so the memory ever gets freed. Otherwise there’s always going to be a reference to it. In the future it will be enough to call .detach() but not yet.