浅谈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中列表和元祖的使用方法
Apr 25 Python
Python中用PIL库批量给图片加上序号的教程
May 06 Python
Python第三方库的安装方法总结
Jun 06 Python
Python实现批量压缩图片
Jan 25 Python
python 重定向获取真实url的方法
May 11 Python
python3.x实现发送邮件功能
May 22 Python
Python + selenium + requests实现12306全自动抢票及验证码破解加自动点击功能
Nov 23 Python
python3 下载网络图片代码实例
Aug 27 Python
Python使用random模块生成随机数操作实例详解
Sep 17 Python
Python编译为二进制so可执行文件实例
Dec 23 Python
Python线程协作threading.Condition实现过程解析
Mar 12 Python
OpenCV 表盘指针自动读数的示例代码
Apr 10 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读取本地文件常用函数(fopen与file_get_contents)
2013/09/09 PHP
apache中为php 设置虚拟目录
2014/12/17 PHP
smarty的section嵌套循环用法示例
2016/05/28 PHP
php简单截取字符串代码示例
2016/10/19 PHP
Laravel 自动生成验证的实例讲解:login / logout
2019/10/14 PHP
Aster vs Newbee BO5 第二场2.19
2021/03/10 DOTA
jquery 操作iframe的几种方法总结
2013/12/13 Javascript
jquery实现人性化的有选择性禁用鼠标右键
2014/06/30 Javascript
JS+CSS实现的日本门户网站经典选项卡导航效果
2015/09/27 Javascript
轻松掌握JavaScript代理模式
2016/08/26 Javascript
JS实现屏蔽网页右键复制及ctrl+c复制的方法【2种方法】
2016/09/04 Javascript
Vue实现双向绑定的方法
2016/12/22 Javascript
Js apply方法详解
2017/02/16 Javascript
Angular2学习教程之ng中变更检测问题详解
2017/05/28 Javascript
vue-cli扩展多模块打包的示例代码
2018/04/09 Javascript
详解Vue打包优化之code spliting
2018/04/09 Javascript
Node.js 多线程完全指南总结
2019/03/27 Javascript
vue中格式化时间过滤器代码实例
2019/04/17 Javascript
使用preload预加载页面资源时注意事项
2020/02/03 Javascript
在react中使用vue的状态管理的方法示例
2020/05/02 Javascript
js数组中去除重复值的几种方法
2020/08/03 Javascript
记一次vue跨域的解决
2020/10/21 Javascript
简单介绍Python中的struct模块
2015/04/28 Python
Python 保存矩阵为Excel的实现方法
2019/01/28 Python
python批量修改文件夹及其子文件夹下的文件内容
2019/03/15 Python
对Python中TKinter模块中的Label组件实例详解
2019/06/14 Python
使用Django和Postgres进行全文搜索的实例代码
2020/02/13 Python
加拿大知名的国际儿童品牌:Hatley
2016/11/09 全球购物
编写类String 的构造函数、析构函数和赋值函数
2012/09/09 面试题
班组长工作职责
2013/12/25 职场文书
大学生职业规划论文
2014/01/11 职场文书
解除劳动合同协议书范本
2014/09/13 职场文书
解除劳动关系协议书2篇
2014/11/28 职场文书
乱世佳人观后感
2015/06/08 职场文书
pytorch交叉熵损失函数的weight参数的使用
2021/05/24 Python
GoFrame gredis缓存DoVar Conn连接对象 自动序列化GoFrame gredisDo/DoVar方法Conn连接对象自动序列化/反序列化总结
2022/06/14 Golang