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)机制,允许在一些运算时使用不同形状的张量. ...
随机推荐
- bzoj 3159: 决战【LCT】
只是想复健一下LCT没想到做了不得了的题--调了两天QAQ 题解是这么说的: 但是果然还不太理解--因为swap的前后问题调了好久,(所以一开始养成的习惯后面就不要再改啦-- 总之大概就是把对位置lc ...
- Word Cloud (词云) - R
在前面已经陆续总结了如何用 Python 和 JavaScript 创建词云了,今天要说的是 R.其实 SPSS 和 SAS 的 Word Cloud 扩展模板都是基于 R 实现的. >> ...
- Tensor Operation
Main operation categories that encompass the operations of tensors. Reshaping operations Element-wis ...
- ppt自动制作器
讲一篇论文导入进入,自动生成该论文的展示ppt,啧啧啧,想想岂不是很美妙
- 前端基础jQuery
jQury jQuery 是一个 JavaScript 函数库,jQuery 极大地简化了 JavaScript 编程. jQuery库包含以下功能: HTML 元素选取 HTML 元素操作 CSS ...
- 使用HttpClient携带证书报错_Certificate for <IP> doesn't match any of the subject alternative names:[域名]
使用HttpClient携带pfx证书通过Https协议发送SOUP报文调用WebService接口时报如下错误: Exception in thread "main" javax ...
- Android偏好设置(6)应用和监听各偏好参数
Reading Preferences By default, all your app's preferences are saved to a file that's accessible fro ...
- Elixir安装
参考:https://laravel.com/docs/5.2/elixir 1. 安装node 去这里下载 2.可以用淘宝的cnpm加速! npm install -g cnpm --registr ...
- C# 控制台语音计算器
记得上高中时,给人当会计,帮忙结算月度工资:用的就是带语音功能的计算器! 当时用起来倍儿爽,于是速度加倍,效率加速:结果让老板赔了不少钱! 就是因为这个,才对语音计算器有了深刻印象!可能是这货坑了我! ...
- ava的动态代理机制详解
在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...