[英]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]
您好,您可以这样做:
顺便说一句,张量对象没有重塑 function 你必须使用 tf.reshape function 来调用它
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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.