首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

【tensorflow】shape与ndim

获取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!
下一篇
举报
领券