In TensorFlow 1.x, changing the shape of an operation's output tensor within the computation graph ("in the graph") depends on what exactly you want to do—whether you want to reshape a tensor, change a variable's shape, or define a custom op with a mutable output shape.
Here are the main approaches and considerations:
1. Reshaping a Tensor in the Graph
If you want to change the shape of a tensor output by an operation without changing the data, you typically use tf.reshape. This creates a new tensor with the same data but a different shape, as long as the total number of elements remains the same.
Example:
import tensorflow as tf
# Original tensor of shape [2, 3, 4]
t1 = tf.constant(range(24), shape=[2, 3, 4])
# Reshape to [2, 12]
t2 = tf.reshape(t1, [2, 12])
# Reshape to [2, 3, 2, 2]
t3 = tf.reshape(t1, [2, 3, 2, 2])
This does not modify the original tensor but returns a new tensor with the desired shape. The reshape operation is efficient because it reuses the underlying data buffer[3][4][5].
2. Changing the Shape of a Variable
TensorFlow variables have a fixed shape once created, but there are some workarounds:
-
When creating a variable, you can specify validate_shape=False to allow assignment of values with different shapes later.
-
You can assign a new value with a different shape using assign with validate_shape=False:
var = tf.Variable(initial_value=[[1, 2], [3, 4]], validate_shape=False)
# Later assign a new value with a different shape
assign_op = var.assign([[1, 2, 3], [4, 5, 6]], validate_shape=False)
-
However, the variable's .shape attribute may not update accordingly, which can cause issues when saving/loading checkpoints[1].
-
In TensorFlow 1.14+, you can create variables with partially unknown shapes using shape=tf.TensorShape(None) to allow flexible shapes on assignment[1].
3. Setting or Changing the Static Shape of a Tensor
Sometimes you want to inform TensorFlow about the static shape of a tensor (for shape inference or graph optimizations) without changing the underlying data. You can use tensor.set_shape(new_shape) to update the static shape information.
Note:
-
set_shape() only updates the static shape metadata in Python; it does not change the tensor's data or runtime shape.
-
The new shape must be compatible with the existing shape (e.g., you cannot change a vector to a 3D tensor directly)[2].
-
For actual shape changes, use tf.reshape.
4. Custom Ops and Shape Functions
If you are creating a custom TensorFlow op (in C++), you define the output shape during op registration using a shape function (SetShapeFn). This shape function can specify fixed or dynamic output shapes based on input shapes.
-
If the output shape cannot be fixed, you can set it as unknown (UnknownShape), but this may cause issues downstream if other ops expect specific shapes.
-
A common workaround is to use tf.reshape after your custom op to enforce a specific shape[6][8].
Summary Table
| Scenario | Method/Function | Notes |
|---|
| Reshape tensor output | tf.reshape(tensor, new_shape) | Creates new tensor with same data, different shape; total elements must match |
| Change variable shape at runtime | var.assign(new_value, validate_shape=False) | Allows assigning new shape; variable's .shape may not update properly |
| Update static shape info | tensor.set_shape(new_shape) | Updates static shape metadata only; shape must be compatible |
| Custom op output shape | Define shape function in REGISTER_OP | Use shape inference API; fallback to unknown shape if necessary; reshape after op if needed |
Practical Example for Reshaping in TensorFlow 1.x
import tensorflow as tf
# Create a constant tensor of shape [2, 3, 4]
t1 = tf.constant(range(24), shape=[2, 3, 4])
# Reshape to [2, 12]
t2 = tf.reshape(t1, [2, 12])
with tf.Session() as sess:
print(sess.run(t2))
This will output a tensor reshaped to 2 rows and 12 columns, preserving all elements[4][5].
In conclusion, to change the shape of an op's output tensor in TensorFlow 1.x graph, use tf.reshape for tensors, and for variables, use assign with validate_shape=False if you need to change the shape dynamically. For custom ops, define or adjust output shapes via shape functions or reshape outputs afterward. Static shape metadata can be updated with set_shape() but does not change the actual tensor shape.