TensorFlow学习笔记(5)tf.rank, 求矩阵的维度

用例对应的源代码,觉得有帮助可以Star

import tensorflow as tf

#  tf.rank(
#      input,
#      name=None
#      )
#
#  Returns the rank of a tensor.

#  tf.rank returns the dimension of a tensor, not the number of elements. For
#  instance, the output from tf.rank called for the 2x2 matrix would be 2.

x = tf.constant([[1, 2, 4]]) # 2x2 matrix
x2 = tf.constant([[1, 2, 4], [8, 16, 32]]) # 2x2 matrix
x3 = tf.constant([1, 2, 4]) # 1x1 matrix

with tf.Session() as sess:
    print(sess.run(tf.rank(x))) # 2
    print(sess.run(tf.rank(x2))) # 2
    print(sess.run(tf.rank(x3))) # 1
    原文作者:谢昆明
    原文地址: https://www.jianshu.com/p/7356b1fe68f2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞