当前位置:网站首页>生信实验记录(part2)--tf.reduce_sum()用法介绍
生信实验记录(part2)--tf.reduce_sum()用法介绍
2022-08-11 01:26:00 【GoatGui】
学习笔记,仅供参考,有错必纠
参考自:tf.reduce_sum()用法介绍
前言
在复现的时候遇到了这个函数…emmm… 说起来我对深度学习相关知识了解的确实很少.
方法定义
tf.reduce_sum()的作用是按一定方式计算张量中元素之和.
tf.reduce_sum(
input_tensor,
axis=None,
keepdims=None,
name=None)
参数解释:
input_tensor为待处理张量.
axis为指定按哪个维度进行加和,默认将所有元素进行加和.
keepdims表示不维持原来张量的维度,默认为False,若为True维持原张量维度, .
name用于定义该操作名字.
示例
有形如(2,3,4)的三维张量:
import tensorflow as tf
a = tf.constant([[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 9, 8]],
[[7, 6, 5, 4],
[3, 2, 1, 0],
[1, 2, 3, 4]]])
print(a.shape)
# (2, 3, 4)
分别设置axis为0,1,2进行加和的结果如下:
b = tf.reduce_sum(a, axis = 0)
c = tf.reduce_sum(a, axis = 1)
d = tf.reduce_sum(a, axis = 2)
sess = tf.InteractiveSession()
b
b.eval()
<tf.Tensor 'Sum_18:0' shape=(3, 4) dtype=int32>
array([[ 8, 8, 8, 8],
[ 8, 8, 8, 8],
[10, 12, 12, 12]])
c
c.eval()
<tf.Tensor 'Sum_19:0' shape=(2, 4) dtype=int32>
array([[15, 18, 19, 20],
[11, 10, 9, 8]])
d
d.eval()
<tf.Tensor 'Sum_20:0' shape=(2, 3) dtype=int32>
array([[10, 26, 36],
[22, 6, 10]])
可以看到,原张量是三维的,指定axis进行一次tf.reduce_sum()后变成了二维张量. 如果设置keepdims=True,则结果如下,可以看到计算完加和后的张量依然是三维的,其中指定axis的那个维度元素个数为1,其他维度形状不变.
b = tf.reduce_sum(a, axis = 0, keepdims=True)
c = tf.reduce_sum(a, axis = 1, keepdims=True)
d = tf.reduce_sum(a, axis = 2, keepdims=True)
b
b.eval()
<tf.Tensor 'Sum_24:0' shape=(1, 3, 4) dtype=int32>
array([[[ 8, 8, 8, 8],
[ 8, 8, 8, 8],
[10, 12, 12, 12]]])
c
c.eval()
<tf.Tensor 'Sum_25:0' shape=(2, 1, 4) dtype=int32>
array([[[15, 18, 19, 20]],
[[11, 10, 9, 8]]])
d
d.eval()
<tf.Tensor 'Sum_26:0' shape=(2, 3, 1) dtype=int32>
array([[[10],
[26],
[36]],
[[22],
[ 6],
[10]]])
sess.close()
边栏推荐
猜你喜欢

Dual machine thermal for comprehensive experiment (VRRP + OSPF + + NAT + DHCP + VTP PVSTP + single-arm routing)

【开源】壁纸软件,给自己电脑设计专属特效

Flink二阶段提交

连流量染色都没有,你说要搞微服务?

Shell Text Three Musketeers Sed

SQL语句--获取数据库表信息,表名、列名、描述注释等

MSTP - Multiple Spanning Tree (Case + Configuration)

Qt 中的隐式共享

rhel7.0解决yum无法使用(system is not registered to Red Hat Subscription Management)

Linux install redis database
随机推荐
【C语言】探索数据的存储(整形篇)
Update chromedriver driver programming skills │ selenium
postgresql parameter meaning
进程间通信方式(1)无名管道(全CSDN最用心的博主)
url转成obj或者obj转成url的方法
络达开发---自定义Timer的实现
微信小程序通过URL Scheme动态的渲染数据
WebView2 通过 PuppeteerSharp 实现RPA获取壁纸 (案例版)
#yyds Dry Goods Inventory#[Yugong Series] August 2022 Go Teaching Course 008-Integer of Data Types
[ASM] The relationship between the role of the bytecode operation ClassWriter COMPUTE_FRAMES and visitMaxs
Exception: try catch finally throws throw
MySQL基础篇【第一篇】| 数据库概述及数据准备、常用命令、查看表结构步骤
划分字母区间[贪心->空间换时间->数组hash优化]
sql 使用到where和groupby时到底怎么建立索引?
C#-委托的详细用法
Construction inspection, no rules and no square
Elastic scaling of construction resources
php 判断数组是否为多维数组
单片机人机交互--矩阵按键
R语言多元线性回归、ARIMA分析美国不同候选人对经济GDP时间序列影响