简体   繁体   中英

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

I have a tensor. Lets say its dimension is [2, 999]. How to reshape it to [999, 2] and obtain the first dimension, ie, 999 in Tensorflow?

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

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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