Skip to content

Commit

Permalink
Added the numpy implementation of switch to the docs. (keras-team#11575)
Browse files Browse the repository at this point in the history
### Summary

I used `np.newaxis` for users who may not be familiar with the `None` indexing.

### Related Issues

### PR Overview

- [ ] This PR requires new unit tests [y/n] (make sure tests are included)
- [ ] This PR requires to update the documentation [y/n] (make sure the docs are up-to-date)
- [x] This PR is backwards compatible [y/n]
- [ ] This PR changes the current API [y/n] (all API changes need to be approved by fchollet)
  • Loading branch information
gabrieldemarmiesse authored and Frédéric Branchaud-Charron committed Jan 14, 2019
1 parent 8bb7e57 commit a96d870
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion keras/backend/numpy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def relu(x, alpha=0., max_value=None, threshold=0.):
def switch(condition, then_expression, else_expression):
cond_float = condition.astype(floatx())
while cond_float.ndim < then_expression.ndim:
cond_float = cond_float[..., None]
cond_float = cond_float[..., np.newaxis]
return cond_float * then_expression + (1 - cond_float) * else_expression


Expand Down
2 changes: 2 additions & 0 deletions keras/backend/tensorflow_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3227,6 +3227,8 @@ def switch(condition, then_expression, else_expression):
# Raises
ValueError: If rank of `condition` is greater than rank of expressions.
{{np_implementation}}
"""
if condition.dtype != tf.bool:
condition = tf.cast(condition, 'bool')
Expand Down

0 comments on commit a96d870

Please sign in to comment.