Vitor Bento
Vitor Bento

Reputation: 404

tensorflow 2.0 how create a new tensor geting value of another tensor given a condition?

I have 2 tensors:

h=[[0 0 0]
 [0 0 1]
 [0 1 0]]

and 

h2=[[0 0 0]
 [0 0 1]
 [0 0 1]]

I want to create 3 vector with values where h=h2 I mean ,i want compare h[0]=h2[0] ,h[1]=h2[1] and h[2]=h2[2]

So I want the 3 vector as:

h3=[[0 0 0]
 [0 0 1]

I tried :

def fn( tensor1,i):
    return tensor1[i]

tensor = [fn(h,i) for i in range(h) if tf.reduce_all(tf.equal(h[i],h2[i])) ] 

but it return this error TypeError: only integer scalar arrays can be converted to a scalar index

Upvotes: 0

Views: 105

Answers (1)

user11530462
user11530462

Reputation:

Working code snippet

import tensorflow as tf

h=[[0,0 ,0],
   [0, 0 ,1],
   [0, 1 ,0]]

h2=[[0 ,0 ,0],
    [0 ,0, 1],
    [0, 0, 1]]

for i in range(len(h)):
  if tf.reduce_all(tf.equal(h[i],h2[i])):
    print(h[i])

Output

[0, 0, 0]
[0, 0, 1]

Upvotes: 1

Related Questions