在TensorFlow中,可以使用tf.reshape()
函数来改变张量的形状。以下是检查和改变张量形状的示例代码:
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)
在上面的示例中,我们首先创建了一个形状为(2, 3)的张量,并打印了它的形状。然后使用tf.reshape()
函数将该张量的形状改变为(3, 2),并打印了改变后张量的形状。