TensorFlow的get_shape()函数和num_elements() 函数

21 篇文章 1 订阅
15 篇文章 1 订阅

今天学习图像数组转置的时候,遇到get_shape()、num_elements()函数。

经查,get_shape()函数的功能和array的shape属性一样,都是可以得到张量(数组)的维度,如下是TesnorFlow的get_shape()源码(..\Lib\site-packages\tensorflow\python\ops\variables.py):

  def get_shape(self):
    """Alias of Variable.shape."""
    return self.shape

num_elements()函数返回数组的总元素个数,具体是通过数组各维度的乘积实现的,源码如下(..\Lib\site-packages\tensorflow\python\framework\tensor_shape.py):

  def num_elements(self):
    """Returns the total number of elements, or none for incomplete shapes."""
    if self.is_fully_defined():
      size = 1
      for dim in self._dims:
        size *= dim.value
      return size
    else:
      return None

好了,上代码,如下是做的一个验证:

import tensorflow as tf

a = tf.zeros([3, 4, 5, 6], tf.int32)
print(a)
print('----------')
b = a.get_shape()
e = a.shape
c = b[1:4].num_elements()
d = tf.reshape(a, [-1, c])

init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print(a.eval())
    print('----------')
    print(b)
    print('----------')
    print(e)
    print('----------')
    print(c)
    print('----------')
    print(d.eval())

结果:

'''
a为维度为(3, 4, 5, 6)的张量
'''
Tensor("zeros_11:0", shape=(3, 4, 5, 6), dtype=int32)
----------
'''
a = tf.zeros([3, 4, 5, 6], tf.int32)
维度为(3, 4, 5, 6)的全零张量
'''
[[[[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]

  [[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]

  [[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]

  [[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]]


 [[[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]

  [[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]

  [[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]

  [[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]]


 [[[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]

  [[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]

  [[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]

  [[0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]
   [0 0 0 0 0 0]]]]
----------
'''
b = a.get_shape()
返回a的维度
'''
(3, 4, 5, 6)
----------
'''
e = a.shape
类似于a.get_shape()
'''
(3, 4, 5, 6)
----------
'''
b[1:4]
对(3, 4, 5, 6)数组进行切片。可以取到索引1,不可以取到索引4;
.num_elements()
返回切片后的数组总元素数,4 * 5 * 6 = 120
'''
120
----------
'''
d = tf.reshape(a, [-1, c])
按照[-1, 120]的维度,改变a张量的形状。
a张量一共3 * 4 * 5 * 6 = 360个元素,所以这里的“-1”代表3,
重新生成的d的形状应该是(3, 120)
'''
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0]]

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值