0

I am new to Tensoflow and I try to implement it in a custom RNN project to learn more about how Neural Networks work.

My problem is simple but I don't seem to find any satisfying answer.

I am used to Numpy and operations on arrays using condition masks but I don't find a way to transform this with Tensors

def ELu(in_array):
    in_array[in_array<= 0] = math.e ** in_array[in_array<= 0] - 1
    return in_array

>>>print(ELu(np.array([1.0,0.0,-1.0])))

Gives me

[ 1.          0.         -0.63212056]

And I would like to edit that function to be able to give me a similar Tensor if I do something like this

>>>print(ELu(tf.convert_to_tensor([1.0,0.0,-1.0])))

Which should give me

<tf.Tensor: shape=(3,), dtype=float32, numpy=array([ 1.,  0., -0.63212056], dtype=float32)>

but accessing a tensor using a similar way in_array[in_array<= 0] doesn't work

1 Answer 1

2

Use tensor_scatter_nd_update():

import math
def ELu(in_array):
  mask = in_array <= 0
  inds = tf.where(mask)
  updates = tf.boolean_mask(in_array, mask)
  updates = math.e ** updates - 1.
  res = tf.tensor_scatter_nd_update(in_array, inds, updates)
  return res
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that works perfectly. I'll start using those functions more often!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.