浅谈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实现从订阅源下载图片的方法
Mar 11 Python
Python的几个高级语法概念浅析(lambda表达式闭包装饰器)
May 28 Python
python 如何快速找出两个电子表中数据的差异
May 26 Python
Python实现的基于优先等级分配糖果问题算法示例
Apr 25 Python
对python3中, print横向输出的方法详解
Jan 28 Python
pytorch+lstm实现的pos示例
Jan 14 Python
pytorch 模拟关系拟合——回归实例
Jan 14 Python
python发qq消息轰炸虐狗好友思路详解(完整代码)
Feb 15 Python
使用 Python 在京东上抢口罩的思路详解
Feb 27 Python
python+selenium+Chrome options参数的使用
Mar 18 Python
Python填充任意颜色,不同算法时间差异分析说明
May 16 Python
opencv 形态学变换(开运算,闭运算,梯度运算)
Jul 07 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
Windows PHP5和Apache的安装与配置
2009/06/08 PHP
php+ajax实现带进度条的上传图片功能【附demo源码下载】
2016/09/14 PHP
PHP实现双链表删除与插入节点的方法示例
2017/11/11 PHP
Laravel 实现关系模型取出需要的字段
2019/10/10 PHP
WordPress免插件实现面包屑导航的示例代码
2020/08/20 PHP
JSON扫盲帖 JSON.as类教程
2009/02/16 Javascript
JavaScript 加号(+)运算符号
2009/12/06 Javascript
jquery自动完成插件(autocomplete)应用之PHP版
2009/12/15 Javascript
IE6/7/8中Option元素未设value时Select将获取空字符串
2011/04/07 Javascript
javascript解析json数据的3种方式
2014/05/08 Javascript
javascript关于继承解析
2016/05/10 Javascript
JavaScript事件详细讲解
2016/06/27 Javascript
微信小程序解除10个请求并发限制
2018/12/18 Javascript
基于jQuery的时间戳与日期间的转化
2019/06/21 jQuery
[30:37]【全国守擂赛】第三周擂主赛 Dark Knight vs. Leopard Gaming
2020/05/04 DOTA
python如何生成各种随机分布图
2018/08/27 Python
在Python中调用Ping命令,批量IP的方法
2019/01/26 Python
Django如何开发简单的查询接口详解
2019/05/17 Python
Python爬虫 urllib2的使用方法详解
2019/09/23 Python
python用类实现文章敏感词的过滤方法示例
2019/10/27 Python
Python 实现敏感目录扫描的示例代码
2020/05/21 Python
python输入中文的实例方法
2020/09/14 Python
css3实现背景图片拉伸效果像桌面壁纸一样
2013/08/19 HTML / CSS
澳大利亚领先的睡衣品牌:Peter Alexander
2016/08/16 全球购物
英国最大的正宗复古足球衫制造商和零售商:TOFFS
2018/06/21 全球购物
Solaris操作系统的线程机制
2012/12/23 面试题
金融专业大学生职业生涯规划范文
2014/01/16 职场文书
个人党性剖析材料
2014/02/03 职场文书
教师节感谢信
2015/01/22 职场文书
2015年监理工作总结范文
2015/04/07 职场文书
计划生育目标责任书
2015/05/09 职场文书
傲慢与偏见电影观后感
2015/06/10 职场文书
MongoDB使用profile分析慢查询的步骤
2021/04/30 MongoDB
QT连接MYSQL数据库的详细步骤
2021/07/07 MySQL
CSS文本阴影 text-shadow 悬停效果详解
2022/05/25 HTML / CSS
MySQL常用慢查询分析工具详解
2022/08/14 MySQL