[477]tf.reduce_mean()

news/2024/10/23 9:34:58/

tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。

reduce_mean(input_tensor,axis=None,keep_dims=False,name=None,reduction_indices=None)
  • 第一个参数input_tensor: 输入的待降维的tensor;
  • 第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值;
  • 第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;
  • 第四个参数name: 操作的名称;
  • 第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用;

以一个维度是2,形状是[2,3]的tensor举例:

import tensorflow as tfx = [[1,2,3],[1,2,3]]xx = tf.cast(x,tf.float32)mean_all = tf.reduce_mean(xx, keep_dims=False)
mean_0 = tf.reduce_mean(xx, axis=0, keep_dims=False)
mean_1 = tf.reduce_mean(xx, axis=1, keep_dims=False)with tf.Session() as sess:m_a,m_0,m_1 = sess.run([mean_all, mean_0, mean_1])print m_a    # output: 2.0
print m_0    # output: [ 1.  2.  3.]
print m_1    #output:  [ 2.  2.]

如果设置保持原来的张量的维度,keep_dims=True ,结果:

print m_a    # output: [[ 2.]]
print m_0    # output: [[ 1.  2.  3.]]
print m_1    #output:  [[ 2.], [ 2.]]

类似函数还有:

tf.reduce_sum :计算tensor指定轴方向上的所有元素的累加和;
tf.reduce_max : 计算tensor指定轴方向上的各个元素的最大值;
tf.reduce_all : 计算tensor指定轴方向上的各个元素的逻辑和(and运算);
tf.reduce_any: 计算tensor指定轴方向上的各个元素的逻辑或(or运算);

来源:https://blog.csdn.net/dcrmg/article/details/79797826


http://www.ppmy.cn/news/535626.html

相关文章

LeetCode_位运算_中等_477.汉明距离总和

目录 1.题目2.思路3.代码实现(Java) 1.题目 两个整数的汉明距离指的是这两个数字的二进制数对应位不同的数量。 给你一个整数数组 nums,请你计算并返回 nums 中任意两个数之间汉明距离的总和。 示例 1: 输入:nums …

LeetCode | 477. Total Hamming Distance

题目 The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given an integer array nums, return the sum of Hamming distances between all the pairs of the integers in nums. Example 1: Input: n…

【博客477】prometheus-----数值数据编码(varint与zigzag)

prometheus-----数据编码(varint与zigzag) prometheus对数值数据进行编码时,使用到了varint与zigzag varint与zigzag编码方法在protobuf中也被使用 prometheus encoding代码: https://github.com/prometheus/prometheus/blob/main/tsdb/encoding/encodi…

​LeetCode刷题实战477:汉明距离总和

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大…

LeetCode 算法 每日一题 477.汉明距离总和

10.正则表达式匹配 题目描述 两个整数的汉明距离指的是这两个数字的二进制数对应位不同的数量。 计算一个数组中,任意两个数之间汉明距离的总和。 示例1 输入: 4, 14, 2 输出: 6 解释: 在二进制表示中,4表示为0100,14表示为1110&#xff0…

奇舞周刊 477 期:一文弄懂 React ref 原理

记得点击文章末尾的“ 阅读原文 ”查看哟~ 下面先一起看下本期周刊 摘要 吧~ 奇舞推荐 ■ ■ ■ 一文弄懂 React ref 原理 对于 Ref 理解与使用,一些读者可能还停留在用 ref 获取真实 DOM 元素和获取类组件实例层面上 其实 ref 除了这两项常用功能之外,还…

Java实现 LeetCode 477 汉明距离总和

477. 汉明距离总和 两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量。 计算一个数组中,任意两个数之间汉明距离的总和。 示例: 输入: 4, 14, 2 输出: 6 解释: 在二进制表示中,4表示为0100,14表示为1110,2表…

leetcode 477.汉明距离总和

每日一题 昨天做了道相似的汉明距离详见leetcode461&#xff0c;今天又看见类似的题目准备重拳出击&#xff01; 博主技术有限…于是直接暴力 class Solution {public int totalHammingDistance(int[] nums) {int sum 0;int total 0;for(int i 0;i < nums.length;i){for…