-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from cpnota/release/0.2.0
Release/0.2.0
- Loading branch information
Showing
46 changed files
with
1,027 additions
and
692 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ all.egg-info | |
local | ||
legacy | ||
/runs | ||
/out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,54 @@ | ||
import torch | ||
from torch.nn import utils | ||
from all.layers import ListToList | ||
from all.environments import State | ||
from .features import Features | ||
|
||
class FeatureNetwork(Features): | ||
def __init__(self, model, optimizer, clip_grad=0): | ||
self.model = ListToList(model) | ||
self.model = model | ||
self.optimizer = optimizer | ||
self.clip_grad = clip_grad | ||
self._cache = [] | ||
self._out = [] | ||
|
||
def __call__(self, states): | ||
return self.model(states) | ||
features = self.model(states.features.float()) | ||
out = features.detach() | ||
out.requires_grad = True | ||
self._cache.append(features) | ||
self._out.append(out) | ||
return State( | ||
out, | ||
mask=states.mask, | ||
info=states.info | ||
) | ||
|
||
def eval(self, states): | ||
with torch.no_grad(): | ||
training = self.model.training | ||
result = self.model(states) | ||
result = self.model(states.features.float()) | ||
self.model.train(training) | ||
return result | ||
return State( | ||
result, | ||
mask=states.mask, | ||
info=states.info | ||
) | ||
|
||
def reinforce(self): | ||
# loss comes from elsewhere | ||
graphs, grads = self._decache() | ||
graphs.backward(grads) | ||
if self.clip_grad != 0: | ||
utils.clip_grad_norm_(self.model.parameters(), self.clip_grad) | ||
self.optimizer.step() | ||
self.optimizer.zero_grad() | ||
|
||
def _decache(self): | ||
graphs = [] | ||
grads = [] | ||
for graph, out in zip(self._cache, self._out): | ||
if out.grad is not None: | ||
graphs.append(graph) | ||
grads.append(out.grad) | ||
self._cache = [] | ||
self._out = [] | ||
return torch.cat(graphs), torch.cat(grads) |
Oops, something went wrong.