TensorFlow Max: Use tf.reduce_max To Get Max Value Of A TensorFlow Tensor

TensorFlow Max - Use tf.reduce_max to get max value of a TensorFlow Tensor

Type: FREE   By: Sebastian Gutierrez, AIWorkbox.com Instructor Sebastian Gutierrez   Duration: 2:34   Technologies: TensorFlow, Python

Page Sections: Video  |  Code  |  Transcript


< > Code:

You must be a Member to view code

Access all courses and lessons, gain confidence and expertise, and learn how things work and how to use them.

    or   Log In


Transcript:

We import TensorFlow as tf.

import tensorflow as tf


Then we print the TensorFlow version that we are using.

print(tf.__version__)

We are using TensorFlow 1.0.1.


Next, we are going to create a TensorFlow tensor that’s going to hold random numbers.

random_int_var = tf.get_variable("random_int_var",
                                 initializer=tf.random_uniform([2, 3, 4],
                                                               minval=0,
                                                               maxval=20,
                                                               dtype=tf.int32))

We’re creating this tensor.

We’re naming it random_int_var.

We’re going to initialize it with the tf.random_uniform operation and we’re going to have it be a 2x3x4 tensor with a minimum value of 0, a max value of 20, and the data type is int32, and we’re going to assign this to the Python variable random_int_var.


Next, we create the TensorFlow operation that initializes all the global variables in the graph.

init_var = tf.global_variables_initializer()


Then we launch the graph in the session.

sess = tf.Session()


The next step is to initialize all the variables.

sess.run(init_var)


To calculate the maximum value of an element in all of our TensorFlow tensor, we’re going to use the tf.reduce_max operation.


But first, let’s print out what our IntTensor looks like so we can visually inspect it.

print(sess.run(random_int_var))

Looking at it, we see that it is a 2x3x4 tensor and visually, it looks like we have a couple of 19s, so the max value should be 19.


So we can manually figure out what the max value of the whole tensor is visually.

However, if we had a very large tensor, it would be much better to do it programmatically.

So we’re going to use the tf.reduce_max operation to figure things out.


So to calculate the max of the random_int_var tensor, we use the tf.reduce_max operation and we pass our tensor to it and we run it within a session and then we print the result.

print(sess.run(tf.reduce_max(random_int_var)))

The value that we get is 19 which is indeed what we figured out would be the max of the tensor.


One thing to note about the tf.reduce_max operation is that it also lets you specify which tensor dimension you want to get the max of.

So we know it’s a 2x3x4 tensor so we know this tensor has a rank of 3 so we can have three possible dimensions.


However, because we want the max of the whole tensor, we can write the following.

print(sess.run(tf.reduce_max(random_int_var, reduction_indices=None)))

So what we did is we said reduction_indices which would be how we specify which dimension we want to get the max across.

We said None with a capital letter.

So when we specified None, it means that we want the max of the whole tensor.

So again, we see 19.


And that is how you can calculate the max of the tensor in TensorFlow using the tf.reduce_max operation.



Back to deep learning tutorial lesson list