Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将标签映射到tf.unique_和_counts()之后的计数_Python_Tensorflow - Fatal编程技术网

Python 将标签映射到tf.unique_和_counts()之后的计数

Python 将标签映射到tf.unique_和_counts()之后的计数,python,tensorflow,Python,Tensorflow,我有一个BxN输入特征张量特征标签,我调用 unique_标签,label_idc,label_counts=tf.unique_with_counts(特征标签) 获取标签计数 如何生成BxN矩阵,它存储标签计数而不是标签 一批的示例: Input: feature_labels: 0 1 1 1 0 2 3 1 3 > unique_labels, _, label_counts = tf.unique_with_counts(feature_labels) => uni

我有一个BxN输入特征张量
特征标签
,我调用

unique_标签,label_idc,label_counts=tf.unique_with_counts(特征标签)
获取标签计数

如何生成BxN矩阵,它存储标签计数而不是标签

一批的示例:

Input: 
feature_labels: 0 1 1 1 0 2 3 1 3

> unique_labels, _, label_counts = tf.unique_with_counts(feature_labels)

=> unique_labels: 0 1 2 3
=> label_counts: 2 4 1 2

Output:
2 4 4 4 2 1 2 4 2

如果我理解正确,我想你可以这样做:

import tensorflow as tf

with tf.Graph().as_default(), tf.Session() as sess:
    feature_labels = tf.constant([0, 1, 1, 1, 0, 2, 3, 1, 3], tf.int32)
    _, unique_idx, label_counts = tf.unique_with_counts(feature_labels)
    result = tf.gather(label_counts, unique_idx)
    print(sess.run(result))
    # [2 4 4 4 2 1 2 4 2]

请给出一个输入示例和您期望的输出,目前还不清楚您想要得到什么矩阵。@jdehesa添加了一个示例,使其能够在BxN矩阵上工作,我必须使用
tf.python.ops.gen\u array\u ops.unique\u with_counts\u v2(功能标签,axis=[-1])
,但除此之外,它还能工作,谢谢。