TensorFlow中tf.argmax()的用法

本文详细解析了TensorFlow中argmax函数的使用方法,包括其参数含义、返回值类型及具体应用实例。通过矩阵和数组的例子,阐述了如何获取最大值的索引,并解释了axis参数的作用。

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

官方API的定义

tf.argmax(input, axis=None, name=None, dimension=None)
Returns the index with the largest value across axes of a tensor.
Args:


input: A Tensor. Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half.

axis: A Tensor. Must be one of the following types: int32, int64. int32, 0 <= axis < rank(input). Describes which axis of the input Tensor to reduce across. For vectors, use axis = 0.

name: A name for the operation (optional).

Returns:

A Tensor of type int64.

关于axis的说明

input可以是数组,矩阵,当input是矩阵(np.mat)时:

  • axis = 0的时候返回每一列最大值的位置索引
  • axis = 1的时候返回每一行最大值的位置索引
import tensorflow as tf
import numpy as np

x = np.mat([0, 0, 1])
y = np.mat([[0, 0, 1], [1, 2, 3]])
x_max_index = tf.argmax(x, 1)
y_max_index = tf.argmax(y, 1)

with tf.Session() as sess:
    x_index = sess.run(x_max_index)
    y_index = sess.run(y_max_index)
    print("x_max_index = ", x_index)
    print("y_max_index = ", y_index)

输出:
x_max_index = [2]
y_max_index = [2 2]

输入和输出的数据类型一样,当输入是array时,输出也是array。不一样的地方在于,当数组是一维数组时,axis只能为0,输出最大数的索引值。

https://www.cnblogs.com/xzcfightingup/p/7598293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

象牙塔小明

您的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值