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

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

a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)
ab1 = tf.concat([a,b], axis=0) # shape(4,3)
ab2 = tf.concat([a,b], axis=1) # shape(2,6)
  • 1
  • 2
  • 3
  • 4

tf.stack其作用类似于tf.concat,都是拼接两个张量,而不同之处在于,tf.concat拼接的是两个shape完全相同的张量,并且产生的张量的阶数不会发生变化,而tf.stack则会在新的张量阶上拼接,产生的张量的阶数将会增加,例如:

a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)
ab = tf.stack([a,b], axis=0) # shape (2,2,3)
  • 1
  • 2
  • 3

改变参数axis为2,有:

import tensorflow as tf
a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)
ab = tf.stack([a,b], axis=2) # shape (2,3,2)

所以axis是决定其层叠(stack)张量的维度方向的。

tf.unstacktf.stack的操作相反,是将一个高阶数的张量在某个axis上分解为低阶数的张量,例如:

a = tf.constant([[1,2,3],[3,4,5]]) # shape (2,3)
b = tf.constant([[7,8,9],[10,11,12]]) # shape (2,3)
ab = tf.stack([a,b], axis=0) # shape (2,2,3) a1 = tf.unstack(ab, axis=0)

其a1的输出为

[<tf.Tensor 'unstack_1:0' shape=(2, 3) dtype=int32>,
<tf.Tensor 'unstack_1:1' shape=(2, 3) dtype=int32>]

tf.concat, tf.stack和tf.unstack的用法的更多相关文章

  1. tf.concat( )和tf.stack( )

    相同点:都是组合重构数据. 不同点:concat()不改变维数,而stack改变了维数(待定!!!) tf.concat是连接两个矩阵的操作,请注意API版本更改问题,相应参数也发生改变,具体查看AP ...

  2. 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 ...

  3. tf.concat()

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

  4. 深度学习原理与框架-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 ...

  5. 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 ...

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

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

  7. 学习TensorFlow的tf.concat使用

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

  8. 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 ...

  9. tf.concat,连接矩阵

    tf.concat(concat_dim, values, name='concat') concat_dim需要连接的矩阵的维度, values需要连接的两个矩阵. a=[[1,2,3],[7,8, ...

随机推荐

  1. Activemq+Zookeeper集群

    如果在同一台机器上请参考 http://blog.csdn.net/liuyifeng1920/article/details/50233067 http://blog.csdn.net/zuolj/ ...

  2. oracle A用户访问B用户的表aa

    在B中:grant select on aa to A; (还可以配置insert,update,delete权限)

  3. HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. BZOJ 1207 DP

    打一次鼹鼠必然是从曾经的某一次打鼹鼠转移过来的 以打每一个鼹鼠时的最优解为DP方程 #include<iostream> #include<cstdio> #include&l ...

  5. LPC LINK2 IO CONNECTOR

  6. Mui --- app与服务器之间的交互原理、mui ajax使用

    1.APP与服务器之间的交互原理 app端(客户端)与服务端的交互其实理解起来和容易,客户端想服务器端发送请求,服务器端进行数据运算后返回最终结果.结果可以是多种格式: 1.text 文本格式 2.x ...

  7. [廖雪峰] Git 分支管理(1):创建与合并分支(HEAD、master、dev、指针)

    每次提交,Git 都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在 Git 里,这个分支叫主分支,即 master 分支.HEAD 严格来说不是指向提交,而是指向 mas ...

  8. Dapper-translation 分布式监控系统

    http://bigbully.github.io/Dapper-translation/ https://github.com/bigbully/Dapper-translation

  9. 在Visual Studio中使用层关系图描述系统架构、技术栈

    当需要描述项目的架构或技术栈的时候,可以考虑使用层关系图. 在解决方案下添加一个名称为"TailspinToys.DesignModel"的建模项目. 在新建的建模项目下添加一个名 ...

  10. IIS 调用Microsoft.Office.Interop.Word.Documents.Open 返回为null

    控制面板->管理工具->组件服务->计算机->我的电脑->DCom配置->找到Microsoft Word文档 之后 单击属性打开此应用程序的属性对话框. 2. 单 ...