title: Tensorflow 2d Histogram
body: <p>I'm trying to create a 2D histogram in tensorflow to use in a custom loss function in tensorflow. More generally, I think people could benefit from using co-activations of neurons, which requires a similar structure.</p>
  
  <p>Here's specifically what I'm trying to do:</p>
  
  <p>Given a Nx2 Tensor, where N is some number of samples, I'd like to create a (binned) histogram of co-activations. For example, in the simple case of input=[[0, 0.01], [0, 0.99], [0.5, 0.5]] and with 10000 bins total, I'd like to generate a 100x100 Tensor with all 0s except for 3 entries at (0, 0.01), (0, 0.99), and (0.5, 0.5), where the value would be 1/3 (scaling is easy, so I'd be fine with 1 instead).</p>
  
  <p>I could do this easily using standard numpy or array operations</p>
  
  <pre><code>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
  </code></pre>
  
  <p>If I want to use hist_2d as part of a loss function in Tensorflow, though, it looks like I can't do this kind of iterating.</p>
  
  <p>Does anyone know of a good way to generate the 2d histogram I'm looking for? I was happy to find tf.histogram_fixed_width(), but that only generates 1d histograms. I've started looking into tf.while_loop() and tf.map_fn(), but I'm pretty new to Tensorflow, so I'm not sure what avenue is most promising.</p>