TensorFlow2-维度变换
TensorFlow2-维度变换
Outline(大纲)
shape, ndim
reshape
expand_dims/squeeze
transpose
图片视图
- [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
(TensorShape([4, 28, 28, 3]), 4)
tf.reshape(a, [4, 784, 3]).shape # 给出一张图片某个通道的数据,丢失行、宽的信息
TensorShape([4, 784, 3])
tf.reshape(a, [4, -1, 3]).shape # 4*(-1)*3 = 4*28*28*3
TensorShape([4, 784, 3])
tf.reshape(a, [4, 784*3]).shape # 给出一张图片的所有数据,丢失行、宽和通道的信息
TensorShape([4, 2352])
tf.reshape(a, [4, -1]).shape
TensorShape([4, 2352])
Second Reshape(恢复视图)
tf.reshape(tf.reshape(a, [4, -1]), [4, 28, 28, 3]).shape
TensorShape([4, 28, 28, 3])
tf.reshape(tf.reshape(a, [4, -1]), [4, 14, 56, 3]).shape
TensorShape([4, 14, 56, 3])
tf.reshape(tf.reshape(a, [4, -1]), [4, 1, 784, 3]).shape
TensorShape([4, 1, 784, 3])
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
TensorShape([4, 3, 2, 1])
tf.transpose(a).shape
TensorShape([1, 2, 3, 4])
tf.transpose(a, perm=[0, 1, 3, 2]).shape # 按照索引替换维度
TensorShape([4, 3, 1, 2])
a = tf.random.normal([4, 28, 28, 3]) # b,h,w,c
a.shape
TensorShape([4, 28, 28, 3])
tf.transpose(a, [0, 2, 1, 3]).shape # b,2,h,c
TensorShape([4, 28, 28, 3])
tf.transpose(a, [0, 3, 2, 1]).shape # b,c,w,h
TensorShape([4, 3, 28, 28])
tf.transpose(a, [0, 3, 1, 2]).shape # b,c,h,w
TensorShape([4, 3, 28, 28])
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
TensorShape([4, 25, 8])
tf.expand_dims(a, axis=0).shape # 索引0前
TensorShape([1, 4, 25, 8])
tf.expand_dims(a, axis=3).shape # 索引3前
TensorShape([4, 25, 8, 1])
tf.expand_dims(a,axis=-1).shape # 索引-1后
TensorShape([4, 25, 8, 1])
tf.expand_dims(a,axis=-4).shape # 索引-4后,即左边空白处
TensorShape([1, 4, 25, 8])
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
TensorShape([2, 3])
a = tf.zeros([1,2,1,3])
a.shape
TensorShape([1, 2, 1, 3])
tf.squeeze(a,axis=0).shape
TensorShape([2, 1, 3])
tf.squeeze(a,axis=2).shape
TensorShape([1, 2, 3])
tf.squeeze(a,axis=-2).shape
TensorShape([1, 2, 3])
tf.squeeze(a,axis=-4).shape
TensorShape([2, 1, 3])
TensorFlow2-维度变换的更多相关文章
- [深度学习] pytorch学习笔记(1)(数据类型、基础使用、自动求导、矩阵操作、维度变换、广播、拼接拆分、基本运算、范数、argmax、矩阵比较、where、gather)
一.Pytorch安装 安装cuda和cudnn,例如cuda10,cudnn7.5 官网下载torch:https://pytorch.org/ 选择下载相应版本的torch 和torchvisio ...
- tensor维度变换
维度变换是tensorflow中的重要模块之一,前面mnist实战模块我们使用了图片数据的压平操作,它就是维度变换的应用之一. 在详解维度变换的方法之前,这里先介绍一下View(视图)的概念.所谓Vi ...
- pytorch张量数据索引切片与维度变换操作大全(非常全)
(1-1)pytorch张量数据的索引与切片操作1.对于张量数据的索引操作主要有以下几种方式:a=torch.rand(4,3,28,28):DIM=4的张量数据a(1)a[:2]:取第一个维度的前2 ...
- 吴裕雄--天生自然TensorFlow2教程:维度变换
图片视图 [b, 28, 28] # 保存b张图片,28行,28列(保存数据一般行优先),图片的数据没有被破坏 [b, 28*28] # 保存b张图片,不考虑图片的行和列,只保存图片的数据,不关注图片 ...
- 机器学习进阶-图像基本操作-数值计算 1.cv2.add(将图片进行加和) 2.cv2.resize(图片的维度变换) 3.cv2.addWeighted(将图片按照公式进行重叠操作)
1.cv2.add(dog_img, cat_img) # 进行图片的加和 参数说明: cv2.add将两个图片进行加和,大于255的使用255计数 2.cv2.resize(img, (500, ...
- TensorFlow2.0(1):基本数据结构—张量
1 引言 TensorFlow2.0版本已经发布,虽然不是正式版,但预览版都发布了,正式版还会远吗?相比于1.X,2.0版的TensorFlow修改的不是一点半点,这些修改极大的弥补了1.X版本的反人 ...
- TensorFlow2教程(目录)
第一篇 基本操作 01 Tensor数据类型 02 创建Tensor 03 Tensor索引和切片 04 维度变换 05 Broadcasting 06 数学运算 07 前向传播(张量)- 实战 第二 ...
- TensorFlow2.0使用方法
TensorFlow2.0 1 使用技巧 更新到最新版本: pip install --upgrade tensorflow pip install --upgrade tensorflow-gpu ...
- 深度学习原理与框架-猫狗图像识别-卷积神经网络(代码) 1.cv2.resize(图片压缩) 2..get_shape()[1:4].num_elements(获得最后三维度之和) 3.saver.save(训练参数的保存) 4.tf.train.import_meta_graph(加载模型结构) 5.saver.restore(训练参数载入)
1.cv2.resize(image, (image_size, image_size), 0, 0, cv2.INTER_LINEAR) 参数说明:image表示输入图片,image_size表示变 ...
- ndarray数组变换
import numpy as np 维度变换 a = np.arange(24) a array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ...
随机推荐
- AOP日志框架实现
AOP日志框架实现 JDK动态代理实现日志框架 首先,在项目包com.ay.test 下创建业务接口类BusinessClassService,具体代码如下: BusinessC lassServic ...
- 我的周记6——"不破楼兰誓不还“
周6 2019-06-13 天气:雨 这张照片是我那天中午回家的时候拍的,下着中雨.这是谁的孩子,又是谁的妈妈.看到这样的场景心里还是蛮有触动的, 因为我现在是离家千里的孩子,思念着故乡也想念着亲人 ...
- 组合数的几种球阀 By cellur925
先来了解几个概念:排列数,组合数. 一.定义及有用的性质 排列数:从n个不同元素中依次取出m个元素排成一列的方案数.P(n,m)=n!/(n-m)! 组合数:从n个不同元素中依次取出m个元素形成一个集 ...
- Unix\Linux | 总结笔记 | man帮助
0.目录 手册页分类说明 man手册中的段落说明 1. man手册页分类 man1 普通用户可以执行的命令帮助 man2 系统调用.内核函数的说明帮助 man3 库函数说明帮助 ma ...
- Sgu294He's Circles
Description 有一个长度为N的环,上面写着'X'和'E',问本质不同的环有多少种.(N不超过200000). Input The input file contains a single i ...
- fastboot命令详解
Android手机分区(每个分区都有相应的img文件对应):开机启动画面区(splash1),数据恢复区(recovery),内核区(boot), 系统区(system),数据缓存区(cache),用 ...
- 443 String Compression 压缩字符串
给定一组字符,使用原地算法将其压缩.压缩后的长度必须始终小于或等于原数组长度.数组的每个元素应该是长度为1 的字符(不是 int 整数类型).在完成原地修改输入数组后,返回数组的新长度.进阶:你能否仅 ...
- 191 Number of 1 Bits 位1的个数
编写一个函数,输入是一个无符号整数,返回的是它所有 位1 的个数(也被称为汉明重量).例如,32位整数 '11' 的二进制表示为 00000000000000000000000000001011,所以 ...
- Snort里如何将一个tcpdump格式的二进制文件读取打印到屏幕上(图文详解)
不多说,直接上干货! 关于tcpdump二进制格式,这个基本概念不说. 支持tcpdump二进制格式的嗅探器工具,这里我说两个:tcpdump或者ethereal. [root@datatest Se ...
- js插件之Ocupload
使用方法: var myUpload = $(element).upload({ name: 'file', action: '', enctype: 'multipart/form-data', p ...