【发布时间】:2019-03-30 06:35:13
【问题描述】:
我正在尝试在 tensorflow 中创建一个 2D 直方图,以用于 tensorflow 中的自定义损失函数。更一般地说,我认为人们可以从使用神经元的共同激活中受益,这需要类似的结构。
这就是我想要做的具体事情:
给定一个 Nx2 张量,其中 N 是一些样本数,我想创建一个(合并的)共激活直方图。例如,在 input=[[0, 0.01], [0, 0.99], [0.5, 0.5]] 和总共 10000 个 bin 的简单情况下,我想生成一个 100x100 的张量,除 3 外全为 0 (0, 0.01)、(0, 0.99) 和 (0.5, 0.5) 处的条目,其中的值为 1/3(缩放很容易,所以我可以用 1 代替)。
我可以使用标准的 numpy 或数组操作轻松做到这一点
neuron1 = data[:, 1]
neuron2 = data[:, 2]
hist_2d = np.zeros((100, 100))
for neuron1_output in neuron1:
for neuron2_output in neuron2:
hist_2d[int(100 * neuron1_output), int(100 * neuron2_output)] += 1
如果我想在 Tensorflow 中使用 hist_2d 作为损失函数的一部分,我似乎无法进行这种迭代。
有人知道生成我正在寻找的二维直方图的好方法吗?我很高兴找到 tf.histogram_fixed_width(),但这只会生成 1d 直方图。我已经开始研究 tf.while_loop() 和 tf.map_fn(),但我对 Tensorflow 还很陌生,所以我不确定哪种途径最有前途。
【问题讨论】:
标签: python numpy tensorflow histogram distribution