Skip to content

Commit

Permalink
Can take subview of container using []-syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasb-eyer committed Aug 31, 2015
1 parent fcabd88 commit 15d0fbf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions DeepFried2/containers/Container.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from ..layers import Module

from DeepFried2.utils import aslist as _aslist


class Container(Module):

Expand Down Expand Up @@ -38,3 +40,6 @@ def get_stat_updates(self):
def add(self, *modules):
assert all(isinstance(m, Module) for m in modules), "`Container`s can only contain objects subtyping `Module`."
self.modules += modules

def __getitem__(self, slice_):
return type(self)(*_aslist(self.modules[slice_]))
9 changes: 9 additions & 0 deletions DeepFried2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ def load_params(module, fromwhere):
with _np.load(fromwhere) as f:
for p, v in zip(params, f['params']):
p.set_value(v)


def aslist(what):
if isinstance(what, list):
return what
elif isinstance(what, tuple):
return list(what)
else:
return [what]

0 comments on commit 15d0fbf

Please sign in to comment.