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-维度变换的更多相关文章

  1. [深度学习] pytorch学习笔记(1)(数据类型、基础使用、自动求导、矩阵操作、维度变换、广播、拼接拆分、基本运算、范数、argmax、矩阵比较、where、gather)

    一.Pytorch安装 安装cuda和cudnn,例如cuda10,cudnn7.5 官网下载torch:https://pytorch.org/ 选择下载相应版本的torch 和torchvisio ...

  2. tensor维度变换

    维度变换是tensorflow中的重要模块之一,前面mnist实战模块我们使用了图片数据的压平操作,它就是维度变换的应用之一. 在详解维度变换的方法之前,这里先介绍一下View(视图)的概念.所谓Vi ...

  3. pytorch张量数据索引切片与维度变换操作大全(非常全)

    (1-1)pytorch张量数据的索引与切片操作1.对于张量数据的索引操作主要有以下几种方式:a=torch.rand(4,3,28,28):DIM=4的张量数据a(1)a[:2]:取第一个维度的前2 ...

  4. 吴裕雄--天生自然TensorFlow2教程:维度变换

    图片视图 [b, 28, 28] # 保存b张图片,28行,28列(保存数据一般行优先),图片的数据没有被破坏 [b, 28*28] # 保存b张图片,不考虑图片的行和列,只保存图片的数据,不关注图片 ...

  5. 机器学习进阶-图像基本操作-数值计算 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, ...

  6. TensorFlow2.0(1):基本数据结构—张量

    1 引言 TensorFlow2.0版本已经发布,虽然不是正式版,但预览版都发布了,正式版还会远吗?相比于1.X,2.0版的TensorFlow修改的不是一点半点,这些修改极大的弥补了1.X版本的反人 ...

  7. TensorFlow2教程(目录)

    第一篇 基本操作 01 Tensor数据类型 02 创建Tensor 03 Tensor索引和切片 04 维度变换 05 Broadcasting 06 数学运算 07 前向传播(张量)- 实战 第二 ...

  8. TensorFlow2.0使用方法

    TensorFlow2.0 1 使用技巧 更新到最新版本: pip install --upgrade tensorflow pip install --upgrade tensorflow-gpu ...

  9. 深度学习原理与框架-猫狗图像识别-卷积神经网络(代码) 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表示变 ...

  10. 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 ...

随机推荐

  1. poj 3130 How I Mathematician Wonder What You Are! 【半平面交】

    求多边形的核,直接把所有边求半平面交判断有无即可 #include<iostream> #include<cstdio> #include<algorithm> # ...

  2. bzoj 3456: 城市规划【NTT+多项式求逆】

    参考:http://blog.miskcoo.com/2015/05/bzoj-3456 首先推出递推式(上面的blog讲的挺清楚的),大概过程是正难则反,设g为n个点的简单(无重边无自环)无向图数目 ...

  3. echarts 实例demo 详细讲解

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     & ...

  4. SPOJ GSS3 线段树系列1

    SPOJ GSS系列真是有毒啊! 立志刷完,把线段树搞完! 来自lydrainbowcat线段树上的一道例题.(所以解法参考了lyd老师) 题意翻译 n 个数, q 次操作 操作0 x y把 Ax 修 ...

  5. 面试基础试题 一 http和HTTPS的区别

    作为老生常谈的问题,我主要给出我自己的理解和结合大牛的叙述的综合看法,来检验自己学习 超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何 ...

  6. IE6 position:fixed bug hack方式

    /* IE6浏览器的特有方法 */ /* 修正IE6振动bug */ * html,* html body{background-image:url(about:blank);background-a ...

  7. Matlab实现Hough变换检测图像中的直线 分类: 图像处理 2014-06-14 22:07 641人阅读 评论(0) 收藏

    Hough变换的原理: 将图像从图像空间变换至参数空间,变换公式如下: 变换以后,图像空间与参数空间存在以下关系: 图像空间中的一点在参数空间是一条曲线,而图像空间共线的各点对应于参数空间交于一点的各 ...

  8. Service官方教程(6)Bound Services主要用来实现通信服务,以及3种实现通信的方案简介。

    1.Bound Services A bound service is the server in a client-server interface. A bound service allows ...

  9. 微信小程序组件解读和分析:八、checkbox复选项

    checkbox复选项组件说明: checkbox是小程序表单组件中的一个组件,作用是在表单中引导用户做出选择. 要使用checkbox组件,还需要在同组中所有的checkbox标签外使用checkb ...

  10. Visual SVN自动给文件加锁

    在使用SVN作为版本控制器的时候,在VS里安装VISUALSVN插件,当修改文件公共文件的时候需要先Get Lock,如果对于多次操作这个鼠标操作显得是一些复杂,自动给文件加锁的操作实际是给文件加一个 ...