浅谈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 25 Python
python机器学习实战之K均值聚类
Dec 20 Python
PyCharm搭建Spark开发环境实现第一个pyspark程序
Jun 13 Python
python爬虫 爬取58同城上所有城市的租房信息详解
Jul 30 Python
python使用多线程编写tcp客户端程序
Sep 02 Python
python读取ini配置文件过程示范
Dec 23 Python
python 线性回归分析模型检验标准--拟合优度详解
Feb 24 Python
Django CSRF认证的几种解决方案
Mar 03 Python
Python退出时强制运行一段代码的实现方法
Apr 29 Python
python爬虫使用正则爬取网站的实现
Aug 03 Python
用Python实现童年贪吃蛇小游戏功能的实例代码
Dec 07 Python
python自动生成sql语句的脚本
Feb 24 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
安装ImageMagick出现error while loading shared libraries的解决方法
2014/09/23 PHP
php使用PDO方法详解
2014/12/27 PHP
PHP获取QQ达人QQ信息的方法
2015/03/05 PHP
php基于自定义函数记录log日志方法
2017/07/21 PHP
js 对联广告、漂浮广告封装类(IE,FF,Opera,Safari,Chrome
2009/11/26 Javascript
js动态加载以及确定加载完成的代码
2011/07/31 Javascript
javascript实现动态导入js与css等静态资源文件的方法
2015/07/25 Javascript
基于jQuery实现网页打印功能
2015/12/01 Javascript
Node.js中Request模块处理HTTP协议请求的基本使用教程
2016/03/31 Javascript
Backbone中View之间传值的学习心得
2016/08/09 Javascript
JS实现商品筛选功能
2020/08/19 Javascript
Angular4学习笔记之实现绑定和分包
2017/08/01 Javascript
webstorm中vue语法的支持详解
2018/05/09 Javascript
Vue.js项目中管理每个页面的头部标签的两种方法
2018/06/25 Javascript
Node.js 路由的实现方法
2019/06/05 Javascript
vue实现登录页面的验证码以及验证过程解析(面向新手)
2019/08/02 Javascript
JavaScript之scrollTop、scrollHeight、offsetTop、offsetHeight等属性学习笔记
2020/07/15 Javascript
详解vue组件之间的通信
2020/08/30 Javascript
[02:44]DOTA2英雄基础教程 克林克兹
2014/01/15 DOTA
Python操作MongoDB详解及实例
2017/05/18 Python
python的scikit-learn将特征转成one-hot特征的方法
2018/07/10 Python
python读取txt文件中特定位置字符的方法
2018/12/24 Python
Python正则表达式实现简易计算器功能示例
2019/05/07 Python
对Django项目中的ORM映射与模糊查询的使用详解
2019/07/18 Python
解决Django提交表单报错:CSRF token missing or incorrect的问题
2020/03/13 Python
10分钟理解CSS3 Grid布局
2018/12/20 HTML / CSS
应届生法律求职信
2013/10/22 职场文书
银行柜员应聘推荐信范文
2013/11/24 职场文书
学生党员思想汇报范文
2014/01/09 职场文书
中文专业毕业生自荐信
2014/05/24 职场文书
2015年人力资源部工作总结
2015/04/30 职场文书
我的长征观后感
2015/06/09 职场文书
学习十八大的感悟
2015/08/11 职场文书
关于MySQL中的 like操作符详情
2021/11/17 MySQL
详解apache编译安装httpd-2.4.54及三种风格的init程序特点和区别
2022/07/15 Servers
Win11 Beta 预览版 22621.575 和 22622.575更新补丁KB5016694发布(附更新内容大全)
2022/08/14 数码科技