浅谈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 相关文章推荐
linux系统使用python监控apache服务器进程脚本分享
Jan 15 Python
Python读取mp3中ID3信息的方法
Mar 05 Python
在Python的while循环中使用else以及循环嵌套的用法
Oct 14 Python
深入理解python函数递归和生成器
Jun 06 Python
python爬取亚马逊书籍信息代码分享
Dec 09 Python
一篇文章快速了解Python的GIL
Jan 12 Python
Python网络爬虫神器PyQuery的基本使用教程
Feb 03 Python
解决python3 json数据包含中文的读写问题
May 10 Python
python 字符串只保留汉字的方法
Nov 16 Python
Python三元运算与lambda表达式实例解析
Nov 30 Python
mac 上配置Pycharm连接远程服务器并实现使用远程服务器Python解释器的方法
Mar 19 Python
python实现企业微信定时发送文本消息的实例代码
Nov 25 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
TP5框架实现签到功能的方法分析
2020/04/05 PHP
JavaScript Event学习第五章 高级事件注册模型
2010/02/07 Javascript
jQuery 网易相册鼠标移动显示隐藏效果实现代码
2013/03/31 Javascript
深入理解javaScript中的事件驱动
2013/05/21 Javascript
解析DHTML,JavaScript,DOM,BOM以及WEB标准的描述
2013/06/19 Javascript
Java/JS获取flash高宽的具体方法
2013/12/27 Javascript
AngularJS iframe跨域打开内容时报错误的解决办法
2015/01/26 Javascript
JQuery查找DOM节点的方法
2015/06/11 Javascript
使用AngularJS创建单页应用的编程指引
2015/06/19 Javascript
JavaScript截断字符串的方法
2015/07/15 Javascript
vue2.0的contextmenu右键弹出菜单的实例代码
2017/07/24 Javascript
详谈js中标准for循环与foreach(for in)的区别
2017/11/02 Javascript
JS 实现百度搜索功能
2018/02/01 Javascript
Vue2 模板template的四种写法总结
2018/02/23 Javascript
Vue.js 图标选择组件实践详解
2018/12/03 Javascript
javascript获取select值的方法完整实例
2019/06/20 Javascript
vue iview实现动态新增和删除
2020/06/17 Javascript
python实现360皮肤按钮控件示例
2014/02/21 Python
python实现数值积分的Simpson方法实例分析
2015/06/05 Python
django限制匿名用户访问及重定向的方法实例
2018/02/07 Python
Django 跨域请求处理的示例代码
2018/05/02 Python
Ubuntu下使用python读取doc和docx文档的内容方法
2018/05/08 Python
python list格式数据excel导出方法
2018/10/31 Python
Python中flatten( )函数及函数用法详解
2018/11/02 Python
Python中format()格式输出全解
2019/04/12 Python
Pytorch 计算误判率,计算准确率,计算召回率的例子
2020/01/18 Python
class类在python中获取金融数据的实例方法
2020/12/10 Python
英国手机壳购买网站:Case Hut
2019/04/11 全球购物
慕尼黑山地运动、户外服装和体育用品专家:Sporthaus Schuster
2019/08/27 全球购物
Linux不知道文件后缀名怎么判断文件类型
2012/04/26 面试题
中软国际Java程序员笔试题
2014/07/19 面试题
助人为乐好少年事迹材料
2014/08/18 职场文书
交通事故委托书范本
2014/09/28 职场文书
成本会计实训报告
2014/11/05 职场文书
《红领巾真好》教学反思
2016/02/16 职场文书
Python还能这么玩之用Python修改了班花的开机密码
2021/06/04 Python