浅谈tensorflow使用张量时的一些注意点tf.concat,tf.reshape,tf.stack


Posted in Python onJune 23, 2020

有一段时间没用tensorflow了,现在跑实验还是存在一些坑了,主要是关于张量计算的问题。tensorflow升级1.0版本后与以前的版本并不兼容,可能出现各种奇奇怪怪的问题。

1 tf.concat函数

tensorflow1.0以前函数用法:tf.concat(concat_dim, values, name='concat'),第一个参数为连接的维度,可以将几个向量按指定维度连接起来。

如:

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
#按照第0维连接
tf.concat(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
#按照第1维连接
tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

tf.concat的作用主要是将向量按指定维连起来,其余维度不变;而1.0版本以后,函数的用法变成:

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
#按照第0维连接
tf.concat( [t1, t2],0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
#按照第1维连接
tf.concat([t1, t2],1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

位置变了,需要注意。

2 tf.stack函数

用法:stack(values, axis=0, name=”stack”):

“”“Stacks a list of rank-R tensors into one rank-(R+1) tensor.

x = tf.constant([1, 4])
y = tf.constant([2, 5])
z = tf.constant([3, 6])
tf.stack([x,y,z]) ==> [[1,4],[2,5],[3,6]]
tf.stack([x,y,z],axis=0) ==> [[1,4],[2,5],[3,6]]
tf.stack([x,y,z],axis=1) ==> [[1, 2, 3], [4, 5, 6]]

tf.stack将一组R维张量变为R+1维张量。注意:tf.pack已经变成了tf.stack

3.tf.reshape

用法:reshape(tensor, shape, name=None):主要通过改变张量形状,可以从高维变低维,也可以从低维变高维;

a = tf.Variable(initial_value=[[1,2,3],[4,5,6]]) ==> shape:[2,3]
b = tf.Variable(initial_value=[[[1,2,3],[4,5,6]],[[7,8,9],[1,0,2]]]) ==> shape:[2,2,3]

a_1 = tf.reshape(a,[2,1,1,3]) ==> [[[[1,2,3]]],[[[4,5,6]]]]
a_2 = tf.reshape(a,[2,1,3]) ==> [[[1,2,3]],[[4,5,6]]]
b_1 = tf.reshape(b,[2,2,1,3]) ==> [[[[1,2,3]],[[4,5,6]]],[[[7,8,9]],[[1,0,2]]]]

new_1 = tf.concat([b_1,a_1],1)
new_2 = tf.reshape(tf.concat([b,a_2],1),[2,3,1,3])
"""
new_1:
[[[[1 2 3]]

 [[4 5 6]]

 [[1 2 3]]]


 [[[7 8 9]]

 [[1 0 2]]

 [[4 5 6]]]]
new_2;
[[[[1 2 3]]

 [[4 5 6]]

 [[1 2 3]]]


 [[[7 8 9]]

 [[1 0 2]]

 [[4 5 6]]]]

补充知识:tensorflow中的reshape(tensor,[1,-1])和reshape(tensor,[-1,1])

和python 中的reshape用法应该一样

import tensorflow as tf
a = [[1,2],[3,4],[5,6]]
tf.reshape(a,[-1,1])
Out[13]: <tf.Tensor 'Reshape_4:0' shape=(6, 1) dtype=int32>
tf.reshape(tf.reshape(a,[-1,1]),[1,-1])
Out[14]: <tf.Tensor 'Reshape_6:0' shape=(1, 6) dtype=int32>

tf.reshape(tensor,[-1,1])将张量变为一维列向量

tf.reshape(tensor,[1,-1])将张量变为一维行向量

以上这篇浅谈tensorflow使用张量时的一些注意点tf.concat,tf.reshape,tf.stack就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
简单的抓取淘宝图片的Python爬虫
Dec 25 Python
使用Python的Tornado框架实现一个一对一聊天的程序
Apr 25 Python
Python实现简单的多任务mysql转xml的方法
Feb 08 Python
python处理Excel xlrd的简单使用
Sep 12 Python
详解python异步编程之asyncio(百万并发)
Jul 07 Python
PyQt5内嵌浏览器注入JavaScript脚本实现自动化操作的代码实例
Feb 13 Python
Python实现FM算法解析
Jun 18 Python
python实现身份证实名认证的方法实例
Nov 08 Python
Python字符串格式化f-string多种功能实现
May 07 Python
一篇文章带你搞定Ubuntu中打开Pycharm总是卡顿崩溃
Nov 02 Python
PyQt 如何创建自定义QWidget
Mar 24 Python
bat批处理之字符串操作的实现
Mar 16 Python
解决tensorflow 释放图,删除变量问题
Jun 23 #Python
TensorFlow中如何确定张量的形状实例
Jun 23 #Python
Python docutils文档编译过程方法解析
Jun 23 #Python
python3的pip路径在哪
Jun 23 #Python
Python错误的处理方法
Jun 23 #Python
python文件读取失败怎么处理
Jun 23 #Python
使用tensorflow根据输入更改tensor shape
Jun 23 #Python
You might like
php 计划任务 检测用户连接状态
2012/03/29 PHP
关于PHP二进制流 逐bit的低位在前算法(详解)
2013/06/13 PHP
Prototype Template对象 学习
2009/07/19 Javascript
js客户端快捷键管理类的较完整实现和应用
2010/06/08 Javascript
超级24小时弹窗代码 24小时退出弹窗代码 100%弹窗代码(IE only)
2010/06/11 Javascript
用Jquery重写windows.alert方法实现思路
2013/04/03 Javascript
jquery.post用法关于type设置问题补充
2014/01/03 Javascript
AngularJS包括详解及示例代码
2016/08/17 Javascript
jQuery css() 方法动态修改CSS属性
2016/09/25 Javascript
JavaScript遍历Json串浏览器输出的结果不统一问题
2016/11/03 Javascript
jquery实现用户登陆界面(示例讲解)
2017/09/06 jQuery
Node.js console控制台简单用法分析
2019/01/04 Javascript
如何能分清npm cnpm npx nvm
2019/01/17 Javascript
微信小程序BindTap快速连续点击目标页面跳转多次问题处理
2019/04/08 Javascript
JavaScript 空间坐标的使用
2020/08/19 Javascript
原生JS实现九宫格抽奖
2020/09/13 Javascript
[01:07:41]IG vs VGJ.T 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
tornado捕获和处理404错误的方法
2014/02/26 Python
Python多进程池 multiprocessing Pool用法示例
2018/09/07 Python
python 实现对文件夹中的图像连续重命名方法
2018/10/25 Python
python字典和json.dumps()的遇到的坑分析
2020/03/11 Python
区分python中的进程与线程
2020/08/13 Python
名词解释WEB SERVICE,SOAP,UDDI,WSDL,JAXP,JAXM;JSWDL开发包的介绍。
2012/10/27 面试题
用Python写一个for循环的例子
2016/07/19 面试题
医务人员竞聘职务自我评价分享
2013/11/08 职场文书
新书吧创业计划书
2014/01/31 职场文书
保卫科工作岗位职责
2014/03/01 职场文书
创建无烟单位实施方案
2014/03/29 职场文书
学校三节实施方案
2014/06/09 职场文书
月度优秀员工获奖感言
2014/08/16 职场文书
竞选班长演讲稿500字
2014/08/22 职场文书
委托证明模板
2014/09/16 职场文书
民主评议党员登记表自我评价
2014/10/20 职场文书
企业群众路线教育实践活动心得体会
2014/11/03 职场文书
党员转正党支部意见
2015/06/02 职场文书
PostgreSQL常用字符串分割函数整理汇总
2022/07/07 PostgreSQL