Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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 在tensorflow中沿给定张量的轴计算模式和模式计数_Python_Tensorflow - Fatal编程技术网

Python 在tensorflow中沿给定张量的轴计算模式和模式计数

Python 在tensorflow中沿给定张量的轴计算模式和模式计数,python,tensorflow,Python,Tensorflow,我有一个二维阵列,如下所示: c=np.array([[1, 3, 0, 0, 3], [1, 3, 1, 0, 2], [1, 3, 1, 2, 2]]) 我想沿轴计算模式和模式计数=0。 因此,结果应如下所示: mode = [1,3,1,0,2], mode-count=[3,3,2,2,2] 我已经在TensorFlow网站上搜索过了,但是除了 ,它需要一个一维张量 我不想在数组c的每一列上运行循环,以便使用tf.unique\u wi

我有一个二维阵列,如下所示:

c=np.array([[1, 3, 0, 0, 3],
            [1, 3, 1, 0, 2],
            [1, 3, 1, 2, 2]])
我想沿轴计算模式和模式计数=0。
因此,结果应如下所示:

mode = [1,3,1,0,2], mode-count=[3,3,2,2,2]
我已经在TensorFlow网站上搜索过了,但是除了 ,它需要一个一维张量

我不想在数组
c
的每一列上运行循环,以便使用
tf.unique\u with_counts
来计算模式和模式计数。
欢迎提供任何示例建议。

有一个功能,可以使这一点非常简单:

将tensorflow导入为tf
导入tensorflow_概率作为tfp
def模式_和_计数(x,轴=-1):
x=tf。将_转换为_张量(x)
dt=x.dtype
#如果输入值为负值,则将其移位
m=tf.math.reduce_min(x)
x2=x-m
#minlength不应该是必需的,但如果没有它,可能会失败
#(此处报道)https://github.com/tensorflow/probability/issues/962)
c=tfp.stats.count_整数(x2,axis=axis,dtype=dt,
minlength=tf.math.reduce_max(x2)+1)
#查找计数最大的值
idx=tf.math.argmax(c,轴=0,输出类型=dt)
#通过移动减去的最小值获得模式
模式=idx+m
#获取计数的数量
计数=tf.math.reduce_max(c,轴=0)
#或者,您可以重用以前获得的索引
#比如说:
#计数=tf.transpose(tf.gather\u nd)(tf.transpose(c),tf.expand\u dims(idx,axis=-1),
#批次(尺寸=tf.等级(c)-1)
返回模式、计数
#试验
x=tf.常数([[1,3,0,0,3],
[1, 3, 1, 0, 2],
[1, 3, 1, 2, 2]])
tf.print(*模式_和_计数(x,轴=0),sep='\n')
# [1 3 1 0 2]
# [3 3 2 2 2]
tf.print(*模式_和_计数(x,轴=1),sep='\n')
# [0 1 1]
# [2 2 2]
模式

tf.map_fn(lambda x:tf.unique_与_计数(x).y[tf.argmax(tf.unique_与_计数(x).count,output_type=tf.int32)],tf.transpose(c))
模式计数

tf.map_fn(lambda x: tf.reduce_max(tf.unique_with_counts(x).count), tf.transpose(c))

<tf.Tensor: shape=(5,), dtype=int32, numpy=array([3, 3, 2, 2, 2])>
tf.map_fn(lambda x:tf.reduce_max(tf.unique_与_计数(x.count)),tf.transpose(c))

我只需使用地图中带有计数的“唯一”来实现这一点。fn

谢谢您的帮助。根据您的建议,我直接调用count_integers函数作为tfp.stats.count_integers(x,axis=0),它向我显示了错误:ValueError:value((4,)的形状不兼容,应为((2,))。但是,如果轴=1,则工作正常。你能提出一个原因吗?@abhi这就是我在代码()中的注释中提到的错误,请尝试传递
minlength=tf.math。将\u max(x)+1减少到
count\u integers
。谢谢你的帮助。你能描述一下你的步骤吗?这些步骤不难遵循。当然。。。它非常简单和优雅。。。我使用map\u fn将unique_与_计数一起应用。这使我们能够在所需轴上的每个元件上重复操作。我使用了tf.transpose(c),因为它使我能够在只有几行的情况下应用axis=0上的操作。如果您直接使用c而不是tf。转置(c)u可以在轴=1上应用相同的操作!别忘了投赞成票或接受它作为答案;-)
tf.map_fn(lambda x: tf.unique_with_counts(x).y[tf.argmax(tf.unique_with_counts(x).count, output_type=tf.int32)], tf.transpose(c))

<tf.Tensor: shape=(5,), dtype=int32, numpy=array([1, 3, 1, 0, 2])>
tf.map_fn(lambda x: tf.reduce_max(tf.unique_with_counts(x).count), tf.transpose(c))

<tf.Tensor: shape=(5,), dtype=int32, numpy=array([3, 3, 2, 2, 2])>