``tf.math.reduce_std ()`` 使用方法


发布日期 : 2024-10-04 16:13:37 UTC

访问量: 10 次浏览

Python – tensorflow.math.reduce_std()

TensorFlow 是谷歌设计的开源 Python 库,用于开发机器学习模型和深度学习神经网络。

reduce_std() 用于查找张量中各维度元素的标准偏差。

语法

tensorflow.math.reduce_std(input_tensor, axis, keepdims, name)

参数

  • input_tensor:要减少的数字张量
  • axis(可选):代表要减少的维度。它的值应该在 [-rank(input_tensor), rank(input_tensor)] 范围内。如果没有给这个值,所有的维度都会被减少
  • keepdims(可选):它的默认值是 False。如果它被设置为 True,它将保留长度为 1 的缩小尺寸
  • name(可选):定义该操作的名称

返回

返回一个张量。

示例 1

# importing the library
import tensorflow as tf

# Initializing the input tensor
a = tf.constant([1, 2, 3, 4], dtype=tf.float64)

# Printing the input tensor
print('Input: ', a)

# Calculating result
res = tf.math.reduce_std(a)

# Printing the result
print('Result: ', res)

输出

Input:  tf.Tensor([1. 2. 3. 4.], shape=(4, ), dtype=float64)
Result:  tf.Tensor(1.118033988749895, shape=(), dtype=float64)

示例 2

# importing the library
import tensorflow as tf

# Initializing the input tensor
a = tf.constant([[1, 2], [3, 4]], dtype=tf.float64)

# Printing the input tensor
print('Input: ', a)

# Calculating result
res = tf.math.reduce_std(a, axis=1, keepdims=True)

# Printing the result
print('Result: ', res)

输出

Input:  tf.Tensor(
[[1. 2.]
 [3. 4.]], shape=(2, 2), dtype=float64)
Result:  tf.Tensor(
[[0.5]
 [0.5]], shape=(2, 1), dtype=float64)