TensorFlow Shape
Static and dynamic:
The most difficult things are arises when we dive deep into the peculiarities of Tensorflow and find there is no constraint about the shape of a tensor. Infact, Tensorflow allows to represent the shape into three different ways.
– Fully-known shape.
– Partially-known shape.
– Unknown shape and known rank.
– Unknown shape and rank.
Here tf.reshape() allows us to the tensors converted into different shapes.
1. Fully-known shape: in this we know the rank and the size for each dimension that are exactly the examples described above.
2. Partially-known shape: in this, we know the rank, but we don’t have the specific sixe which means we have an unknown size for one or more dimension (everyone that has trained a model in batch is aware of this, when we define the input we just specify the feature vector shape, letting the batch dimension set to None, e.x.: (None, 28, 28, 1).
3. Unknown shape and known rank: in this rank of the tensor is Known, but we don’t know any of the dimension value, ex: (None, None, None).
4. Unknown shape and rank: compare to others this is the toughest case, in which we know nothing about the tensor; the rank nor the value of any dimension.
A Static Shape, that’s known at graph dynamic shape definition time and that will be known only at graph execution time.
A placeholder is also defined in this way i.e,
inputs_ = tf.placeholder(tf.float32, shape=(None, None, None, None))
It has an Known rank i,e=4 and unknown shape at graph definition time only.
In order to check if the depth of the input is in the accepted value,we have to use tf.shape
The difference between the tf.shape function and the .shape attribute is most important:
1. tf.shape(inputs_) : It returns only a 1-D integer tensor representing the dynamic shapeof inputs_.
2. inputs_.shape : returns a tuple [immutable]representing the static shape of inputs_.