TensorFlow的reshape操作 tf.reshape的实现


Posted in Python onApril 19, 2020

初学tensorflow,如果写的不对的,请更正,谢谢!

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

函数的作用是将tensor变换为参数shape的形式。

其中shape为一个列表形式,特殊的一点是列表中可以存在-1。-1代表的含义是不用我们自己指定这一维的大小,函数会自动计算,但列表中只能存在一个-1。(当然如果存在多个-1,就是一个存在多解的方程了)

好了我想说的重点还有一个就是根据shape如何变换矩阵。其实简单的想就是,

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

首先将矩阵t变为一维矩阵,然后再对矩阵的形式更改就可以了。

官方的例子:

# 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

在举几个例子或许就清楚了,有一个数组z,它的shape属性是(4, 4)

z = np.array([[1, 2, 3, 4],
     [5, 6, 7, 8],
     [9, 10, 11, 12],
     [13, 14, 15, 16]])
z.shape
(4, 4)

z.reshape(-1)

z.reshape(-1)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])

z.reshape(-1, 1)
也就是说,先前我们不知道z的shape属性是多少,但是想让z变成只有一列,行数不知道多少,通过`z.reshape(-1,1)`,Numpy自动计算出有12行,新的数组shape属性为(16, 1),与原来的(4, 4)配套。

z.reshape(-1,1)
 array([[ 1],
    [ 2],
    [ 3],
    [ 4],
    [ 5],
    [ 6],
    [ 7],
    [ 8],
    [ 9],
    [10],
    [11],
    [12],
    [13],
    [14],
    [15],
    [16]])

z.reshape(-1, 2)

newshape等于-1,列数等于2,行数未知,reshape后的shape等于(8, 2)

z.reshape(-1, 2)
 array([[ 1, 2],
    [ 3, 4],
    [ 5, 6],
    [ 7, 8],
    [ 9, 10],
    [11, 12],
    [13, 14],
    [15, 16]])

到此这篇关于TensorFlow的reshape操作 tf.reshape的实现的文章就介绍到这了,更多相关TensorFlow的reshape操作 tf.reshape内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python中关键字is与==的区别简述
Jul 31 Python
Python中利用函数装饰器实现备忘功能
Mar 30 Python
Python读写文件方法总结
Jun 09 Python
python3.x 将byte转成字符串的方法
Jul 17 Python
python使用Matplotlib画条形图
Mar 25 Python
Python 判断奇数偶数的方法
Dec 20 Python
11个Python Pandas小技巧让你的工作更高效(附代码实例)
Apr 30 Python
详解使用python绘制混淆矩阵(confusion_matrix)
Jul 14 Python
多版本python的pip 升级后, pip2 pip3 与python版本失配解决方法
Sep 11 Python
Python类中self参数用法详解
Feb 13 Python
python自动化测试三部曲之unittest框架的实现
Oct 07 Python
Django启动时找不到mysqlclient问题解决方案
Nov 11 Python
pip安装tensorflow的坑的解决
Apr 19 #Python
查看已安装tensorflow版本的方法示例
Apr 19 #Python
在Anaconda3下使用清华镜像源安装TensorFlow(CPU版)
Apr 19 #Python
Django项目uwsgi+Nginx保姆级部署教程实现
Apr 19 #Python
Python如何把Spark数据写入ElasticSearch
Apr 18 #Python
Python virtualenv虚拟环境实现过程解析
Apr 18 #Python
python实现贪吃蛇双人大战
Apr 18 #Python
You might like
php模拟post提交数据的方法
2015/02/12 PHP
php array_key_exists() 与 isset() 的区别
2016/10/24 PHP
Yii框架Session与Cookie使用方法示例
2019/10/14 PHP
PHP程序员简单的开展服务治理架构操作详解(三)
2020/05/14 PHP
PHP实现倒计时功能
2020/11/16 PHP
javascript移出节点removeChild()使用介绍
2014/04/03 Javascript
IE8下Jquery获取select选中的值post到后台报错问题
2014/07/02 Javascript
JS去除iframe滚动条的方法
2015/04/01 Javascript
JS使用正则表达式除去字符串中重复字符的方法
2015/11/05 Javascript
深入浅析JavaScript系列(13):This? Yes,this!
2016/01/05 Javascript
jQuery使用serialize()表单序列化时出现中文乱码问题的解决办法
2016/07/27 Javascript
微信小程序 五星评分(包括半颗星评分)实例代码
2016/12/14 Javascript
详解Vue.js iview实现树形权限表(可扩展表)
2018/09/30 Javascript
详解在vue-test-utils中mock全局对象
2018/11/07 Javascript
图文讲解vue的v-if使用方法
2019/02/11 Javascript
cordova+vue+webapp使用html5获取地理位置的方法
2019/07/06 Javascript
[02:30]联想杯DOTA2完美世界全国高校联赛—北京站现场
2015/11/16 DOTA
Python中的闭包详细介绍和实例
2014/11/21 Python
Python读取环境变量的方法和自定义类分享
2014/11/22 Python
简单学习Python多进程Multiprocessing
2017/08/29 Python
pandas系列之DataFrame 行列数据筛选实例
2018/04/12 Python
小白入门篇使用Python搭建点击率预估模型
2018/10/12 Python
Python3.5内置模块之os模块、sys模块、shutil模块用法实例分析
2019/04/27 Python
Django 用户认证组件使用详解
2019/07/23 Python
tensorflow-gpu安装的常见问题及解决方案
2020/01/20 Python
Python+PyQt5实现灭霸响指功能
2020/05/25 Python
Python尾递归优化实现代码及原理详解
2020/10/09 Python
巴西儿童时尚购物网站:Dinda
2019/08/14 全球购物
Hashtable 添加内容的方式有哪几种,有什么区别?
2012/04/08 面试题
秘书岗位职责
2013/11/18 职场文书
小学科学教学反思
2014/01/26 职场文书
房屋租赁合同解除协议书
2014/10/11 职场文书
2014年班干部工作总结
2014/11/25 职场文书
SpringBoot整合Redis入门之缓存数据的方法
2021/11/17 Redis
在Python 中将类对象序列化为JSON
2022/04/06 Python
python开发制作好看的时钟效果
2022/05/02 Python