Broadcasting
Broadcasting
- expand(扩展数据)
- without copying data(不复制数据)
- tf.broadcast_to
Key idea
- Insert 1 dim ahead if needed
- Expand dims with size 1 to same size
- example:
[4,16,16,32]
[32]
[4,16,16,32]
[1,1,1,32]
[4,16,16,32]
[4,16,16,32]

How to understand?
When it has no axis
- Create a new concepy
- [classes, students, scores] + [scores]
When it has dim of size 1
- Treat it shared by all
- [classes,students,scores] + [students,1]
Broadcasting可以理解成把维度分成大维度和小维度,小维度较为具体,大维度更加抽象。也就是小维度针对某个示例,然后让这个示例通用语大维度。
Why broadcasting?
for real demanding
- [classes, students, scores]
- Add bias for every student: +5 score
- [4,32,8] + [4,32,8]
- [4,32,8] + [5.0]
memory consumption
- [4,32,8] -> 1024
- bias = [8]: [5.0,5.0,5.0,...] -> 8
Broadcastable?
Match from Last dim!
- if current dim=1, expand to same
- if either has no dim, insert one dim and expand to same
- otherwise, Not Broadcastable
[4,32,14,14]
[1,32,1,1] -> [4,32,14,14] √
[14,14] -> [1,1,14,14] -> [4,32,14,14] √
[2,32,14,14] ×
[3] √
[32,32,1] √
[4,1,1,1] √
import tensorflow as tf
x = tf.random.normal([4,32,32,3])
x.shape
TensorShape([4, 32, 32, 3])
(x+tf.random.normal([3])).shape
TensorShape([4, 32, 32, 3])
(x+tf.random.normal([32,32,1])).shape
TensorShape([4, 32, 32, 3])
(x+tf.random.normal([4,1,1,1])).shape
TensorShape([4, 32, 32, 3])
try:
(x+tf.random.normal([1,4,1,1])).shape
except Exception as e:
print(e)
Incompatible shapes: [4,32,32,3] vs. [1,4,1,1] [Op:Add] name: add/
(x+tf.random.normal([4,1,1,1])).shape
TensorShape([4, 32, 32, 3])
b = tf.broadcast_to(tf.random.normal([4,1,1,1]),[4,32,32,3])
b.shape
TensorShape([4, 32, 32, 3])
Broadcast VS Tile
a = tf.ones([3,4])
a.shape
TensorShape([3, 4])
a1 = tf.broadcast_to(a,[2,3,4])
a1.shape
TensorShape([2, 3, 4])
a2 = tf.expand_dims(a,axis=0) # 0前插入一维
a2.shape
TensorShape([1, 3, 4])
a2 = tf.tile(a2,[2,1,1]) # 复制一维2次,复制二、三维1次
a2.shape
TensorShape([2, 3, 4])
Broadcasting的更多相关文章
- broadcasting Theano vs. Numpy
broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...
- theano broadcasting
当我们使用函数对两个数组进行计算时,函数会对这两个数组的对应元素进行计算,因此它要求这两个数组有相同的大小(shape相同).如果两个数组的shape不同的话,会进行如下的广播(broadcastin ...
- Arduino live weather broadcasting 实时天气站
Live broadcasting with arduino get a pc , make it run linux. make arduino catch the weather sensor a ...
- numpy 中的 broadcasting 理解
broadcast 是 numpy 中 array 的一个重要操作. 首先,broadcast 只适用于加减. 然后,broadcast 执行的时候,如果两个 array 的 shape 不一样,会先 ...
- MATLAB/Octave warning: mx_el_eq: automatic broadcasting operation applied 错误分析
在进行对一个mXn的矩阵与mX1的矩阵进行==比较时,原意是想让mXn的矩阵的每一行分别与mX1的矩阵每一行进行比较,得到的结果虽然是对的,但会报一个warning: mx_el_eq: automa ...
- some code about numpy and notes about copy&broadcasting
import numpy as np np.__version__ #版本 #由于python的list不要求存储同样的类型,但是效率不高. L = [i for i in range(10)] L[ ...
- tensor的维度扩张的手段--Broadcasting
broadcasting是tensorflow中tensor维度扩张的最常用的手段,指对某一个维度上重复N多次,虽然它呈现数据已被扩张,但不会复制数据. 可以这样理解,对 [b,784]@[784,1 ...
- 吴裕雄--天生自然TensorFlow2教程:Broadcasting
Broadcasting可以理解成把维度分成大维度和小维度,小维度较为具体,大维度更加抽象.也就是小维度针对某个示例,然后让这个示例通用语大维度. import tensorflow as tf x ...
- 广播 (broadcasting)
广播 (broadcasting) 飞桨(PaddlePaddle,以下简称Paddle)和其他框架一样,提供的一些API支持广播(broadcasting)机制,允许在一些运算时使用不同形状的张量. ...
随机推荐
- 51nod 1239 欧拉函数之和【欧拉函数+杜教筛】
和bzoj 3944比较像,但是时间卡的更死 设\( f(n)=\sum_{d|n}\phi(d) g(n)=\sum_{i=1}^{n}f(i) s(n)=\sum_{i=1}^{n}\phi(i) ...
- 30行JavaScript代码实现一个比特币量化策略
精简极致的均线策略 30行打造一个正向收益系统 原帖地址:https://www.fmz.com/bbs-topic-new/262 没错!你听的没错是30行代码!仅仅30行小编我习惯先通篇来看看 代 ...
- python之定时器Timer
timer类 Timer(定时器)是Thread的派生类,用于在指定时间后调用一个方法. 构造方法: Timer(interval, function, args=[], kwargs={}) in ...
- ElasticSearch | centos7 上安装ES
0 参考博客文章(感谢!!!) [1] https://www.jianshu.com/p/10949f44ce9c 在linux服务器上安装jdk [2] https://www.elastic ...
- Windows中句柄和ID的区别
写在前面:这里介绍句柄 对于“句柄”,在下一直停留在一知半解的认识层面,近日在下学习Windows编程,决定趁此机会将句柄彻底搞清楚.查阅了一些网络上的资料,发现网络上的讲解大概可以分为两类:一种是以 ...
- 51nod 1138 连续整数的和
1138 连续整数的和 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 给出一个正整数N,将N写为若干个连续数字和的形式(长度 >= 2). ...
- 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- Linux单机环境下HDFS伪分布式集群安装操作步骤v1.0
公司平台的分布式文件系统基于Hadoop HDFS技术构建,为开发人员学习及后续项目中Hadoop HDFS相关操作提供技术参考特编写此文档.本文档描述了Linux单机环境下Hadoop HDFS伪分 ...
- 针对谷歌默认最小字体12px的正确解决方案
利用css3的缩放,其最终大小就是:12px * 0.9(缩放比例) = 10.8px; 居然行得通.但回头一想,这么写的话,IE7 IE8会不会不兼容,还是12px呢?不出所料,果然不兼容.此时,又 ...
- JData 整合ArtTemplate的前端框架
因为项目需要和自己的兴趣,几个月前结合模板解析神速的ArtTemplate,自己写了个框架取名JData,多多指教啊---因为一直没时间写文档,为了能够更方便地使用和避免我把代码忘了,今天抽空把文档写 ...