Python ``tensorflow.math.argmin()`` 最小值索引查找方法与实例


发布日期 : 2024-03-20 21:25:13 UTC

访问量: 10 次浏览

Python tensorflow.math.argmin()方法

TensorFlow是谷歌设计的开源python库,用于开发机器学习模型和深度学习神经网络。 argmin()是tensorflow数学模块中的一个方法。这个方法是用来寻找整个轴的最小值。

语法:

tensorflow.math.argmin(
input,axes,output_type,name
)

参数 :

1.input:
它是一个张量。这个张量允许的dtypes是 float32, float64, int32, uint8, int16, int8, complex64, int64, qint8, quint8, qint32, bfloat16, uint16, complex128, half, uint32, uint64。

2. axes:
它也是一个矢量。它描述了用于减少张量的轴。允许的dtype是int32和int64。另外,
[-rank(input),rank(input)]
是允许的范围。axes=0用于矢量。

3. output\_type:
它定义了返回结果的dtype。允许的值是int32,int64,默认值是int64。

name:
它是一个可选的参数,定义了操作的名称。

返回:

一个 output\_type 的张量,包含沿轴的最小值的索引。

示例 1:

# importing the library
import tensorflow as tf

# initializing the constant tensor
a = tf.constant([5,10,5.6,7.9,1,50]) # 1 is the minimum value at index 4

# getting the minimum value index tensor
b = tf.math.argmin(input = a)

# printing the tensor
print('tensor: ',b)

# Evaluating the value of tensor
c = tf.keras.backend.eval(b)

#printing the value
print('value: ',c)

输出:

tensor: tf.Tensor(4, shape=(), dtype=int64)
value: 4

示例 2:

这个例子使用了一个 shape(3,3) 的张量。

# importing the library
import tensorflow as tf

# initializing the constant tensor
a = tf.constant(value = [9,8,7,3,5,4,6,2,1],shape = (3,3))

# printing the initialized tensor
print(a)

# getting the minimum value indices tensor
b = tf.math.argmin(input = a)

# printing the tensor
print('Indices Tensor: ',b)

# Evaluating the tensor value
c = tf.keras.backend.eval(b)

# printing the value
print('Indices: ',c)

输出:

tf.Tensor(
[[9 8 7]
[3 5 4]
[6 2 1]], shape=(3, 3), dtype=int32)
Indices tensor: tf.Tensor([1 2 2], shape=(3,), dtype=int64)
Indices: [1 2 2]