Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It Possible to the Take the Mode of a Tensor in Tensorflow?

I'm trying to construct a DAG in Tensorflow where I need to take the mode (most frequent value) of individual regions of my target. This is in order to construct a downsampled target.

Right now, I'm pre-processing the downsampled targets for every individual situation I might encounter, saving them, and then loading them. Obviously, this would all be much easier if it was integrated into my Tensorflow graph, so that I could downsample at runtime.

But I've looked everywhere, and I can find no evidence of a tf.reduce_mode, that would function the same as tf.reduce_mean. Is there any way to construct this functionality in a Tensorflow graph?

like image 458
DM Relenzo Avatar asked Oct 10 '17 16:10

DM Relenzo


People also ask

How do you evaluate a tensor in TensorFlow?

The easiest[A] way to evaluate the actual value of a Tensor object is to pass it to the Session. run() method, or call Tensor. eval() when you have a default session (i.e. in a with tf.

Can you slice tensors?

You can use tf. slice on higher dimensional tensors as well. You can also use tf. strided_slice to extract slices of tensors by 'striding' over the tensor dimensions.

What is TensorFlow graph mode?

TensorFlow uses graphs as the format for saved models when it exports them from Python. Graphs are also easily optimized, allowing the compiler to do transformations like: Statically infer the value of tensors by folding constant nodes in your computation ("constant folding").

What is the difference between tensor and TensorFlow?

TensorFlow, as the name indicates, is a framework to define and run computations involving tensors. A tensor is a generalization of vectors and matrices to potentially higher dimensions. Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes.


1 Answers

My idea is that we get the unique numbers and their counts. We then find the numbers that appear most frequently. Finally we fetch those numbers (could be more than one) out by using their indices in the number-count tensor.

samples = tf.constant([10, 32, 10, 5, 7, 9, 9, 9])
unique, _, count = tf.unique_with_counts(samples)
max_occurrences = tf.reduce_max(count)
max_cond = tf.equal(count, max_occurrences)
max_numbers = tf.squeeze(tf.gather(unique, tf.where(max_cond)))

with tf.Session() as sess:
  print 'Most frequent Numbers\n', sess.run(max_numbers)
> Most frequent Numbers
  9
like image 63
greeness Avatar answered Sep 19 '22 17:09

greeness