How to get Tensorflow tensor dimensions (shape) as int values?

后端 未结 6 1799
孤城傲影
孤城傲影 2020-12-12 20:23

Suppose I have a Tensorflow tensor. How do I get the dimensions (shape) of the tensor as integer values? I know there are two methods, tensor.get_shape() and

6条回答
  •  伪装坚强ぢ
    2020-12-12 20:46

    2.0 Compatible Answer: In Tensorflow 2.x (2.1), you can get the dimensions (shape) of the tensor as integer values, as shown in the Code below:

    Method 1 (using tf.shape):

    import tensorflow as tf
    c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
    Shape = c.shape.as_list()
    print(Shape)   # [2,3]
    

    Method 2 (using tf.get_shape()):

    import tensorflow as tf
    c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
    Shape = c.get_shape().as_list()
    print(Shape)   # [2,3]
    

提交回复
热议问题