当前位置:网站首页>Shengxin experiment record (part2)--tf.reduce_sum() usage introduction
Shengxin experiment record (part2)--tf.reduce_sum() usage introduction
2022-08-11 01:40:00 【GoatGui】
学习笔记,仅供参考,有错必纠
参考自:tf.reduce_sum()用法介绍
前言
I came across this function while reproducing…emmm… Speaking of which, I really know very little about deep learning.
方法定义
tf.reduce_sum()
The role of is to calculate the sum of the elements in the tensor in a certain way.
tf.reduce_sum(
input_tensor,
axis=None,
keepdims=None,
name=None)
参数解释:
input_tensoris the tensor to be processed.
axisSpecifies which dimension to sum by,All elements are added by default.
keepdimsIndicates that the dimensions of the original tensor are not maintained,默认为False,若为TrueThe original tensor dimensions are maintained, .
nameUsed to define the operation 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,2The result of performing the addition is as follows:
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]])
可以看到,The original tensor is three-dimensional,指定axis
进行一次tf.reduce_sum()
Then it becomes a two-dimensional tensor. 如果设置keepdims=True
,则结果如下,It can be seen that the tensor after calculating the sum is still three-dimensional,其中指定axisThe number of elements in that dimension is 1,The other dimensions remain unchanged.
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()
边栏推荐
猜你喜欢
随机推荐
Single-chip human-computer interaction--matrix key
Is container technology really the savior of environmental management?
J9 Digital Theory: DAO governance is more like an ecological process: governance is native to the network and continues to evolve
Data Filters in ABP
More parameter exposure of Pico 4: Pancake + color perspective, and Pro version
Apache Commons Configuration远程代码执行漏洞(CVE-2022-33980)分析&复现
SAP ABAP JSON 格式数据处理
安装dlib库
Mysql数据库安装配置详细教程
嵌入式软件打log的一些心得
Apache Commons Configuration Remote Code Execution Vulnerability (CVE-2022-33980) Analysis & Reproduction
什么是数组
简陋的nuxt3学习笔记
SystemVerilog: 验证知识点点滴滴
【ASM】字节码操作 ClassWriter COMPUTE_FRAMES 的作用 与 visitMaxs 的关系
进程间通信方式(1)无名管道(全CSDN最用心的博主)
双机热备综合实验(VRRP+OSPF+VTP+NAT+DHCP+PVSTP+单臂路由)
OpenWrt之opkg详解
Exceptions and exception handling mechanisms
Experiment record of Shengxin (part3)--scipy.spatial.distance_matrix