Dynamically split tensor based on batch size

I have a 1D tensor a that I want to lay / pack / alternate into a 2D tensor, for example y=[a, a, a] . If I knew how many times I wanted this to be repeated, I could use tf.tile with reshape .

But I am not because the size depends on the size of the party. A placeholder value of None , which is not valid. I know that for tf.slice you can enter -1 , and let the tensor stream figure it out, but I don’t see how the tensor stream can print the correct size. I have a tensor x that will be equal in shape to y , but I do not see the tile_like function.

Any suggestions?

+8
source share
2 answers

You can use tf.shape to determine the tensor runtime form and use it as the basis for the tf.tile argument:

 import tensorflow as tf import numpy as np x = tf.placeholder(tf.float32, shape=[None, 3]) y = tf.tile([2, 3], tf.shape(x)[0:1]) sess = tf.Session() print(sess.run(y, feed_dict={x: np.zeros([11, 3])})) 

I checked this code while working with a candidate for the release of Tensorflow 1.0. Hope this helps!

+14
source

Thanks @Peter Hawkins for a great answer. In many cases, an extra step of changing the shape is required to add batch_size as the first dimension. I added an optional extra step that changes the tensor:

 import tensorflow as tf import numpy as np batch_dependent_tensor = tf.placeholder(tf.float32, shape=[None, 3]) other_tensor = tf.constant([2, 3]) y = tf.tile(other_tensor, tf.shape(batch_dependent_tensor)[0:1]) # Reshaping the y tensor by adding the batch_size as the first dimension new_shape = tf.concat([tf.shape(batch_dependent_tensor)[0:1], tf.shape(other_tensor)[0:1]], axis=0) y_reshaped = tf.reshape(y, new_shape) sess = tf.Session() y_val, y_reshaped_val = sess.run([y, y_reshaped], feed_dict={batch_dependent_tensor: np.zeros([11, 3])}) print("y_val has shape %s, and value: %s" %(y_val.shape, y_val)) print("y_reshaped_val has shape %s, and value: %s" %(y_reshaped_val.shape, y_reshaped_val)) """ # print output: y_val has shape (22,), and value: [2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3] y_reshaped_val has shape (11, 2), and value: [[2 3] [2 3] [2 3] [2 3] [2 3] [2 3] [2 3] [2 3] [2 3] [2 3] [2 3]] """ 

Note that if other_tensor has a rank> 1 (is it a matrix or tensor with a larger dimension), then some changes to the code need to be made.

0
source

Source: https://habr.com/ru/post/1263544/


All Articles