【问题标题】:How to count in a one hot tensor如何计算一个热张量
【发布时间】:2019-02-06 15:43:43
【问题描述】:

我知道我可以使用以下命令将张量转换为 one-hot:

one_hot_labels = tf.one_hot(labels,depth=3)

现在我想计算one_hot_labels 中有多少 0 类、1 类和 2 类。最简单的计算方法是什么?

例子:

输入:

one_hot_labels = [[1,0,0],[1,0,0],[0,0,1]]
one_hot_labels.count([1,0,0]) # something like this command

输出:

2

【问题讨论】:

    标签: python tensorflow one-hot-encoding


    【解决方案1】:

    这样的东西应该适合你:

    one_hot_labels = np.array([[1,0,0],[1,0,0],[0,0,1]])
    count_label = tf.reduce_sum(one_hot_labels, axis=0)
    sess = tf.Session()
    sess.run(count_label)
    # array([2, 0, 1])
    

    现在例如你可以这样做:

    count_label = tf.reduce_sum(one_hot_labels, axis=0)[0]
    # 2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-20
      • 2022-01-27
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 2017-01-19
      • 2019-10-24
      • 2018-11-04
      相关资源
      最近更新 更多