如何重塑张量并获得Tensorflow中的第一维?

How to reshape a tensor and obtain the first dimension in Tensorflow?

我有一个张量。假设它的维度是 [2, 999]。如何将其整形为 [999, 2] 并获得第一个维度,即 Tensorflow 中的 999?

整形:

new_tensor = tensor.reshape((999,2))

找到第一个维度:

first_dimension = tensor.shape[0]

您好,您可以这样做:

顺便说一句,张量对象没有重塑函数,您必须使用 tf.reshape 函数

调用它
import tensorflow as tf 
tensor = tf.range(999 * 2) # create a tensor
reshaped_tensor = tf.reshape(tensor, (2, 999))

# this doesn't work
tensor = tensor.reshape(999, 2)

你可以用转置命令切换轴,因为你有相同的维度

switched_axis = tf.transpose(tensor) # assuming tensor has a shape of 999, 2

打印第一个维度

print(tensor.shape[0])