Skip to content

Commit

Permalink
Shape inference for dot (keras-team#5675)
Browse files Browse the repository at this point in the history
* Shape inference for dot

* Remove dependency on numpy

* Check for scalar x (ndim=0)
  • Loading branch information
farizrahman4u authored and fchollet committed Mar 10, 2017
1 parent ea78473 commit 152d896
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions keras/backend/theano_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,21 @@ def moving_average_update(variable, value, momentum):


def dot(x, y):
# TODO: `keras_shape` inference.
if is_sparse(x):
return th_sparse_module.basic.structured_dot(x, y)
out = th_sparse_module.basic.structured_dot(x, y)
else:
return T.dot(x, y)
out = T.dot(x, y)
if hasattr(x, '_keras_shape') and hasattr(y, '_keras_shape'):
x_shape = list(x._keras_shape)
y_shape = list(y._keras_shape)
if len(x_shape) > 0:
x_shape.pop()
if len(y_shape) == 1:
y_shape.pop()
elif len(y_shape) > 1:
y_shape.pop(-2)
out._keras_shape = tuple(x_shape + y_shape)
return out


def batch_dot(x, y, axes=None):
Expand Down

0 comments on commit 152d896

Please sign in to comment.