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使用socket进行简单网络连接的方法
Apr 29 Python
python获得一个月有多少天的方法
Jun 04 Python
Django中URL视图函数的一些高级概念介绍
Jul 20 Python
python实现读Excel写入.txt的方法
Apr 29 Python
对python opencv 添加文字 cv2.putText 的各参数介绍
Dec 05 Python
Python OpenCV利用笔记本摄像头实现人脸检测
Aug 20 Python
python判断变量是否为int、字符串、列表、元组、字典的方法详解
Feb 13 Python
python GUI库图形界面开发之PyQt5布局控件QGridLayout详细使用方法与实例
Mar 06 Python
Python 统计位数为偶数的数字代码详解
Mar 15 Python
python 决策树算法的实现
Oct 09 Python
python中print格式化输出的问题
Apr 16 Python
Pytorch DataLoader shuffle验证方式
Jun 02 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
Discuz批量替换帖子内容的方法(使用SQL更新数据库)
2014/06/23 PHP
取得单条网站评论以数组形式进行输出
2014/07/28 PHP
基于jQuery的js分页代码
2010/06/10 Javascript
jquery判断RadioButtonList和RadioButton中是否有选中项示例
2013/09/29 Javascript
jquery checkbox实现单选小例
2013/11/27 Javascript
jQuery中对未来的元素绑定事件用bind、live or on
2014/04/17 Javascript
js与css实现弹出层覆盖整个页面的方法
2014/12/13 Javascript
angularJS提交表单(form)
2015/02/09 Javascript
实现前后端数据交互方法汇总
2015/04/07 Javascript
JavaScript SHA512&SHA256加密算法详解
2015/08/11 Javascript
实例解析Array和String方法
2016/12/14 Javascript
vue3.0 CLI - 3.2 路由的初级使用教程
2018/09/20 Javascript
详解vue使用插槽分发内容slot的用法
2019/03/28 Javascript
jQuery实现弹幕特效
2019/11/29 jQuery
[48:47]VGJ.S vs NB 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
Python使用defaultdict读取文件各列的方法
2017/05/11 Python
python的set处理二维数组转一维数组的方法示例
2019/05/31 Python
Pycharm如何打断点的方法步骤
2019/06/13 Python
django 配置阿里云OSS存储media文件的例子
2019/08/20 Python
Python利用逻辑回归模型解决MNIST手写数字识别问题详解
2020/01/14 Python
Python短信轰炸的代码
2020/03/25 Python
Django的ListView超详细用法(含分页paginate)
2020/05/21 Python
通过cmd进入python的步骤
2020/06/16 Python
基于tensorflow for循环 while循环案例
2020/06/30 Python
Python urllib3软件包的使用说明
2020/11/18 Python
html5 worker 实例(一) 为什么测试不到效果
2013/06/24 HTML / CSS
智利最大的网上商店:Linio智利
2016/11/24 全球购物
员工自我鉴定
2013/10/09 职场文书
求职自荐信范文格式
2013/11/29 职场文书
电子技术专业中专生的自我评价
2013/12/17 职场文书
大学生英语演讲稿
2014/04/24 职场文书
体育课课后反思
2014/04/24 职场文书
一年级语文下册复习计划
2015/01/17 职场文书
干部培训工作总结2015
2015/05/25 职场文书
信仰纪录片观后感
2015/06/08 职场文书
《地。-关于地球的运动-》单行本第七集上市,小说家朝井辽献上期待又害怕的推荐文
2022/03/31 日漫