在处理图像数据的时候总会遇到输入图像的维数不符合的情况,此时tensorflow中reshape()就很好的解决了这个问题。 
更为详细的可以参考官方文档说明: 
numpy.reshape


reshape()的括号中所包含的参数有哪些呢?常见的写法有tf.reshape((28,28)):

tf.reshape(tensor,shape,name=None)

函数的作用是将tensor变换为参数shape形式,其中的shape为一个列表形式,特殊的是列表可以实现逆序的遍历,即list(-1).-1所代表的含义是我们不用亲自去指定这一维的大小,函数会自动进行计算,但是列表中只能存在一个-1。(如果存在多个-1,就是一个存在多解的方程) 
下面就说一下reshape是如何进行矩阵的变换的,其简单的流程就是:

将矩阵t变换为一维矩阵,然后再对矩阵的形式进行更改就好了,具体的流程如下:

reshape(t,shape) =>reshape(t,[-1]) =>reshape(t,shape)

实际操作中,有如下效果:我创建了一个一维的数组

>>>import numpy as np
>>>a= np.array([1,2,3,4,5,6,7,8])
>>>a
array([1,2,3,4,5,6,7,8])
>>>

使用reshape()方法来更改数组的形状,使得数组成为一个二维的数组:(数组中元素的个数是2×4=8)

>>>d = a.reshape((2,4))
>>>d
array([[1, 2, 3, 4],
[5, 6, 7, 8]])

进一步提升,可以得到一个三维的数组f:(注意数组中元素的个数时2×2×2=8)

>>>f = a.reshape((2,2,2))
>>>f
array([[[1, 2],
[3, 4]], [[5, 6],
[7, 8]]])

注意:形状发生变化的原则时数组元素的个数是不能发生改变的,比如像下面这样的写法就会报错:

(元素的个数是2×2=4,所以会报错)

>>> e = a.shape((2,2))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable

-1 的应用:-1 表示不知道该填什么数字合适的情况下,可以选择,由python通过a和其他的值3推测出来,比如,这里的a 是二维的数组,数组中共有6个元素,当使用reshape()时,6/3=2,所以形成的是3行2列的二维数组,可以看出,利用reshape进行数组形状的转换时,一定要满足(x,y)中x×y=数组的个数。

>>>a = np.array([[1,2,3],[4,5,6]])
>>>np.reshape(a,(3,-1))
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.reshape(a,(1,-1))
array([[1, 2, 3, 4, 5, 6]])
>>> np.reshape(a,(6,-1))
array([[1],
[2],
[3],
[4],
[5],
[6]])
>>> np.reshape(a,(-1,1))
array([[1],
[2],
[3],
[4],
[5],
[6]])

下面是两张2×3大小的图片(不知道有几张图片可以用-1代替),如何把所有二维照片给转换成一维的,请看以下三维的数组:

>>>image = np.array([[[1,2,3], [4,5,6]], [[1,1,1], [1,1,1]]])
>>>image.shape
(2,2,3)
>>>image.reshape((-1,6))
array([[1, 2, 3, 4, 5, 6],
[1, 1, 1, 1, 1, 1]])
>>> a = image.reshape((-1,6))
>>> a.reshape((-1,12))
array([[1, 2, 3, 4, 5, 6, 1, 1, 1, 1, 1, 1]])
a.reshape((12,-1))
array([[1],
[2],
[3],
[4],
[5],
[6],
[1],
[1],
[1],
[1],
[1],
[1]])
>>> a.reshape([-1])
array([1, 2, 3, 4, 5, 6, 1, 1, 1, 1, 1, 1])

通过reshape生成的新的形状的数组和原始数组共用一个内存,所以一旦更改一个数组的元素,另一个数组也将会发生改变。

>>>a[1] = 100
>>>a
array([ 1, 100, 3, 4, 5, 6, 7, 8])
>>> d
array([[ 1, 100, 3, 4],
[ 5, 6, 7, 8]])

最后再给大家呈现一下官方给出的例子:

# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]] # tensor 't' is [[[1, 1], [2, 2]],
# [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2],
[3, 3, 4, 4]] # tensor 't' is [[[1, 1, 1],
# [2, 2, 2]],
# [[3, 3, 3],
# [4, 4, 4]],
# [[5, 5, 5],
# [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6] # -1 can also be used to infer the shape # -1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]] # tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape(t, []) ==> 7

tensorflow的reshape操作tf.reshape()的更多相关文章

  1. tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask

    1.  tf.split(3, group, input)  # 拆分函数    3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...

  2. TensoFlow的tf.reshape()

    tf.reshape(tensor,shape,name=None) 函数的作用是将tensor变换为参数shape形式,其中的shape为一个列表形式,特殊的是列表可以实现逆序的遍历,即list(- ...

  3. TF-调整矩阵维度 tf.reshape 介绍

    函数原型为 def reshape(tensor, shape, name=None) 第1个参数为被调整维度的张量. 第2个参数为要调整为的形状. 返回一个shape形状的新tensor 注意sha ...

  4. tensorflow生成随机数的操作 tf.random_normal & tf.random_uniform & tf.truncated_normal & tf.random_shuffle

    tf.random_normal 从正态分布输出随机值. random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name ...

  5. tf.reshape

    tf.reshape(tensor, shape, name=None) 其中,tensor是向量,或者说矩阵 shape是转换后的向量,或者转换后的矩阵形状 [2,1]转换成二行一列 [2,-1]转 ...

  6. 图融合之加载子图:Tensorflow.contrib.slim与tf.train.Saver之坑

    import tensorflow as tf import tensorflow.contrib.slim as slim import rawpy import numpy as np impor ...

  7. 跟我学算法- tensorflow 实现RNN操作

    对一张图片实现rnn操作,主要是通过先得到一个整体,然后进行切分,得到的最后input结果输出*_w[‘out’] + _b['out']  = 最终输出结果 第一步: 数据载入 import ten ...

  8. 吴裕雄 python 神经网络——TensorFlow 数据集高层操作

    import tempfile import tensorflow as tf train_files = tf.train.match_filenames_once("E:\\output ...

  9. tensorflow的tfrecord操作代码与数据协议规范

    tensorflow的数据集可以说是非常重要的部分,我认为人工智能就是数据加算法,数据没处理好哪来的算法? 对此tensorflow有一个专门管理数据集的方式tfrecord·在训练数据时提取图片与标 ...

随机推荐

  1. Java第二阶段之常用类

    包装类缓存-128到127之间的数字(系统初始的时候就创建了,当我们调用ValueOf时,首先检查是否在范围内,在则直接取用)integer in1 = integer.valueOf(-128):i ...

  2. zabbix agentd错误日志解决办法

    公司新上了一台服务器,我安装了zabbix_agents软件包,并复制了zabbix server端的zabbix_agentd.conf到/etc/zabbix里面并修改了相关的参数,并启动了zab ...

  3. jmeter接口测试与接口测试工具

    接口测试与接口测试工具 1,什么是接口? 接口,Application Programming Interface(API) 通俗的讲 就是HTTP请求 2,什么是接口测试? 接口测试测试组件间接口的 ...

  4. HashMap与HashTable的哈希算法——JDK1.9源码阅读总结

    下面是HashTable源码中的put方法: 注意上面注释标注的地方: HashTable对于元素在哈希表中的坐标算法是: 将对象自身的哈希值key.hashCode()变为正数:hash & ...

  5. php面试专题---MySQL分区

    php面试专题---MySQL分区 一.总结 一句话总结: mysql的分区操作还比较简单,好处是也不用自己动手建表进行分区,和水平分表有点像 1.mysql分区简介? 一个表或索引-->N个物 ...

  6. MySQL部分索引

    部分索引 char/varchar2太长,全部做索引的话,效率低,浪费存储空间 select avg(length(username)) from 索引统计: show index from tabl ...

  7. 双轴按键摇杆控制器控制TFTLCD(使用ADC1双通道DMA传输)

    实验使用如下所示的双轴按键摇杆控制器,来控制TFTLCD上显示的直线.首先介绍一下双轴按键摇杆控制器.原理:十字摇杆为一个双向的10K电阻器,随着摇杆方向不同,抽头的阻值随着变化.本模块使用5V供电( ...

  8. ubuntu搭建jdk+jenkins

    第一步,安装jdk(如果已安装,直接进行第二步) 1.下载 jdk-8u172-linux-x64.tar.gz       点此下载   2.解压 tar -zxvf jdk-8u172-linux ...

  9. linux下rpm包安装、配置和卸载mysq

    l   WIN10下虚拟机:VMware workstation 12 PRO 安装 # 1.查看系统版本 [root@vm-xiluhua][/home/xiluhua]$ cat /etc/red ...

  10. HTML--JS 9*9乘法口诀

    <html> <head> <title>9*9乘法口诀</title> <script language="JavaScript&qu ...