Skip to content

Instantly share code, notes, and snippets.

@ixaxaar
Created November 17, 2017 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ixaxaar/02b65312eb1a840cc14e74d93b35178f to your computer and use it in GitHub Desktop.
Save ixaxaar/02b65312eb1a840cc14e74d93b35178f to your computer and use it in GitHub Desktop.
def repackage_hidden(h):
"""Wraps hidden states in new Variables, to detach them from their history."""
if type(h) == Variable:
return Variable(h.data)
elif type(h) == tuple:
return tuple(repackage_hidden(v) for v in h)
elif type(h) == list:
return [ repackage_hidden(v) for v in h ]
def repackage_hidden_dnc(h):
if h is None:
return None
if type(h) is list and h[0] is None:
return [None]
(chx, mhxs, _) = h[0]
chx = repackage_hidden(chx)
if type(mhxs) is list:
mhxs = [dict([(k, repackage_hidden(v)) for k, v in mhx.items()]) for mhx in mhxs]
else:
mhxs = dict([(k, repackage_hidden(v)) for k, v in mhxs.items()])
return [(chx, mhxs, None)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment