Tensorflow Learning Notes-tf.argmax() tf.reduce_max()

In tensorflow source code,we may often meet those functions like:

tf.argmax(),tf.reduce_max(),tf.argmin(),tf.reduce_min(),tf.reduce_mean...and so on.Likes their function name,those functions have many similarities,not only in their Input function but also in their inner principles.Just now, I have readed the offical API and got a preliminary understanding of those functions.Under this blogs,there also have several examples abouts those functions,and I will explain them by my own understanding.I just hope that when I next time meet those functions,I know how to use them.

  • tf.argmax(input,axis=None,name=None,output_types=tf.dtypes.int64)
  • input:input tensor
  • axis:a tensor,must be in the range [-rank(input),rank(input)],Describs which axis of the input Tensor to reduce across.
  • name:the name of Tensor
  • output_types:the Dtypes of output Tensor

This function will return the index with the largest value axes of a tensor.

what we should pay attention to is that this function return the index rather than the largest arguments of the Tensor.Such as we have a (3,3) tensor a,and pass a to this function,set axis = -1,it returns a (3) vector b,each elements of  b represents a index of largest value in a's last dimention.And,waht is the axis =-1 mean?Before illustrating the meaning of axis=-1,let's recall the list in python,for example we have a list a=[2,2,3].It is very easy to see that a have 3 elements,and each elements in a has an index,respacetively are 0,1,2 or -3,-2,-1.And if (2,2,3) is a tensor's shape,similar to the list,we may understand waht means of axis=-1.It means we operate the tensor in the last dimention of (2,2,3),and if axis=0,it means we operate the first dimention of (2,2,3),and so on.And then,there is a example about this:

import tensorflow as tf
a=tf.constant([[1,2,3],
               [4,5,6],
               [7,8,9]])
b=tf.argmax(a,axis=-1)

with tf.Session() as sess:
    print(sess.run(b))

Reslut:[2,2,2]

As you can in the example,tf.argmax() returns the largest argument's index,as result of a is a (3,3) tensor,so b is a 1D tensor.The function tf.argmin() is same as this function,so I will not spend too much word to introduce this funtion again.

  • tf.reduce_max(inputs,axis=None,keepdims=None)
  • input: the input tensor
  • axis : the dimention to reduce
  • keepdims : if ture,retains reduced dimentions with length 1

There are a lot of differents between tf.argmax() and tf.reduce_max(),first of all,tf.reduce_max() returns the largest elements in inputs rather than returns the index.On the other hand,"reduce" means that this function will reduce the dimentions of input tensor.and argument axis is the reduce dimention of input tensor.

import tensorflow as tf
a=tf.constant([[1,2,3],
               [4,5,6],
               [7,8,9]])
b=tf.argmax(a,axis=-1)

with tf.Session() as sess:
    print(sess.run(b))

Reslut:[3,6,9]

 

 

 

 

 

扫描二维码关注公众号,回复: 5613670 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_23557345/article/details/88076487