浅谈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使用cookielib库示例分享
Mar 03 Python
Python中使用strip()方法删除字符串中空格的教程
May 20 Python
详解Django中的权限和组以及消息
Jul 23 Python
python中的turtle库函数简单使用教程
Jul 23 Python
使用celery执行Django串行异步任务的方法步骤
Jun 06 Python
python版百度语音识别功能
Jul 09 Python
Python编写带选项的命令行程序方法
Aug 13 Python
Python OpenCV视频截取并保存实现代码
Nov 30 Python
Tensorflow 自定义loss的情况下初始化部分变量方式
Jan 06 Python
Python如何爬取51cto数据并存入MySQL
Aug 25 Python
python 自定义异常和主动抛出异常(raise)的操作
Dec 11 Python
python爬取企查查企业信息之selenium自动模拟登录企查查
Apr 08 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中静态方法和非静态方法的相互调用
2016/10/04 PHP
火狐浏览器(firefox)下获得Event对象以及keyCode
2008/11/13 Javascript
十个迅速提升JQuery性能让你的JQuery跑得更快
2012/12/10 Javascript
js网页中的(运行代码)功能实现思路
2013/02/04 Javascript
JS获取地址栏参数的小例子
2013/08/23 Javascript
js获取指定日期周数以及星期几的小例子
2014/06/27 Javascript
javascript实现图片跟随鼠标移动效果的方法
2015/05/13 Javascript
继续学习javascript闭包
2015/12/03 Javascript
深入学习jQuery Validate表单验证
2016/01/18 Javascript
基于Bootstrap实现的下拉菜单手机端不能选择菜单项的原因附解决办法
2016/07/22 Javascript
jQuery页面弹出框实现文件上传
2017/02/09 Javascript
easy ui datagrid 从编辑框中获取值的方法
2017/02/22 Javascript
基于JQuery的Ajax方法使用详解
2017/08/16 jQuery
jQuery中库的引用方法
2018/01/06 jQuery
新年快乐! javascript实现超级炫酷的3D烟花特效
2019/01/30 Javascript
vue权限问题的完美解决方案
2019/05/08 Javascript
解决使用layui的时候form表单中的select等不能渲染的问题
2019/09/18 Javascript
vue实现移动端图片上传功能
2019/12/23 Javascript
原生js实现分页效果
2020/09/23 Javascript
利用python模拟sql语句对员工表格进行增删改查
2017/07/05 Python
python决策树之CART分类回归树详解
2017/12/20 Python
pyqt5简介及安装方法介绍
2018/01/31 Python
python中的插值 scipy-interp的实现代码
2018/07/23 Python
远程部署工具Fabric详解(支持Python3)
2019/07/04 Python
mac系统下Redis安装和使用步骤详解
2019/07/09 Python
详解css3 mask遮罩实现一些特效
2018/10/24 HTML / CSS
详解快速开发基于 HTML5 网络拓扑图应用
2018/01/08 HTML / CSS
前端H5 Video常见使用场景简介
2020/08/21 HTML / CSS
文明倡议书范文
2014/04/15 职场文书
商业项目策划方案
2014/06/05 职场文书
讲党性心得体会
2014/09/03 职场文书
党员个人批评与自我批评
2014/10/14 职场文书
环卫工作个人总结
2015/03/04 职场文书
2015建军节87周年演讲稿
2015/03/19 职场文书
联欢会开场白
2015/06/01 职场文书
2016初一新生军训心得体会
2016/01/11 职场文书