相同点:都是组合重构数据.

不同点:concat()不改变维数,而stack改变了维数(待定!!!)

tf.concat是连接两个矩阵的操作,请注意API版本更改问题,相应参数也发生改变,具体查看API.

tf.concat(concat_dim, values, name='concat')

除去name参数用以指定该操作的name,与方法有关的一共两个参数:

第一个参数concat_dim:必须是一个数,表明在哪一维上连接

     如果concat_dim是0,那么在某一个shape的第一个维度上连,对应到实际,就是叠放到列上

  1. t1 = [[1, 2, 3], [4, 5, 6]]
  2. t2 = [[7, 8, 9], [10, 11, 12]]
  3. tf.concat(0, [t1, t2]) == > [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat(0, [t1, t2]) == > [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

如果concat_dim是1,那么在某一个shape的第二个维度上连

  1. t1 = [[1, 2, 3], [4, 5, 6]]
  2. t2 = [[7, 8, 9], [10, 11, 12]]
  3. tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12

如果有更高维,最后连接的依然是指定那个维:

values[i].shape = [D0, D1, ... Dconcat_dim(i), ...Dn]连接后就是:[D0, D1, ... Rconcat_dim, ...Dn]

  1. # tensor t3 with shape [2, 3]
  2. # tensor t4 with shape [2, 3]
  3. tf.shape(tf.concat(0, [t3, t4])) ==> [4, 3]
  4. tf.shape(tf.concat(1, [t3, t4])) ==> [2, 6]
# tensor t3 with shape [2, 3]
# tensor t4 with shape [2, 3]
tf.shape(tf.concat(0, [t3, t4])) ==> [4, 3]
tf.shape(tf.concat(1, [t3, t4])) ==> [2, 6]

第二个参数values:就是两个或者一组待连接的tensor了

 

/×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××/

这里要注意的是:如果是两个向量,它们是无法调用

  1. tf.concat(1, [t1, t2])
tf.concat(1, [t1, t2])

来连接的,因为它们对应的shape只有一个维度,当然不能在第二维上连了,虽然实际中两个向量可以在行上连,但是放在程序里是会报错的

如果要连,必须要调用tf.expand_dims来扩维:

  1. t1=tf.constant([1,2,3])
  2. t2=tf.constant([4,5,6])
  3. #concated = tf.concat(1, [t1,t2])这样会报错
  4. t1=tf.expand_dims(tf.constant([1,2,3]),1)
  5. t2=tf.expand_dims(tf.constant([4,5,6]),1)
  6. concated = tf.concat(1, [t1,t2])#这样就是正确的

tf.concat( )和tf.stack( )的更多相关文章

  1. tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask

    1.  tf.split(3, group, input)  # 拆分函数    3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...

  2. tf.concat()

    转载自:https://blog.csdn.net/appleml/article/details/71023039 https://www.cnblogs.com/mdumpling/p/80534 ...

  3. tensorflow 生成随机数 tf.random_normal 和 tf.random_uniform 和 tf.truncated_normal 和 tf.random_shuffle

    ____tz_zs tf.random_normal 从正态分布中输出随机值. . <span style="font-size:16px;">random_norma ...

  4. tf.concat, tf.stack和tf.unstack的用法

    tf.concat, tf.stack和tf.unstack的用法 tf.concat相当于numpy中的np.concatenate函数,用于将两个张量在某一个维度(axis)合并起来,例如: a ...

  5. 深度学习原理与框架-Alexnet(迁移学习代码) 1.sys.argv[1:](控制台输入的参数获取第二个参数开始) 2.tf.split(对数据进行切分操作) 3.tf.concat(对数据进行合并操作) 4.tf.variable_scope(指定w的使用范围) 5.tf.get_variable(构造和获得参数) 6.np.load(加载.npy文件)

    1. sys.argv[1:]  # 在控制台进行参数的输入时,只使用第二个参数以后的数据 参数说明:控制台的输入:python test.py what, 使用sys.argv[1:],那么将获得w ...

  6. tensorflow报错error,tf.concat Expected int32, got list containing Tensors of type '_Message' instead

    参考:https://stackoverflow.com/questions/41813665/tensorflow-slim-typeerror-expected-int32-got-list-co ...

  7. tensorflow 笔记7:tf.concat 和 ops中的array_ops.concat

    用于连接两个矩阵: mn = array_ops.concat([a, d], 1) #  按照第二维度相接,shape1 [m,a] shape2 [m,b] ,concat_done shape ...

  8. 学习TensorFlow的tf.concat使用

    https://www.tensorflow.org/api_docs/python/tf/concat

  9. tf.concat的用法

    import numpy as npimport tensorflow as tfsess=tf.Session()a=np.zeros((1,2,3,4))b=np.ones((1,2,3,4))c ...

随机推荐

  1. mybatis 动态SQL .2

    目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,otherwise) 语句 5.动态SQL:tri ...

  2. MySQL 数据库下载

    地址链接: msi:https://dev.mysql.com/downloads/installer/ zip:https://downloads.mysql.com/archives/commun ...

  3. C++写Socket——TCP篇(0)建立连接及双方传输数据

    满山的红叶--飘落之时-- 最近接触了点关于用C++写socket的东西,这里总结下. 这里主要是关于TCP的,TCP的特点什么的相关介绍在我另一篇博文里,所以这里直接动手吧. 我们先在windows ...

  4. vue-cli 3 ----- 项目频繁发送‘sockjs-node/info’请求

    在vue-cli3跑项目时发现了这个问题,浏览器一直在频繁发送这个请求,导致联调时很不方便,而且本地开发时项目也不能实时更新. 看了网上很多的 (1)  解决方案, 大多都是直接去node_modul ...

  5. 2019JAVA第十次实验报告

    Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间 2019.11.15 评分等级 实验代码 package Domon9; import java.awt.Font; ...

  6. Spring Boot常用功能

    1.Spring Boot打war包配置 利用IDEA将SpringBoot的项目打包成war文件

  7. 使用注解方式搭建SpringMVC

    1.以前搭建Spring MVC 框架一般都使用配置文件的方式进行,相对比较繁琐.spring 提供了使用注解方式搭建Spring MVC 框架的方式,方便简洁.使用Spring IOC 作为根容器管 ...

  8. Hive Error : Java heap space 解决方案

    Java heap space问题一般解决方案: 设置 set io.sort.mb=10; 排序所使用的内存数量,默认值是100M,和mapred.child.java.opts相对应,opts默认 ...

  9. mysql 官网下载太慢了,来这里!!!

    RT.去官网下载mysql简直是折磨,太慢了!!! 但我还是坚持住了,下载下来了,我自己下载的是 MAC 5.7.27版本,网盘分享下,有需要的自提吧: 链接:https://pan.baidu.co ...

  10. 【洛谷p1058】立体图(已完结)

    立体图[题目链接] 然后因为有点(不是有点,非常)懵,因此我只能看一步写一步. 首先总体思路: 将三维立体图看做二维平面图,先确定出二维图的长和宽,然后,按照三维立体图的透视顺序,从最后一排的最左开始 ...