浅谈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 相关文章推荐
Pyramid将models.py文件的内容分布到多个文件的方法
Nov 27 Python
在Python中操作时间之tzset()方法的使用教程
May 22 Python
python利用urllib实现爬取京东网站商品图片的爬虫实例
Aug 24 Python
Python自定义线程类简单示例
Mar 23 Python
pandas去除重复列的实现方法
Jan 29 Python
Python 图像对比度增强的几种方法(小结)
Sep 25 Python
python实现根据文件格式分类
Oct 31 Python
python实现图像拼接功能
Mar 23 Python
tensorflow 2.1.0 安装与实战教程(CASIA FACE v5)
Jun 30 Python
python 绘制国旗的示例
Sep 27 Python
基于Python的接口自动化unittest测试框架和ddt数据驱动详解
Jan 27 Python
pytorch DataLoader的num_workers参数与设置大小详解
May 28 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面向对象全攻略 (九)访问类型
2009/09/30 PHP
奉献出一个封装的curl函数 便于调用(抓数据专用)
2013/07/22 PHP
PHP中模拟链表和链表的基本操作示例
2016/02/27 PHP
在CentOS系统上从零开始搭建WordPress博客的全流程记录
2016/04/21 PHP
javascript在事件监听方面的兼容性小结
2010/04/07 Javascript
jquery+css实现的红色线条横向二级菜单效果
2015/08/22 Javascript
浅析在javascript中创建对象的各种模式
2016/05/06 Javascript
微信小程序 详解页面跳转与返回并回传数据
2017/02/13 Javascript
JavaScript学习笔记之函数记忆
2017/09/06 Javascript
jQuery中将json数据显示到页面表格的方法
2018/05/27 jQuery
浅谈Vue路由快照实现思路及其问题
2018/06/07 Javascript
npm 语义版本控制详解
2019/09/10 Javascript
快速解决vue2+vue-cli3项目ie兼容的问题
2020/11/17 Vue.js
用Python编写一个简单的FUSE文件系统的教程
2015/04/02 Python
安装ElasticSearch搜索工具并配置Python驱动的方法
2015/12/22 Python
python实现文本去重且不打乱原本顺序
2016/01/26 Python
python3.5仿微软计算器程序
2020/03/30 Python
python 从csv读数据到mysql的实例
2018/06/21 Python
python得到windows自启动列表的方法
2018/10/14 Python
Python装饰器简单用法实例小结
2018/12/03 Python
python opencv图片编码为h264文件的实例
2019/12/12 Python
Python正则表达式如何匹配中文
2020/05/27 Python
浅谈Python里面None True False之间的区别
2020/07/09 Python
python3定位并识别图片验证码实现自动登录功能
2021/01/29 Python
python matplotlib工具栏源码探析三之添加、删除自定义工具项的案例详解
2021/02/25 Python
python推导式的使用方法实例
2021/02/28 Python
英国最大的手表网站:The Watch Hut
2017/03/31 全球购物
美国在线购买空气净化器、除湿器、加湿器网站:AllergyBuyersClub
2021/03/16 全球购物
文明寄语大全
2014/04/11 职场文书
自主招生推荐信范文
2014/05/10 职场文书
环保倡议书400字
2014/05/15 职场文书
公司财务经理岗位职责
2015/04/08 职场文书
2015年暑期社会实践报告
2015/07/13 职场文书
立秋之描写立秋的作文(五年级)
2019/08/08 职场文书
python 定义函数 返回值只取其中一个的实现
2021/05/21 Python
Python中非常使用的6种基本变量的操作与技巧
2022/03/22 Python