简体   繁体   English

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

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

I have a tensor.我有一个张量。 Lets say its dimension is [2, 999].假设它的维度是 [2, 999]。 How to reshape it to [999, 2] and obtain the first dimension, ie, 999 in Tensorflow?如何将其重塑为 [999, 2] 并获得第一个维度,即 Tensorflow 中的 999?

Reshape:重塑:

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

Find first dimension:查找第一个维度:

first_dimension = tensor.shape[0]

Hi here's how you can do it:您好,您可以这样做:

Btw tensor objects dont have a reshape function u have to call it as such with the tf.reshape function顺便说一句,张量对象没有重塑 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)

U can switch axis with the transpose command since u have the same dimensions您可以使用转置命令切换轴,因为您具有相同的尺寸

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

To print the first dimensions打印第一个尺寸

print(tensor.shape[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM