Home > OS >  In Tensorflow 1.8.0, How to combine two placeholder into tuples in Tensorflow
In Tensorflow 1.8.0, How to combine two placeholder into tuples in Tensorflow

Time:03-10

I have two placeholder both with 10 values in it. Now I want to convert it to tuple.

In python, I know how to do it, for example:

a = [1, 2, 3]
b = [4, 5, 6]

c = list(zip(a, b)

Then c=[(1, 4), (2, 5), (3, 6)]

So, I have a and b in placeholder, and how can I get c?

Can't use eager execution and placeholder at the same time.

But the way, my Tensorflow version is 1.8.0

CodePudding user response:

Try using tf.data.Data.from_tensor_slices:

import tensorflow as tf
import numpy as np
tf.compat.v1.enable_eager_execution()

a = [1, 2, 3]
b = [4, 5, 6]

c = np.asarray(list(tf.data.Dataset.from_tensor_slices((a, b)).map(lambda x, y: (x, y))), dtype="i,i")
print(c)
[(1, 4) (2, 5) (3, 6)]

If you are working with placeholders, try:

import tensorflow as tf
import numpy as np

a = tf.compat.v1.placeholder(tf.int32)
b = tf.compat.v1.placeholder(tf.int32)

def f(a, b):
  return np.stack([a, b], axis=1)

c = tf.py_func(f, [a, b], tf.int32)

with tf.compat.v1.Session() as sess:
 c = sess.run(c, feed_dict={a: [1, 2, 3], b: [4, 5, 6]})
 c = [tuple(i) for i in c]
 print(c)
[(1, 4) (2, 5) (3, 6)]
  • Related