How to Use TensorFlow get_shape() Function

Recently, I was working on a neural network project for predicting housing prices in California, and I needed to verify the dimensions of my tensors at various stages of the model. That’s when I realized how important TensorFlow’s get_shape() function is for debugging and building robust machine learning models.

In this article, I’ll show you exactly how to use the get_shape() function in TensorFlow to check tensor dimensions. I’ll cover several methods with practical examples that you can immediately apply to your projects.

So let’s get started..!

The get_shape() Function

Python get_shape() function in TensorFlow is used to retrieve the shape of a tensor. Understanding tensor shapes is crucial when building and debugging neural networks, as incorrect dimensions often lead to errors.

Simply put, this function returns a TensorShape an object that describes the dimensions of your tensor.

Method 1: Use get_shape() With Basic Tensors

Let me show you how to use the get_shape() function in Python with a simple tensor example:

import tensorflow as tf

# Create a simple 2D tensor
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])

# Get the shape using get_shape()
shape = tensor.get_shape()

print("Tensor shape:", shape)
print("Shape as list:", shape.as_list())

When you run this code, you’ll get:

Tensor shape: (2, 3)
Shape as list: [2, 3]

I executed the above example code and added the screenshot below.

get shape of tensor

This tells us that our tensor has 2 rows and 3 columns.

Python’s as_list() method is particularly useful as it converts the TensorShape object to a Python list, which is often easier to work with in your code.

Method 2: Dynamic Shapes With get_shape()

Sometimes, when working with dynamic computational graphs, you might encounter tensors with partially unknown shapes:

import tensorflow as tf

# Create a placeholder with partially known shape
x = tf.keras.Input(shape=(None, 3))

# Get the shape information
shape = x.get_shape()

print("Tensor shape:", shape)
print("Is the shape fully defined?", shape.is_fully_defined())

Output:

Tensor shape: (None, None, 3)
Is the shape fully defined? False

I executed the above example code and added the screenshot below.

get tensor shape

The None values indicate dimensions that will be determined at runtime. This is common when working with batches of data or sequences of varying length.

Read Iterate Over Tensor In TensorFlow

Method 3: Compare get_shape() With tf.shape()

TensorFlow offers two primary ways to get tensor shapes: get_shape() and tf.shape(). Here’s how they differ:

import tensorflow as tf

# Create a tensor
tensor = tf.random.normal((3, 4, 5))

# Method 1: get_shape() - static shape, known at graph construction time
static_shape = tensor.get_shape()

# Method 2: tf.shape() - dynamic shape, evaluated at runtime
dynamic_shape = tf.shape(tensor)

print("Static shape (get_shape):", static_shape)
print("Dynamic shape (tf.shape):", dynamic_shape)

Output:

Static shape (get_shape): (3, 4, 5)
Dynamic shape (tf.shape): tf.Tensor([3 4 5], shape=(3,), dtype=int32)

I executed the above example code and added the screenshot below.

tensorflow shape

The key difference:

  • get_shape() returns the static shape known at graph construction time
  • tf.shape() returns a tensor that will be evaluated at runtime

For most standard cases with fixed dimensions, both will give the same result. However, tf.shape() is more appropriate when working with dynamic shapes.

Check out Convert Tensor to Numpy in TensorFlow

Work with get_shape() in Neural Networks

When building neural networks, checking tensor shapes helps prevent dimension errors. Here’s a practical example with a simple CNN for image classification:

import tensorflow as tf
from tensorflow.keras import layers

# Create a simple CNN model for MNIST classification
model = tf.keras.Sequential()
model.add(layers.Input(shape=(28, 28, 1)))  # MNIST images are 28x28 pixels
model.add(layers.Conv2D(32, kernel_size=(3, 3), activation="relu"))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Conv2D(64, kernel_size=(3, 3), activation="relu"))
model.add(layers.Flatten())
model.add(layers.Dense(10, activation="softmax"))

# Let's check the shape at each layer
for i, layer in enumerate(model.layers):
    print(f"Layer {i}: {layer.__class__.__name__}, Output Shape: {layer.output.get_shape()}")

This will print the shape of each layer’s output, helping you ensure your network architecture is correctly configured.

Read TensorFlow One_Hot Encoding

Method 4: Use get_shape() with TensorFlow 2.x

In TensorFlow 2.x, while get_shape() still works, the preferred way to get the shape information is to use the shape attribute or tf.shape(). However, get_shape() is still useful in certain contexts:

import tensorflow as tf

# Create a tensor
tensor = tf.random.normal((2, 3, 4))

# TF 2.x preferred way
shape_attribute = tensor.shape
print("Shape attribute:", shape_attribute)

# get_shape() method still works
shape_method = tensor.get_shape()
print("get_shape() method:", shape_method)

# Are they the same?
print("Are they equal?", shape_attribute == shape_method)

Output:

Shape attribute: (2, 3, 4)
get_shape() method: (2, 3, 4)
Are they equal? True

As you can see, in modern TensorFlow, both approaches give the same result, but using the .shape attribute is generally more concise.

Read Tensorflow Activation Functions

Practical Example: Debug Model Dimensions

Let me share a real-world scenario where I used get_shape() to debug a model that was giving dimension errors.

I was working on a time series forecasting model for stock prices using LSTM, and I kept getting shape mismatch errors:

import tensorflow as tf
from tensorflow.keras.layers import LSTM, Dense, Input
from tensorflow.keras.models import Model

# Define input
sequence_length = 10
features = 5
batch_size = 32

# Create input tensor
inputs = Input(batch_shape=(batch_size, sequence_length, features))
print("Input shape:", inputs.get_shape())

# Add LSTM layer
x = LSTM(64, return_sequences=True)(inputs)
print("After LSTM shape:", x.get_shape())

# Add Dense layer
outputs = Dense(1)(x)
print("Output shape:", outputs.get_shape())

# Build model
model = Model(inputs=inputs, outputs=outputs)

This prints:

Input shape: (32, 10, 5)
After LSTM shape: (32, 10, 64)
Output shape: (32, 10, 1)

By inspecting the shapes at each step, I could see that the LSTM layer was returning sequences of shape (32, 10, 64) and the Dense layer was being applied to each time step, resulting in a final shape of (32, 10, 1).

This helped me understand why my model wasn’t working as expected – I needed to reshape the output for my specific task.

Check out Tensorflow Gradient Descent in Neural Network

Common Issues When Using get_shape()

When working with get_shape(), I’ve encountered a few common issues:

  1. Confusion with static vs. dynamic shapes: get_shape() returns the static shape, which might not be fully defined at graph construction time.
  2. Deprecated warnings in newer TensorFlow versions: In newer TensorFlow versions, you might see warnings suggesting to use the .shape attribute instead.
  3. Handling None dimensions: When a dimension is None, you need to be careful with shape-dependent operations.

Here’s how to handle the last issue:

import tensorflow as tf

# Create a tensor with a None dimension (batch size)
x = tf.keras.Input(shape=(10,))

# Check the shape
shape = x.get_shape().as_list()
print("Shape with None:", shape)  # [None, 10]

# Handling None dimensions
if shape[0] is None:
    print("First dimension is None (probably batch size)")

# For operations requiring specific dimensions, you can use tf.shape
dynamic_batch_size = tf.shape(x)[0]
print("Dynamic dimension will be evaluated at runtime")

The get_shape() function in TensorFlow is a valuable tool for understanding and debugging tensor dimensions in your machine learning models.

While in TensorFlow 2.x, the .shape attribute is often preferred for simplicity, get_shape() still has its place, especially when working with legacy code or when you need to extract shape information as a list using the .as_list() method.

By understanding how to properly use get_shape() and alternatives like tf.shape() you can build more robust models and avoid frustrating dimension errors that often plague deep learning projects.

Other Python TensorFlow tutorials you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.