tf.unstack

原型:

unstack(
value,
num=None,
axis=0,
name='unstack' )

官方解释:https://tensorflow.google.cn/api_docs/python/tf/unstack

解释:这是一个对矩阵进行分解的函数,以下为关键参数解释:

value:代表需要分解的矩阵变量(其实就是一个多维数组,一般为二维);

axis:指明对矩阵的哪个维度进行分解。

要理解tf.unstack函数,我们不妨先来看看tf.stack函数。Tf.stack刚好是与tf.unstack函数相反,前者是对矩阵进行拼接,后者则对矩阵进行分解。

Tf.stack用法举例:假如现在有两个变量,a=[1, 2, 3],b=[4, 5, 6],现在我要使用tf.stack对他们进行拼接,变成一个二维矩阵[ [1, 2, 3], [4, 5, 6] ]。代码【示例1】如下:

【示例1】



import tensorflow as tf a = tf.constant([1, 2, 3]) b = tf.constant([4, 5, 6]) c = tf.stack( [a,b], axis=0) with tf.Session() as sess: print(sess.run(c))

输出结果是:



[[1 2 3] [4 5 6]]

此时,我如果把【示例1】里面的tf.stack参数axis=0改成1,运行结果如下:


[[1 4]

 [2 5]

 [3 6]]

可以理解,axis作用就是指明以何种方式对矩阵进行拼接,说白了,就是对原矩阵的哪个维度进行拼接。

理解了tf.stack,tf.unstack也就不难理解了。比如说现在有变量c,如下:

c=[[1 2 3]

[4 5 6]]

现在要对c进行分解,代码如下:



import tensorflow as tf c = tf.constant([[1, 2, 3], [4, 5, 6]]) d = tf.unstack(c, axis=0) e = tf.unstack(c, axis=1) with tf.Session() as sess: print(sess.run(d)) print(sess.run(e))

结果如下:


[array([1, 2, 3]), array([4, 5, 6])]

[array([1, 4]), array([2, 5]), array([3, 6])]

可以看出来,tf.unstack其实就是在做与tf.stack相反的事情。这样一来,你是不是恍然大悟了呢?

作者:JempChou
链接:https://www.jianshu.com/p/25706575f8d4
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

tf.unstack()、tf.stack()的更多相关文章

  1. tf.Variable()、tf.get_variable()和tf.placeholder()

    1.tf.Variable() tf.Variable(initializer,name) 功能:tf.Variable()创建变量时,name属性值允许重复,检查到相同名字的变量时,由自动别名机制创 ...

  2. deep_learning_Function_tf.add()、tf.subtract()、tf.multiply()、tf.div()

    tf.add().tf.subtract().tf.multiply().tf.div()函数介绍和示例 1. tf.add() 释义:加法操作 示例: x = tf.constant(2, dtyp ...

  3. 通俗理解tf.name_scope()、tf.variable_scope()

    前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记:tf.name_scope( ...

  4. tensorflow 学习笔记-- tf.reduce_max、tf.sequence_mask

    1.tf.reduce_max函数的作用:计算张量的各个维度上的元素的最大值.例子: import tensorflow as tfmax_value = tf.reduce_max([1, 3, 2 ...

  5. TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系

    1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要 ...

  6. tensorflow笔记4:函数:tf.assign()、tf.assign_add()、tf.identity()、tf.control_dependencies()

    函数原型: tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)   Defined in tensorflo ...

  7. 理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope

    tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable( ...

  8. 彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同

    https://blog.csdn.net/qq_22522663/article/details/78729029 1. tf.Variable与tf.get_variabletensorflow提 ...

  9. tensorflow中 tf.add_to_collection、 tf.get_collection 和 tf.add_n函数

    tf.add_to_collection(name, value)  用来把一个value放入名称是'name'的集合,组成一个列表; tf.get_collection(key, scope=Non ...

随机推荐

  1. 学习笔记之FluentAssertions

    dotnet/src/MoqSample at master · haotang923/dotnet · GitHub https://github.com/htanghtang/dotnet/tre ...

  2. pyplot 绘图与可视化

    1. 基本使用 #!/usr/bin/env python # coding=utf-8 import matplotlib.pyplot as plt from numpy.random impor ...

  3. 比较有意思的原生态js拖拽写法----摘自javascript高级程序设计3

    var DragDrop = function () { var dragging = null; var diffX = 0; var diffY = 0; function handleEvent ...

  4. java中key-value数据有重复KEY如何存储

    http://www.iteye.com/problems/87219 Map<Key, List<Value>>, 这个好 师兄厉害,给介绍了个神器:guava

  5. CUDA C Programming Guide 在线教程学习笔记 Part 11

    ▶ 数学函数 ● 舍入函数,考虑被舍入参数有双精度浮点和单精度浮点,舍入方式有区别,舍入结果有整形.长整形和长长整形,所以共有以下舍入函数. // math_functions.h extern __ ...

  6. 64位win10系统无法安装.Net framework3.5的两种解决方法

    参考网站: https://blog.csdn.net/zang141588761/article/details/52177290 在Windows10中,当我们安装某些软件的时候会提示“你的电脑上 ...

  7. 自动化运维工具 SaltStack 在云计算环境中的实践

    http://www.talkwithtrend.com/Article/218473

  8. leetcode237

    /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...

  9. JS转义解码方法

    之前只知道可以解决传值乱码问题,今天刚好看到,从新补充下: JavaScript中有三个可以对字符串编码的函数,分别是: 转义方法: escape();//函数可对字符串进行编码 encodeURI( ...

  10. linux查看磁盘占用常用的两个命令

    1.查看总容量.已使用.未使用容量:df -hl -h:以kb以上单位显示 -l:仅显示本地文件系统 2.查看当前路径下,每个文件/夹占用空间大小:du -sh *