title: Get Tensor shape at train time in Tensorflow
body: <p>How do you get the &quot;actual&quot; shape of a tensor at training time? e.g.,</p>
  <pre><code>(None, 64) -&gt; (128, 64)
  </code></pre>
  <p>In other words, at training time, I get a shape like <code>(None, 64)</code> where <code>None</code> means the first dimension of the tensor is dynamic w.r.t to the input size, and <code>64</code> is an example value for the second dimension.  I assume at training time, the &quot;actual&quot; size of that tensor is known to the framework, so I am wondering how/if I can get the actual size of the tensor, where <code>None</code> is evaluated to the train/test/eval dataset size. Hence, I am interested to get <code>(128, 64)</code> instead of <code>(None, 64)</code> where <code>128</code> is the size of the input.</p>
  <p>Please consider the following simplified code example.</p>
  <pre class="lang-py prettyprint-override"><code>class ALayer(tensorflow.keras.layers.Layer):
      def call(self, inputs):
          features = tf.matmul(inputs, self.kernel) + self.bias
          
          # These are the different approaches I've tried.
          print(features.shape)
          # This prints: (None, 64)
  
          print(tf.shape(features)
          # This prints: Tensor(&quot;ALayer/Shape:0&quot;, shape=(2,), dtype=int32)
          return features
  
  input_layer = layers.Input(input_dim)
  x = ALayer()([input_layer])
  x = layers.Dense(1)(x)
  model = keras.Model(inputs=[input_layer], outputs=[x])
  model.compile()
  
  train_dataset = tf.data.Dataset.from_tensor_slices((X_train, (y_train)))
  val_dataset = tf.data.Dataset.from_tensor_slices((X_val, (y_val)))
  model.fit(train_dataset, validation_data=val_dataset)
  </code></pre>