TensorFlow Reshape: Check & Modify Tensor Shape
In TensorFlow, you can use the tf.reshape() function to change the shape of a tensor. Below is an example code for checking and changing the shape of a tensor.
import tensorflow as tf
# 创建一个张量
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
# 打印张量的形状
print("原始张量的形状:", tensor.shape)
# 改变张量的形状为(3, 2)
reshaped_tensor = tf.reshape(tensor, (3, 2))
# 打印改变后张量的形状
print("改变后张量的形状:", reshaped_tensor.shape)
In the example above, we created a tensor of shape (2, 3) and printed its shape. Next, we used the tf.reshape() function to change the shape of the tensor to (3, 2) and printed the new shape of the tensor.