tf.unstack\tf.unstack
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.unstack的更多相关文章
- tf.concat, tf.stack和tf.unstack的用法
tf.concat, tf.stack和tf.unstack的用法 tf.concat相当于numpy中的np.concatenate函数,用于将两个张量在某一个维度(axis)合并起来,例如: a ...
- TFboy养成记 tf.cast,tf.argmax,tf.reduce_sum
referrence: 莫烦视频 先介绍几个函数 1.tf.cast() 英文解释: 也就是说cast的直译,类似于映射,映射到一个你制定的类型. 2.tf.argmax 原型: 含义:返回最大值所在 ...
- TensorFlow学习笔记之--[tf.clip_by_global_norm,tf.clip_by_value,tf.clip_by_norm等的区别]
以下这些函数可以用于解决梯度消失或梯度爆炸问题上. 1. tf.clip_by_value tf.clip_by_value( t, clip_value_min, clip_value_max, n ...
- TensorFlow tf.app&tf.app.flags用法介绍
TensorFlow tf.app&tf.app.flags用法介绍 TensorFlow tf.app argparse tf.app.flags 下面介绍 tf.app.flags.FL ...
- TF:TF下CNN实现mnist数据集预测 96%采用placeholder用法+2层C及其max_pool法+隐藏层dropout法+输出层softmax法+目标函数cross_entropy法+AdamOptimizer算法
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # number 1 to 10 ...
- TF:TF分类问题之MNIST手写50000数据集实现87.4%准确率识别:SGD法+softmax法+cross_entropy法—Jason niu
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # number 1 to 10 ...
- TF:TF之Tensorboard实践:将神经网络Tensorboard形式得到events.out.tfevents文件+dos内运行该文件本地服务器输出到网页可视化—Jason niu
import tensorflow as tf import numpy as np def add_layer(inputs, in_size, out_size, n_layer, activat ...
- TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值—Jason niu
#TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值 import tensorflow as tf input1 = tf.placeholder ...
- tensorflow生成随机数的操作 tf.random_normal & tf.random_uniform & tf.truncated_normal & tf.random_shuffle
tf.random_normal 从正态分布输出随机值. random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name ...
随机推荐
- java操作Excel之POI(5)利用POI实现使用模板批量导出数据
后台导出方法: 在源文件夹src下面放个准备好的模板:/com/cy/template/userExportTemplate.xls,这个模板有头部一行: /** * 后台导出方法 * 利用POI实现 ...
- Access restriction: The type Resource is not accessible due to restriction on required library
方法一: 全局属性Project>preferences>java>Compiler>Errors/Warnings>把右侧的[Deprecated and restri ...
- RouterOS 设定NAT loopback (Hairpin NAT)回流
In the below network topology a web server behind a router is on private IP address space, and the r ...
- JSON Web Token的使用(转载)
定义 JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用JWT在用户和服务器之间传递安全可靠的信息. 适用场景 1.用于向Web应用传递一些非敏感信息.例如完成加好友.下 ...
- django从请求到响应的过程深入讲解
django启动 我们在启动一个django项目的时候,无论你是在命令行执行还是在pycharm直接点击运行,其实都是执行'runserver'的操作,而ruserver是使用django自带的的we ...
- 异步与websocket
异步与WebSockets 知识点 理解同步与异步执行过程 理解异步代码的回调写法与yield写法 Tornado异步 异步Web客户端AsyncHTTPClient tornado.web.asyn ...
- 0_Simple__simpleZeroCopy
两种方法使用零拷贝内存做简单的向量加和,并评估 GPU 计算结果与 CPU 计算结果的差. ▶ 源代码 #include <stdio.h> #include <cuda.h> ...
- Dark theme for Texstudio - TeX - LaTeX
Dark theme for Texstudio ~~~ 1.window系统如下操作 ~~~ 1. texstudio的配置文件texstudio 的配置文件在~/.config/texstudi ...
- Python 中一个逗号引发的悲剧
遇到一个 Python 字符串的坑,记录一下.看看下面这些代码 >>> a = [ ... 'foo' ... 'bar', ... 'tree' ... ] >>> ...
- python 编写远程连接服务器脚本
import paramiko client = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPoli ...