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. app 后台程序设计

    限制客户端一分钟之内访问接口的次数 1.设备的唯一标识获取这个实际上IOS7后会存在问题,权限已经收回了,android可以2.唯一标识可以通过生成一个token区分3.每分钟的频率可以这样设置 ke ...

  2. 在JS/jQuery中,怎么触发input的keypress/keydown/keyup事件?

    怎么触发keypress/keydown/keyup事件? 问题: 1.在之前的写的input后面添加了搜索按钮 2.input只有keyup事件,如下: $("#desktop_folde ...

  3. 实现strcat功能

    实现两个字符串相连 1 #include<stdio.h> #include<string.h> //把源字符串连接到目的字符串中 char *strcat1(char *de ...

  4. Golang bash脚本自动创建一个go工程

    原作者的代码里面,存在智能引号问题,所以他的代码并不能正常运行: 这个是我微调后的版本. 代码如下: #!/bin/bash #————————————– # Module : mk_go_pro.s ...

  5. 深入理解Android

    http://blog.csdn.net/innost/article/details/47254381

  6. 字体使用sp、dp的区别

    Android设置字体大小, 该用sp还是dp? 大部分人肯定脱口而出, 用sp啊, 傻瓜都知道要用sp而不是dp!!! 那么为什么呢? 可能有人会说, 是google官方专门定义了sp这个单位来描述 ...

  7. 题解报告:hdu 1261 字串数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1261 Problem Description 一个A和两个B一共可以组成三种字符串:"ABB ...

  8. 在Eclipse+ADT中开发Android系统的内置应用

    转自:  http://www.iteye.com/topic/1050439 在Eclipse+ADT中开发Android系统的内置应用 Android系统内置有:Browser(浏览器).Mms( ...

  9. IIS7 网站发布

    选择“网站” 添加网站

  10. axis2与eclipse的整合:开始一个简单的axis2 的demo

    1.下载axis2,现在axis2最新版本是axis2-1.6.2,下载地址:http://axis.apache.org/axis2/java/core/download.cgi 2.下载好的zip ...