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 ...
随机推荐
- Codeforces 908F - New Year and Rainbow Roads 【脑洞】
题意:在一条数轴上有若干'R','B','G'点,连接两个点的代价是位置差,要求使用最小代价使得除去所有'R'点后剩下的所有点联通,除去所有'B'点后剩下的所有点联通. 还以为会是什么最小生成树,结果 ...
- DBMS "无法作为数据库主体执行,因为主体“dbo”不存在、无法模拟这种..........”
解决方案: 新附加的数据库需要设置所有者才能建立数据库关系图.供参考的操作步骤如下: 选择“AdventureWorks2012LT”,右键,选择“属性”,选择“文件”页,点击“所有者”右侧按钮,点击 ...
- _bzoj1036 [ZJOI2008]树的统计Count【树链剖分】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 保存模版. 执行qmax与qsum操作,往上爬的时候最开始的代码出了点小问题,往上爬的 ...
- Acitivty四种启动模式
Acitivty的四种启动模式 在清单文件中声明 Activity 时,您可以使用 <activity> 元素的 launchMode 属性指定 Activity 应该如何与任务关联. l ...
- 转-解决Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'问题
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) Red Hat Enterpr ...
- mysql各个版本下载地址
之所记录下来是因为我找了好久才找到:这下记着了:http://downloads.mysql.com/archives/community/ 希望对没有找到的朋友有帮助
- [转]在 Azure 云服务上设计大规模服务的最佳实践
本文转自:http://technet.microsoft.com/zh-cn/magazine/jj717232.aspx 英文版:http://msdn.microsoft.com/library ...
- hihocoder 神奇字符串
思路: 暴力,模拟. 实现: #include <iostream> #include <algorithm> #include <cstdio> #include ...
- 架构师细说 NGINX 的配置及优化
最近感觉很多东西在运用到一定的程度之后,会发现原来是自己了解到的不够.一方面限于实际运用到的不多,一方面可能是因为一开始没有进行全面认识.遂这里搜集整理了一番NGINX. 一.nginx启动和关闭 c ...
- hibernate 批量插入数据
如题,有两种方法 1)使用FLUSH 2)使用JDBC 分别来解释: 1)hibernate在进行数据库操作的时候,都要有事务支持的.可能你曾遇到过,没有加事务,程序会报错的情况. 而事务每次提交的时 ...