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
redis之django-redis的简单缓存使用
Jun 07 Python
python仿evething的文件搜索器实例代码
May 13 Python
python 随机森林算法及其优化详解
Jul 11 Python
Python内置方法实现字符串的秘钥加解密(推荐)
Dec 09 Python
Python测试Kafka集群(pykafka)实例
Dec 23 Python
Python单元测试模块doctest的具体使用
Feb 10 Python
Python pyautogui模块实现鼠标键盘自动化方法详解
Feb 17 Python
Python如何省略括号方法详解
Mar 21 Python
Java byte数组操纵方式代码实例解析
Jul 22 Python
python help函数实例用法
Dec 06 Python
利用Python如何画一颗心、小人发射爱心
Feb 21 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 file_get_contents抓取Gzip网页乱码的三种解决方法
2013/11/12 PHP
php如何修改SESSION的生存存储时间的实例代码
2017/07/05 PHP
PHP mysqli事务操作常用方法分析
2017/07/22 PHP
Laravel框架创建路由的方法详解
2019/09/04 PHP
Extjs的FileUploadField文件上传出现了两个上传按钮
2014/04/29 Javascript
js操作XML文件的实现方法兼容IE与FireFox
2016/06/25 Javascript
js中window.open的参数及注意注意事项
2016/07/06 Javascript
jQuery实现的无限级下拉菜单功能示例
2016/09/12 Javascript
详解Nodejs之静态资源处理
2017/06/05 NodeJs
基于对象合并功能的实现示例
2017/10/10 Javascript
通过jquery toggleClass()属性制作文章段落更改背景颜色
2018/05/21 jQuery
vue 父组件中调用子组件函数的方法
2019/06/06 Javascript
微信小程序下拉框搜索功能的实现方法
2019/07/31 Javascript
微信小程序分包加载代码实现方法详解
2019/09/23 Javascript
在vue中使用echars实现上浮与下钻效果
2019/11/08 Javascript
简单介绍Python的Django框架的dj-scaffold项目
2015/05/30 Python
python编程测试电脑开启最大线程数实例代码
2018/02/09 Python
Python学生信息管理系统修改版
2018/03/13 Python
Tensorflow中的placeholder和feed_dict的使用
2018/07/09 Python
Python绘制正余弦函数图像的方法
2018/08/28 Python
Python控制Firefox方法总结
2019/06/03 Python
浅谈python多进程共享变量Value的使用tips
2019/07/16 Python
python库matplotlib绘制坐标图
2019/10/18 Python
python3使用Pillow、tesseract-ocr与pytesseract模块的图片识别的方法
2020/02/26 Python
基于matplotlib中ion()和ioff()的使用详解
2020/06/16 Python
Django filter动态过滤与排序实现过程解析
2020/11/26 Python
西班牙英格列斯百货官网:El Corte Inglés
2016/09/25 全球购物
英国天然宝石首饰购买网站:Gemondo Jewellery
2018/10/23 全球购物
小米乌克兰网上商店:Xiaomi.UA
2019/10/29 全球购物
西班牙购买隐形眼镜、眼镜和太阳镜网站:Lentiamo.es
2020/06/11 全球购物
包装类的功能、种类、常用方法
2012/01/27 面试题
资深地理教师自我评价
2013/09/21 职场文书
酒店圣诞节活动总结
2015/05/06 职场文书
2015年体检中心工作总结
2015/05/27 职场文书
新学期开学标语2015
2015/07/16 职场文书
MySQL 如何分析查询性能
2021/05/12 MySQL