TensorFlow2.0:张量的合并与分割实例

yipeiwu_com5年前Python基础

**

一 tf.concat( ) 函数–合并
**

In [2]: a = tf.ones([4,35,8])                          

In [3]: b = tf.ones([2,35,8])                          

In [4]: c = tf.concat([a,b],axis=0)                       

In [5]: c.shape                                 
Out[5]: TensorShape([6, 35, 8])

In [6]: a = tf.ones([4,32,8])                          

In [7]: b = tf.ones([4,3,8])                          

In [8]: c = tf.concat([a,b],axis=1)                       

In [9]: c.shape                                 
Out[9]: TensorShape([4, 35, 8])

**

二 tf.stack( ) 函数–数据的堆叠,创建新的维度
**

In [2]: a = tf.ones([4,35,8])                          

In [3]: a.shape                                 
Out[3]: TensorShape([4, 35, 8])

In [4]: b = tf.ones([4,35,8])                          

In [5]: b.shape                                 
Out[5]: TensorShape([4, 35, 8])

In [6]: tf.concat([a,b],axis=-1).shape                     
Out[6]: TensorShape([4, 35, 16])

In [7]: tf.stack([a,b],axis=0).shape                      
Out[7]: TensorShape([2, 4, 35, 8])

In [8]: tf.stack([a,b],axis=3).shape                      
Out[8]: TensorShape([4, 35, 8, 2])

**

三 tf.unstack( )函数–解堆叠
**

In [16]: a = tf.ones([4,35,8])                                                                                       

In [17]: b = tf.ones([4,35,8])                                                                                       

In [18]: c = tf.stack([a,b],axis=0)                                                                                     

In [19]: a.shape,b.shape,c.shape                                                                                      
Out[19]: (TensorShape([4, 35, 8]), TensorShape([4, 35, 8]), TensorShape([2, 4, 35, 8]))

In [20]: aa,bb = tf.unstack(c,axis=0)                                                                                    

In [21]: aa.shape,bb.shape                                                                                         
Out[21]: (TensorShape([4, 35, 8]), TensorShape([4, 35, 8]))

In [22]: res = tf.unstack(c,axis=1)                                                                                     

In [23]: len(res)                                                                                              
Out[23]: 4

**

四 tf.split( ) 函数
**

In [16]: a = tf.ones([4,35,8])                                                                                       

In [17]: b = tf.ones([4,35,8])                                                                                       

In [18]: c = tf.stack([a,b],axis=0)                                                                                     

In [19]: a.shape,b.shape,c.shape                                                                                      
Out[19]: (TensorShape([4, 35, 8]), TensorShape([4, 35, 8]), TensorShape([2, 4, 35, 8]))

In [20]: aa,bb = tf.unstack(c,axis=0)                                                                                    

In [21]: aa.shape,bb.shape                                                                                         
Out[21]: (TensorShape([4, 35, 8]), TensorShape([4, 35, 8]))

In [22]: res = tf.unstack(c,axis=1)                                                                                     

In [23]: len(res)                                                                                              
Out[23]: 4

以上这篇TensorFlow2.0:张量的合并与分割实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python字典的核心底层原理讲解

Python字典的核心底层原理讲解

字典对象的核心是散列表。散列表是一个稀疏数组(总是有空白元素的数组),数组的每个单元叫做 bucket。每个 bucket 有两部分:一个是键对象的引用,一个是值对象的引用。所有 buc...

Windows 64位下python3安装nltk模块

Windows 64位下python3安装nltk模块

在网上找了各种安装教程,都没有在python3下安装nltk,于是我自己尝试着安装,算是成功了 1、首先,假设你的python3已经安装好,并且安装了numpy,matplotlib,p...

python3.5 tkinter实现页面跳转

python3.5 tkinter实现页面跳转

本文实例为大家分享了tkinter实现页面跳转的具体代码,供大家参考,具体内容如下 主函数main.py from tkinter import * from LoginPag...

Android基于TCP和URL协议的网络编程示例【附demo源码下载】

Android基于TCP和URL协议的网络编程示例【附demo源码下载】

本文实例讲述了Android基于TCP和URL协议的网络编程。分享给大家供大家参考,具体如下: 手机本身是作为手机终端使用的,因此它的计算能力,存储能力都是有限的。它的主要优势是携带方便...

python发送告警邮件脚本

python发送告警邮件脚本

python脚本为敏捷开发脚本,在zabbix监控也起到重要作用,以下是使用python脚本发送告警邮件配置方法。 脚本如下: #!/usr/bin/python #coding:u...