吴裕雄--天生自然TensorFlow2教程:维度变换
图片视图
[b, 28, 28] # 保存b张图片,28行,28列(保存数据一般行优先),图片的数据没有被破坏
[b, 28*28] # 保存b张图片,不考虑图片的行和列,只保存图片的数据,不关注图片数据的细节
[b, 2, 14*28] # 保存b张图片,把图片分为上下两个部分,两个部分具体多少行是不清楚的
[b, 28, 28, 1] # 保存b张图片,28行,28列,1个通道
First Reshape(重塑视图) import tensorflow as tf
a = tf.random.normal([4, 28, 28, 3])
a.shape, a.ndim tf.reshape(a, [4, 784, 3]).shape # 给出一张图片某个通道的数据,丢失行、宽的信息 tf.reshape(a, [4, -1, 3]).shape # 4*(-1)*3 = 4*28*28*3 tf.reshape(a, [4, 784*3]).shape # 给出一张图片的所有数据,丢失行、宽和通道的信息 tf.reshape(a, [4, -1]).shape
Second Reshape(恢复视图) tf.reshape(tf.reshape(a, [4, -1]), [4, 28, 28, 3]).shape tf.reshape(tf.reshape(a, [4, -1]), [4, 14, 56, 3]).shape tf.reshape(tf.reshape(a, [4, -1]), [4, 1, 784, 3]).shape first reshape:
images: [4,28,28,3]
reshape to: [4,784,3] second reshape:
[4,784,3] height:28,width:28 [4,28,28,3] √
[4,784,3] height:14,width:56 [4,14,56,3] ×
[4,784,3] width:28,height:28 [4,28,28,3] ×
Transpose(转置) a = tf.random.normal((4, 3, 2, 1))
a.shape tf.transpose(a).shape tf.transpose(a, perm=[0, 1, 3, 2]).shape # 按照索引替换维度 a = tf.random.normal([4, 28, 28, 3]) # b,h,w,c
a.shape tf.transpose(a, [0, 2, 1, 3]).shape # b,2,h,c tf.transpose(a, [0, 3, 2, 1]).shape # b,c,w,h tf.transpose(a, [0, 3, 1, 2]).shape # b,c,h,w
Expand_dims(增加维度) a:[classes, students, classes]
add school dim(增加学校的维度):
[1, 4, 35, 8] + [1, 4, 35, 8] = [2, 4, 35, 8] a = tf.random.normal([4, 25, 8])
a.shape tf.expand_dims(a, axis=0).shape # 索引0前 tf.expand_dims(a, axis=3).shape # 索引3前 tf.expand_dims(a,axis=-1).shape # 索引-1后 tf.expand_dims(a,axis=-4).shape # 索引-4后,即左边空白处
Squeeze(挤压维度)
Only squeeze for shape = 1 dim(只删除维度为1的维度) [4, 35, 8, 1] = [4, 35, 8]
[1, 4, 35, 8] = [14, 35, 8]
[1, 4, 35, 1] = [4, 35, 8] tf.squeeze(tf.zeros([1,2,1,1,3])).shape a = tf.zeros([1,2,1,3])
a.shape
tf.squeeze(a,axis=0).shape
tf.squeeze(a,axis=2).shape
tf.squeeze(a,axis=-2).shape
tf.squeeze(a,axis=-4).shape
吴裕雄--天生自然TensorFlow2教程:维度变换的更多相关文章
- 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度
import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...
- 吴裕雄--天生自然TensorFlow2教程:合并与分割
import tensorflow as tf # 6个班级的学生分数情况 a = tf.ones([4, 35, 8]) b = tf.ones([2, 35, 8]) c = tf.concat( ...
- 吴裕雄--天生自然TensorFlow2教程:Broadcasting
Broadcasting可以理解成把维度分成大维度和小维度,小维度较为具体,大维度更加抽象.也就是小维度针对某个示例,然后让这个示例通用语大维度. import tensorflow as tf x ...
- 吴裕雄--天生自然TensorFlow2教程:Tensor数据类型
list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array,存成一个静态数组,但是numpy在深度学习之前就出现了,所以不适合深度学习 tf.Tensor,为了弥补nump ...
- 吴裕雄--天生自然TensorFlow2教程:手写数字问题实战
import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, ...
- 吴裕雄--天生自然TensorFlow2教程:函数优化实战
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...
- 吴裕雄--天生自然TensorFlow2教程:反向传播算法
- 吴裕雄--天生自然TensorFlow2教程:链式法则
import tensorflow as tf x = tf.constant(1.) w1 = tf.constant(2.) b1 = tf.constant(1.) w2 = tf.consta ...
- 吴裕雄--天生自然TensorFlow2教程:单输出感知机及其梯度
import tensorflow as tf x = tf.random.normal([1, 3]) w = tf.ones([3, 1]) b = tf.ones([1]) y = tf.con ...
随机推荐
- jQuery省市联动(XML/JSON)
准备: 导包 在src下导入c3p0-config.xml 导入JDBCUtil 创建数据库 新建js文件夹导入jQuery配置文件 NO01:创建city.jsp页面 <%@ page lan ...
- 160-PHP 文本替换函数str_replace(一)
<?php $str='Hello world!'; //定义源字符串 $search='o'; //定义将被替换的字符 $replace='O'; //定义替换的字符串 $res=str_re ...
- 【剑指Offer】面试题05.替换空格
题目 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 示例 1: 输入:s = "We are happy." 输出:"We%20are ...
- Codeforces 392 B Blown Garland
题意:输入是由连续的RYGB和字符!组成的字符串,R代表红灯,Y代表黄灯,G代表绿灯,B代表蓝灯.简而言之,就是给定的字符串保证了下标对4取余相同的灯颜色都相同,但是有的地方为‘!’代表这个灯坏了,然 ...
- css文本强制两行超出就显示省略号,不显示省略号
1. 强制一行的情况很简单 overflow:hidden; //超出的隐藏 text-overflow:ellipsis; //省略号 white-space:nowrap; //强制一行显示 2. ...
- BZOJ:2242: [SDOI2011]计算器
题解:BSGS 问题:map空间 BSGS判无解 a%p!=0 0与最小非负整数 有区别 函数传参类型转换int->long long long long ->int; 费马小定理充分必要 ...
- BGP(IBGP“内部路由器”和EBGP“外部路由器”)命令解析
BGP:基于策略的路径向量路由协议. ①:(attribute)属性描述路径. ②:使用TCP(端口179)作为传输协议——(IBGP多使用loopback端口建立update-source) IBG ...
- Spring(5) -(14) pointcut 语法
AOP的规范本应该由SUM公司提出,但是被AOP联盟捷足先登.AOP联盟指定AOP规范,首先就要解决一个问题,怎么表示切入点,也就是在哪些方法上增强(where) AspectJ 是一个面向切面的框架 ...
- ucosiii 学习笔记
OSTaskCreate 的参数中有一个 void *p_ext,这个参数用来传递额外的信息,可以用来传递数组,结构体等等. AppTaskStart --> CPU_Init --> C ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 常量
常量是固定值,在程序执行期间不会改变.这些固定的值,又叫做字面量. 常量可以是任何的基本数据类型,可分为整型数字.浮点数字.字符.字符串和布尔值. 常量就像是常规的变量,只不过常量的值在定义后不能进行 ...