Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have create_param() set the broadcast pattern #715

Merged
merged 2 commits into from
Jul 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lasagne/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ def test_create_param_retain_ndarray_dtype():
assert (result.dtype == param.dtype)


def test_create_param_broadcast_pattern():
from lasagne.utils import create_param
for shape in (10, 1, 20), (1, 2), (3, 1), (2, 3):
bcast = tuple(s == 1 for s in shape)
assert create_param(np.zeros, shape).broadcastable == bcast
assert create_param(np.zeros(shape, np.float32),
shape).broadcastable == bcast


def test_unroll_scan():
from lasagne.utils import unroll_scan
k = 2
Expand Down
10 changes: 6 additions & 4 deletions lasagne/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,21 @@ def create_param(spec, shape, name=None):
if spec.shape != shape:
raise ValueError("%s has shape %s, should be %s" %
(err_prefix % "numpy array", spec.shape, shape))
spec = theano.shared(spec)
# We assume parameter variables do not change shape after creation.
# We can thus fix their broadcast pattern, to allow Theano to infer
# broadcastable dimensions of expressions involving these parameters.
bcast = tuple(s == 1 for s in shape)
spec = theano.shared(spec, broadcastable=bcast)

if isinstance(spec, theano.Variable):
# We cannot check the shape here, Theano expressions (even shared
# variables) do not have a fixed compile-time shape. We can check the
# dimensionality though.
# Note that we cannot assign a name here. We could assign to the
# `name` attribute of the variable, but the user may have already
# named the variable and we don't want to override this.
if spec.ndim != len(shape):
raise ValueError("%s has %d dimensions, should be %d" %
(err_prefix % "Theano variable", spec.ndim,
len(shape)))
# We only assign a name if the user hasn't done so already.
if not spec.name:
spec.name = name
return spec
Expand Down