Skip to content

Commit

Permalink
[Bugfix][Keras] axis of softmax
Browse files Browse the repository at this point in the history
  • Loading branch information
yongwww committed Aug 26, 2019
1 parent 70f0a16 commit 338e88e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion python/tvm/relay/frontend/keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,15 @@ def _convert_advanced_activation(inexpr, keras_layer, etab):
act_type = type(keras_layer).__name__

if act_type == 'Softmax':
return _op.nn.softmax(inexpr, axis=1)
axis = keras_layer.axis
dims = len(keras_layer.input_shape)
if isinstance(axis, list):
raise tvm.error.OpAttributeUnImplemented('Softmax with axes {} is not supported.'.format(axis))
if axis == -1:
axis = 1
else:
axis = axis + 1 if axis < dims - 1 else 1
return _op.nn.softmax(inexpr, axis=axis)
if act_type == 'ReLU':
if keras_layer.max_value:
return _op.clip(inexpr, a_min=0., a_max=float(keras_layer.max_value))
Expand Down
6 changes: 5 additions & 1 deletion tests/python/frontend/keras/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ def test_forward_merge():
def test_forward_activations():
data = keras.layers.Input(shape=(32, 32, 3))
act_funcs = [keras.layers.Activation('softmax'),
keras.layers.Softmax(),
keras.layers.Softmax(axis=-1),
keras.layers.Softmax(axis=1),
keras.layers.Softmax(axis=2),
keras.layers.Softmax(axis=3),
keras.layers.Activation('softplus'),
keras.layers.Activation('relu'),
keras.layers.Activation('softsign'),
Expand All @@ -103,7 +108,6 @@ def test_forward_activations():
keras.layers.Activation('tanh'),
keras.layers.Activation('linear'),
keras.layers.Activation('selu'),
keras.layers.Softmax(),
keras.layers.ReLU(),
keras.layers.ReLU(max_value=6.),
keras.layers.LeakyReLU(alpha=0.3),
Expand Down

0 comments on commit 338e88e

Please sign in to comment.