remove values from a tensor that are within a given range

  • Last Update :
  • Techknowledgy :

Use tf.boolean_mask to filter out values at indices that don't meet the required threshold.

# remove values from `X` in interval(lo, hi)
mask = tf.math.logical_or(tf.lesser(X, lo), tf.greater(X, hi))
X = tf.boolean_mask(X, mask)

In your case, you would define soft_acc as

def soft_acc(y_true, y_pred):
   mask = tf.math.logical_or(tf.greater(y_pred, 0.55), tf.lesser(y_pred, 0.45))
y_true2 = tf.boolean_mask(y_true, mask)
y_pred2 = tf.boolean_mask(y_pred, mask)

return K.mean(K.equal(K.round(y_true2), K.round(y_pred2)))

Suggestion : 2

Use tf.boolean_mask to filter out values anycodings_tensorflow at indices that don't meet the required anycodings_tensorflow threshold.,I tried using the soft_acc function on anycodings_tensorflow stackoverflow and adding an if else to the anycodings_tensorflow beginning to filter out the near 50/50s.,I want to be able to know the rounded anycodings_tensorflow accuracy of my neural network when the anycodings_tensorflow prediction is above or below a certain anycodings_tensorflow threshold. For example, I want it to only anycodings_tensorflow calculate accuracy when the prediction is anycodings_tensorflow above 0.55 or below 0.45 in order to filter anycodings_tensorflow out near 50/50 cases.,TypeError: Using a tf.Tensor as a Python anycodings_tensorflow bool is not allowed. Use if t is not None: anycodings_tensorflow instead of if t: to test if a tensor is anycodings_tensorflow defined, and use TensorFlow ops such as anycodings_tensorflow tf.cond to execute subgraphs conditioned on anycodings_tensorflow the value of a tensor.

I tried using the soft_acc function on anycodings_tensorflow stackoverflow and adding an if else to the anycodings_tensorflow beginning to filter out the near 50/50s.

def soft_acc(y_true, y_pred):
   if y_pred > 0.55 or y_pred < 0.45:
   return K.mean(K.equal(K.round(y_true), K.round(y_pred)))

Use tf.boolean_mask to filter out values anycodings_tensorflow at indices that don't meet the required anycodings_tensorflow threshold.

# remove values from `X` in interval(lo, hi)
mask = tf.math.logical_or(tf.lesser(X, lo), tf.greater(X, hi))
X = tf.boolean_mask(X, mask)

In your case, you would define soft_acc anycodings_tensorflow as

def soft_acc(y_true, y_pred):
   mask = tf.math.logical_or(tf.greater(y_pred, 0.55), tf.lesser(y_pred, 0.45))
y_true2 = tf.boolean_mask(y_true, mask)
y_pred2 = tf.boolean_mask(y_pred, mask)

return K.mean(K.equal(K.round(y_true2), K.round(y_pred2)))

Suggestion : 3

So I have a 1-d tensor T and an index i and need to remove i-th element from a tensor T, much like in pure python T.remove(i).,How about removing more than 1 elements by indices? Is there a faster way than looping ?,Thank you guys, this solves the problem. But there must be a way less clunky solution, I believe.,One possible way would be to index the tensor for all other values:

I’ve tried to do this:

i = 2
T = torch.tensor([1, 2, 3, 4, 5])
T = torch.cat([T[0: i], T[i + 1: -1]])

Update, this works

i = 2
T = torch.tensor([1, 2, 3, 4, 5])
T = torch.cat([T[0: i], T[i + 1: ]])

One possible way would be to index the tensor for all other values:

x = torch.arange(10)
value = 5
x = x[x != value]

I’m getting this error

Expected object of scalar type Float but got scalar type Long
for argument #2 'other'

For example

i = [0, 100, 2, 5, 10, 200, 500]
T = torch.randn(1000)

T_new = T[whose elements not equal to elements in i]

Hello, I think, A more elegant way will be to use a function like:

import torch as th

def th_delete(tensor, indices):
   mask = th.ones(tensor.numel(), dtype = th.bool)
mask[indices] = False
return tensor[mask]

It can be used like this:

>>> a = th.arange(5) >>>
   a
tensor([0, 1, 2, 3, 4]) >>>
   th_delete(a, [0, 3, 4])
tensor([1, 2]) >>>
   th_delete(a, [1])
tensor([0, 2, 3, 4]) >>>
   th_delete(a, th.tensor([1, 4]))
tensor([0, 2, 3])

And even like this:

>>> a = th.arange(5) >>>
   a
tensor([0, 1, 2, 3, 4]) >>>
   th_delete(a, 1)
tensor([0, 2, 3, 4])

Suggestion : 4

Last updated 2022-06-28 UTC.

View aliases

Compat aliases for migration

See Migration guide for more details.

tf.compat.v1.where_v2

tf.where(
   condition, x = None, y = None, name = None
)

Here condition is a 1-axis bool tensor with 2 True values. The result has a shape of [2,1]

tf.where([True, False, False, True]).numpy()
array([
   [0],
   [3]
])

Here condition is a 2-axis integer tensor, with 3 non-zero values. The result has a shape of [3, 2].

tf.where([
   [1, 0, 0],
   [1, 0, 1]
]).numpy()
array([
   [0, 0],
   [1, 0],
   [1, 2]
])

These indices are the same that tf.sparse.SparseTensor would use to represent the condition tensor:

sparse = tf.sparse.from_dense(float_tensor)
sparse.indices.numpy()
array([
   [0, 0, 0],
   [0, 1, 1],
   [0, 2, 0],
   [0, 2, 1],
   [1, 2, 0]
])

A complex number is considered non-zero if either the real or imaginary component is non-zero:

tf.where([complex(0.), complex(1.), 0 + 1 j, 1 + 1 j]).numpy()
array([
   [1],
   [2],
   [3]
])

Suggestion : 5

Last Updated : 18 Jul, 2021,GATE CS 2021 Syllabus

Syntax:

 index_add_(dim, index, ensor) -- - > Tensor

Output:

tensor([
   [1., 3., 5., 7., 9.],
   [0., 0., 0., 0., 0.],
   [1., 3., 5., 7., 9.],
   [0., 0., 0., 0., 0.],
   [1., 3., 5., 7., 9.]
])
34._
Output:
   tensor([
      [1., 1., 1., 1.],
      [1., 1., 1., 1.],
      [1., 1., 1., 1.],
      [1., 1., 1., 1.]
   ])