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

本文深入解析了TensorFlow中tf.argmax(), tf.reduce_max()等函数的使用方法与原理,通过实例说明了这些函数如何处理张量,以及它们在深度学习项目中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值