Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set shape in placeholder for non-deterministic array size

Tags:

tensorflow

I want to train a data which varies array size. For example, Let's assume we wish to train sentences. The first sentence is "I am John" and The second one is "I know". If the sentences are converted into tensor. The first thing will become ["I", "am", "John"] and the next will be ["I","know"]. Consequently, the first array will need 3 as n_input for shape of placeholder. However, 2 is needed for the second array.

x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])

I will need above codes for definition of placeholder. However, I can't determine n_input.

Additionally, What does None in shape mean? batch_size? Please help me.

like image 393
hackartist Avatar asked Sep 21 '16 11:09

hackartist


2 Answers

Technically the placeholder doesn't need a shape at all. It can be defined as such.

x =tf.placeholder(tf.float32, shape=[])

In this case the place holder itself has no shape information to it. If you know the dimensions of the tensor but not it's actual numerical shape we replace the numerical value of that dimension with None because it can have a variable size.

 x =tf.placeholder(tf.float32, shape=[None, None, None])

This affects some down stream static shape analysis that tensorflow does to get the shape information but otherwise it should still work as intended.

like image 118
Steven Avatar answered Jun 22 '23 07:06

Steven


In scenarios like yours, we usually make the second dimension(the time_steps or the input length) as a constant number, say the largest length of the sentence, and the blanks are fed with pads or zeros directly. Then you can set the placeholders as:

x = tf.placeholder(tf.int32, [None, max_input_steps])
y = tf.placeholder(tf.int32, [None, nax_output_steps])
like image 23
Lerner Zhang Avatar answered Jun 22 '23 08:06

Lerner Zhang