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里大整数相乘相关技巧指南
Sep 12 Python
学习python类方法与对象方法
Mar 15 Python
详解使用python crontab设置linux定时任务
Dec 08 Python
Python使用QQ邮箱发送Email的方法实例
Feb 09 Python
Python模拟用户登录验证
Sep 11 Python
python使用Pycharm创建一个Django项目
Mar 05 Python
Python使用tkinter库实现文本显示用户输入功能示例
May 30 Python
Python实现二叉树前序、中序、后序及层次遍历示例代码
May 18 Python
Golang GBK转UTF-8的例子
Aug 26 Python
浅谈python3打包与拆包在函数的应用详解
May 02 Python
详解python实现可视化的MD5、sha256哈希加密小工具
Sep 14 Python
Python可变集合和不可变集合的构造方法大全
Dec 06 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
全国FM电台频率大全 - 27 陕西省
2020/03/11 无线电
php中时间函数date及常用的时间计算
2017/05/12 PHP
PHP实现转盘抽奖算法分享
2020/04/15 PHP
JavaScript CSS菜单功能 改进版
2008/12/20 Javascript
如何使用Javascript正则表达式来格式化XML内容
2013/07/04 Javascript
jQuery截取指定长度字符串代码
2014/08/21 Javascript
javascript常用的方法分享
2015/07/01 Javascript
安装使用Mongoose配合Node.js操作MongoDB的基础教程
2016/03/01 Javascript
怎样判断jQuery当前元素是隐藏还是显示
2016/11/23 Javascript
微信小程序 支付简单实例及注意事项
2017/01/06 Javascript
深究AngularJS——ng-checked(回写:带真实案例代码)
2017/06/13 Javascript
React Native实现进度条弹框的示例代码
2017/07/17 Javascript
js实现拖拽上传图片功能
2017/08/01 Javascript
AngularJS 实现购物车全选反选功能
2017/10/24 Javascript
JS实现的数组去除重复数据算法小结
2017/11/17 Javascript
Angular实现的进度条功能示例
2018/02/18 Javascript
Vue filter介绍及详细使用
2018/04/04 Javascript
Bootstrap Paginator+PageHelper实现分页效果
2018/12/29 Javascript
JS正则表达式验证端口范围(0-65535)
2020/01/06 Javascript
Vue双向绑定实现原理与方法详解
2020/05/07 Javascript
python 自动化将markdown文件转成html文件的方法
2016/09/23 Python
python人民币小写转大写辅助工具
2018/06/20 Python
在Pycharm中对代码进行注释和缩进的方法详解
2019/01/20 Python
详解Python Qt的窗体开发的基本操作
2019/07/14 Python
python飞机大战pygame游戏框架搭建操作详解
2019/12/17 Python
浅谈Python中的继承
2020/06/19 Python
澳大利亚优质葡萄酒专家:Vintage Cellars
2019/01/08 全球购物
八一建军节部队活动方案
2014/02/04 职场文书
十八届三中全会感言
2014/03/10 职场文书
求职意向书
2014/04/01 职场文书
政府绩效管理实施方案
2014/05/04 职场文书
篮球比赛策划方案
2014/06/05 职场文书
平面设计师岗位职责
2014/09/18 职场文书
党支部创先争优公开承诺书
2015/04/30 职场文书
python 爬取华为应用市场评论
2021/05/29 Python
如何用python清洗文件中的数据
2021/06/18 Python