0

I need to create a tensor by calling some function fn over two other tensors and indices in a loop as follows:

tensor = [[fn(tensor1, tensor2, i, j) for i in range(3)] for j in range(4)]

Not sure how to approach this problem. Use tf.map_fn somehow?

1
  • Uncertainty about the function of your fn. You can also try tf.while_loop besides tf.map_fn. Commented Mar 26, 2019 at 1:43

1 Answer 1

1

So for your simple case your code will execute as it is.

import tensorflow as tf


sess = tf.Session()

a = tf.constant([1,2,3])
b = tf.constant([3,4,5,6])

def fn( tensor1, tensor2, i, j ):
   return tensor1[i] * tensor2[j]

tensor = [[fn(a, b, i, j) for i in range(3)] for j in range(4)]

init = tf.global_variables_initializer()
sess.run(init)
print (sess.run(tensor))

[[3, 6, 9], [4, 8, 12], [5, 10, 15], [6, 12, 18]]

Sign up to request clarification or add additional context in comments.

Comments

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.