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
Tensorflow 行直方图_Tensorflow - Fatal编程技术网

Tensorflow 行直方图

Tensorflow 行直方图,tensorflow,Tensorflow,给定一个二维张量t,计算张量h的最快方法是什么 h[i, :] = tf.histogram_fixed_width(t[i, :], vals, nbins) 也就是说,tf.每行输入张量t调用一个固定宽度的直方图 似乎tf.histogram\u fixed\u width缺少一个类似于tf.reduce\u sum的轴的参数的轴参数。确实适用于整个张量。您必须显式地循环行以计算每行直方图。以下是使用TensorFlow构造的完整工作示例: import tensorflow as tf

给定一个二维张量
t
,计算张量
h
的最快方法是什么

h[i, :] = tf.histogram_fixed_width(t[i, :], vals, nbins)
也就是说,
tf.每行输入张量
t
调用一个固定宽度的直方图

似乎
tf.histogram\u fixed\u width
缺少一个类似于
tf.reduce\u sum
轴的
参数的
参数。

确实适用于整个张量。您必须显式地循环行以计算每行直方图。以下是使用TensorFlow构造的完整工作示例:

import tensorflow as tf

t = tf.random_uniform([2, 2])

i = 0
hist = tf.constant(0, shape=[0, 5], dtype=tf.int32)

def loop_body(i, hist):
  h = tf.histogram_fixed_width(t[i, :], [0.0, 1.0], nbins=5)
  return i+1, tf.concat_v2([hist, tf.expand_dims(h, 0)], axis=0)

i, hist = tf.while_loop(
              lambda i, _: i < 2, loop_body, [i, hist],
              shape_invariants=[tf.TensorShape([]), tf.TensorShape([None, 5])])

sess = tf.InteractiveSession()
print(hist.eval())
将tensorflow导入为tf
t=tf.随机均匀([2,2])
i=0
hist=tf.constant(0,shape=[0,5],dtype=tf.int32)
def回路_主体(i,历史):
h=tf.直方图固定宽度(t[i,:],[0.0,1.0],nbins=5)
返回i+1,tf.concat_v2([hist,tf.expand_dims(h,0)],轴=0)
i、 hist=tf.while\u循环(
λi,i:i<2,环体,[i,hist],
形状_不变量=[tf.TensorShape([]),tf.TensorShape([None,5]))
sess=tf.InteractiveSession()
打印(hist.eval())
确实适用于整个张量。您必须显式地循环行以计算每行直方图。以下是使用TensorFlow构造的完整工作示例:

import tensorflow as tf

t = tf.random_uniform([2, 2])

i = 0
hist = tf.constant(0, shape=[0, 5], dtype=tf.int32)

def loop_body(i, hist):
  h = tf.histogram_fixed_width(t[i, :], [0.0, 1.0], nbins=5)
  return i+1, tf.concat_v2([hist, tf.expand_dims(h, 0)], axis=0)

i, hist = tf.while_loop(
              lambda i, _: i < 2, loop_body, [i, hist],
              shape_invariants=[tf.TensorShape([]), tf.TensorShape([None, 5])])

sess = tf.InteractiveSession()
print(hist.eval())
将tensorflow导入为tf
t=tf.随机均匀([2,2])
i=0
hist=tf.constant(0,shape=[0,5],dtype=tf.int32)
def回路_主体(i,历史):
h=tf.直方图固定宽度(t[i,:],[0.0,1.0],nbins=5)
返回i+1,tf.concat_v2([hist,tf.expand_dims(h,0)],轴=0)
i、 hist=tf.while\u循环(
λi,i:i<2,环体,[i,hist],
形状_不变量=[tf.TensorShape([]),tf.TensorShape([None,5]))
sess=tf.InteractiveSession()
打印(hist.eval())

受到keveman答案的启发,并且由于
t
的行数是固定的,而且非常小,因此我选择使用
tf.gather
组合来拆分行,并使用
tf.pack
组合来连接行。它看起来简单有效,我们会看看它是否有效

t_histo_rows = [
        tf.histogram_fixed_width(
            tf.gather(t, [row]),
            vals, nbins)
        for row in range(t_num_rows)]

t_histo = tf.pack(t_histo_rows, axis=0)

受keveman答案的启发,由于
t
的行数是固定的,而且非常小,我选择使用
tf.gather
组合来拆分行,使用
tf.pack
组合来连接行。它看起来简单有效,我们会看看它是否有效

t_histo_rows = [
        tf.histogram_fixed_width(
            tf.gather(t, [row]),
            vals, nbins)
        for row in range(t_num_rows)]

t_histo = tf.pack(t_histo_rows, axis=0)

我想提出另一个实施方案。 此实现还可以处理多轴和未知尺寸(批处理)

这里唯一慢的部分是
tf.map\fn
,但它仍然比前面提到的其他解决方案快


如果有人知道一个更快的实现,请评论,因为这个操作仍然非常昂贵。

我想提出另一个实现。 此实现还可以处理多轴和未知尺寸(批处理)

这里唯一慢的部分是
tf.map\fn
,但它仍然比前面提到的其他解决方案快


如果有人知道一个更快的实现,请发表评论,因为这个操作仍然非常昂贵。

上面的回答在GPU中仍然运行缓慢。在这里,我给出了另一个选项,它更快(至少在我的运行环境中),但它被限制为0~1(可以先规范化值)。可以提前定义一次列相等掩码

def直方图(张量、nbins、行数、列数):
#初始掩码
相等掩码列表=[]
对于范围内的i(nbins):
相等掩码列表追加(tf.ones([row\u num,col\u num],dtype=tf.int32)*i)
#[nbins,row,col]
#[0,row,col]是形状为[row,col]且所有值均为0的张量
#[1,row,col]是所有值为1的形状为[row,col]的张量
#....
列相等掩码=tf.stack(相等掩码列表,轴=0)
#[inst,doc_len]浮点到整数(容器中的相等分段浮点)
int_input=tf.cast(张量*(nbins),dtype=tf.int32)
#输入[row,col]->复制N次[nbins,row\u num,col\u num]
int\u input\u nbin\u copy=tf.reformate(tf.tile(int\u input,[nbins,1]),[nbins,row\u num,col\u num])
#计算直方图
直方图=tf.转置(tf.计数非零(tf.相等(列相等掩码,整数输入掩码,轴=2))
返回直方图

上面的答案在GPU中仍然运行缓慢。在这里,我给出了另一个选项,它更快(至少在我的运行环境中),但它被限制为0~1(可以先规范化值)。可以提前定义一次列相等掩码

def直方图(张量、nbins、行数、列数):
#初始掩码
相等掩码列表=[]
对于范围内的i(nbins):
相等掩码列表追加(tf.ones([row\u num,col\u num],dtype=tf.int32)*i)
#[nbins,row,col]
#[0,row,col]是形状为[row,col]且所有值均为0的张量
#[1,row,col]是所有值为1的形状为[row,col]的张量
#....
列相等掩码=tf.stack(相等掩码列表,轴=0)
#[inst,doc_len]浮点到整数(容器中的相等分段浮点)
int_input=tf.cast(张量*(nbins),dtype=tf.int32)
#输入[row,col]->复制N次[nbins,row\u num,col\u num]
int\u input\u nbin\u copy=tf.reformate(tf.tile(int\u input,[nbins,1]),[nbins,row\u num,col\u num])
#计算直方图
直方图=tf.转置(tf.计数非零(tf.相等(列相等掩码,整数输入掩码,轴=2))
返回直方图

我只想指出,如果
t\u num\u rows
在图形构建时间未知(使用tensorflow和批处理时很可能发生这种情况),则
tf.gather
解决方案不起作用。我只想指出,如果
t\u num\u rows
在图形构建时间未知,则
tf.gather
解决方案不起作用(使用tensorflow和批处理时很可能发生的情况)。