Skip to content

Commit

Permalink
Merge pull request #974 from rizar/fix_application_serialization
Browse files Browse the repository at this point in the history
Fix Application pickling
  • Loading branch information
rizar committed Feb 6, 2016
2 parents 4ae4a9c + 0b36a54 commit 00281d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions blocks/bricks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ def copy_and_tag(variable, role, name):
return OrderedDict(zip(bound_application.outputs, outputs))
return unpack(outputs)

# Application instances are used instead of usual methods in bricks.
# The usual methods are not pickled per-se, similarly to classes
# and modules. Instead, a reference to the method is put into the pickle.
# Here, we ensure the same behaviour for Application instances.
def __reduce__(self):
return (getattr, (self.brick, self.application_name))


class BoundApplication(object):
"""An application method bound to a :class:`Brick` instance."""
Expand Down
5 changes: 5 additions & 0 deletions tests/bricks/test_bricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import theano
from numpy.testing import assert_allclose, assert_raises
from theano import tensor
from six.moves import cPickle

from blocks.bricks import (Identity, Linear, Maxout, LinearMaxout, MLP, Tanh,
Sequence, Random, Logistic, Softplus, Softmax)
Expand Down Expand Up @@ -249,6 +250,10 @@ def test_application():
assert TestBrick.delegated_apply.inputs == ['w']


def test_application_serialization():
assert id(cPickle.loads(cPickle.dumps(Linear.apply))) == id(Linear.apply)


def test_apply():
brick = TestBrick(0)
assert TestBrick.apply(brick, [0]) == [0, 1]
Expand Down

0 comments on commit 00281d0

Please sign in to comment.