Skip to content

Commit

Permalink
More fix (#68)
Browse files Browse the repository at this point in the history
* More fix

* Add decorator to pow
  • Loading branch information
kevinthesun authored and piiswrong committed Jul 3, 2017
1 parent 77cc790 commit e8e8136
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions keras/backend/mxnet_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
_MODEL = None
_REENTRY = False

placeholder_name_dict = dict()
set_image_dim_ordering('th')


Expand Down Expand Up @@ -419,6 +420,7 @@ def __gt__(self, other):
lhs=self.symbol,
rhs=other.symbol))

@keras_symbol_child
def __pow__(self, power, modulo=None):
return KerasSymbol(self.symbol.__pow__(power), neighbor=[self])

Expand Down Expand Up @@ -526,6 +528,13 @@ def placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None):
dtype = np.dtype(dtype)
if name is None:
name = _autogen_name('placeholder')
elif name in placeholder_name_dict:
placeholder_name_dict[name] += 1
name = name + '_' + str(placeholder_name_dict[name] - 1)
placeholder_name_dict[name] = 1 if name not in placeholder_name_dict \
else placeholder_name_dict[name] + 1
else:
placeholder_name_dict[name] = 1
if not shape:
if ndim:
shape = tuple([0 for _ in range(ndim)])
Expand Down Expand Up @@ -2516,10 +2525,14 @@ def sparse_categorical_crossentropy(output, target, from_logits=False):
"""Categorical crossentropy between an output tensor
and a target tensor, where the target is an integer tensor.
"""
output = mx.sym.softmax_cross_entropy(output.symbol, target.symbol)
assert not from_logits
target = KerasSymbol(mx.sym.one_hot(flatten(target).symbol, output.shape[1]))
axis = ndim(output) - 1
output = output.symbol
output = mx.sym.clip(output, a_min=_EPSILON, a_max=1. - _EPSILON)
output = - mx.sym.sum(target.symbol * mx.sym.log(output), axis=axis)
return KerasSymbol(output)


@keras_symbol_child
def binary_crossentropy(output, target, from_logits=False):
"""Binary crossentropy between an output tensor and a target tensor.
Expand Down
2 changes: 1 addition & 1 deletion keras/engine/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -1848,7 +1848,7 @@ def str2context(s):
self._test_sym = test_keras_symbol.symbol
self._ntest = len(self.metrics_tensors) + 1

pred_keras_symbol = K.group(self.outputs + state_updates)
pred_keras_symbol = K.group(self.outputs + [symbol for symbol in state_updates if symbol not in self.outputs])
bind_values.update(K.dfs_get_bind_values(pred_keras_symbol))
self._pred_sym = pred_keras_symbol.symbol
self._npred = len(self.outputs)
Expand Down

0 comments on commit e8e8136

Please sign in to comment.