【tensorflow】shape與ndim

獲取shape

import tensorflow as tf

tensor = tf.placeholder(dtype=tf.float32, shape=[200, 200, 3])

print('\n=========== get shape ============')
print('tensor                   : ', tensor)
print('tensor.shape             : ', tensor.shape)
print('tensor.get_shape()       : ', tensor.get_shape())

Print:

=========== get shape ============
tensor                   :  Tensor("Placeholder:0", shape=(200, 200, 3), dtype=float32)
tensor.shape             :  (200, 200, 3)
tensor.get_shape()       :  (200, 200, 3)

獲取ndim

import tensorflow as tf

tensor = tf.placeholder(dtype=tf.float32, shape=[200, 200, 3])

print('\n=========== get dims ============')
print('tf.shape(tensor)         : ', tf.shape(tensor))
print('tensor.shape.ndims       : ', tensor.shape.ndims)
print('tensor.get_shape().ndims : ', tensor.get_shape().ndims)

Print:

=========== get dims ============
tf.shape(tensor)         :  Tensor("Shape:0", shape=(3,), dtype=int32)
tensor.shape.ndims       :  3
tensor.get_shape().ndims :  3

補充

  • 需要特別注意的是,tf.shape(tensor)返回的其實是ndim,而非標準意義上的shape!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章