【tensorflow】shape与ndim

2020-03-31 11:58:21 浏览数 (1)

获取shape

代码语言:javascript复制
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:

代码语言:javascript复制
=========== get shape ============
tensor                   :  Tensor("Placeholder:0", shape=(200, 200, 3), dtype=float32)
tensor.shape             :  (200, 200, 3)
tensor.get_shape()       :  (200, 200, 3)

获取ndim

代码语言:javascript复制
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:

代码语言:javascript复制
=========== 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!

0 人点赞