question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Boolean indexing with keras

See original GitHub issue

Hi everyone,

Is it possible to use boolean indexing in Keras (with TF backend) ?

Here is a little script to see what I want to do, with the errors I get:

import numpy as np
import keras
from keras import backend as K

print(keras.__version__,K.tensorflow_backend.tf.__version__)
# 1.2.0 0.11.0rc2

m = np.random.randn(10,10)
b = np.random.uniform(size=(10,10))
M = K.variable(m)
B = K.variable(B)
boolean_mask = K.greater(B,0.5)

# This works fine, in numpy
np_bool = M.eval(session=K.get_session())[boolean_mask.eval(session=K.get_session())] 

# But this does not
keras_bool = M[boolean_mask]

I am looking for something that is similar to tf.boolean_mask.

Thank you in advance !

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

6reactions
DSLituievcommented, Jan 2, 2018

this is a very desirable feature to have when writing custom loss functions. The solution by @MarkSeygan might works if loss then aggregated by sum, but breaks if the aggregation is performed with mean.

0reactions
chrishmorriscommented, Feb 13, 2020

See tf.boolean_mask

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tensorflow indexing with boolean tensor - Stack Overflow
Is there a way to index with a boolean tensor right now or is that currently unsupported? python · indexing · tensorflow ·...
Read more >
Pandas Boolean indexing - Javatpoint
Boolean indexing is defined as a very important feature of numpy, which is frequently used in pandas. Its main task is to use...
Read more >
How Boolean Indexing works in Pandas. - Life With Data
Boolean Indexing on a DataFrame with single condition –. Let's read the data to work with. import pandas as pd import numpy as...
Read more >
tf.boolean_mask | TensorFlow v2.11.0
K-D boolean Tensor, K <= N and K must be known statically. A 0-D int Tensor representing the axis in tensor to mask...
Read more >
NumPy Boolean Indexing - Finxter
You can index specific values from a NumPy array using another NumPy array of Boolean values on one axis to specify the indices...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found