Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >tensorflow: Shapes and Shaping 探究

tensorflow: Shapes and Shaping 探究

作者头像
JNingWei
发布于 2018-09-28 07:38:08
发布于 2018-09-28 07:38:08
42800
代码可运行
举报
文章被收录于专栏:JNing的专栏JNing的专栏
运行总次数:0
代码可运行

定义

Tensor Transformations - Shapes and Shaping: TensorFlow provides several operations that you can use to determine the shape of a tensor and change the shape of a tensor.

tensorflow提供了一些操作,让用户可以定义和修改tensor的形状


常用API

tf.shape

  以tensor形式,返回tensor形状。

tf.shape(input, name=None, out_type=tf.int32)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tensorflow as tf

t = tf.constant(value=[[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]], dtype=tf.int32)
with tf.Session() as sess:
    print (sess.run(tf.shape(t)))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[2 2 3]

  另一种方法也可以的到类似答案:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tensorflow as tf

t = tf.constant(value=[[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]], dtype=tf.int32)
print (t.shape)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
(2, 2, 3)

tf.size

  以tensor形式,返回tensor元素总数。

tf.size(input, name=None, out_type=tf.int32)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tensorflow as tf

t = tf.ones(shape=[2, 5, 10], dtype=tf.int32)

with tf.Session() as sess:
    print (sess.run(tf.size(t)))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
100

tf.rank

  以tensor形式,返回tensor阶数。

tf.rank(input, name=None)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tensorflow as tf

t = tf.constant(value=[[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]], dtype=tf.int32)
with tf.Session() as sess:
    print (sess.run(tf.rank(t)))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
3

tf.reshape

  以tensor形式,返回重新被塑形的tensor。

tf.reshape(tensor, shape, name=None)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tensorflow as tf

t = tf.constant(value=[1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=tf.int32)
with tf.Session() as sess:
    print (sess.run(t))
    print
    print (sess.run(tf.reshape(t, [3, 3])))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[1 2 3 4 5 6 7 8 9]

[[1 2 3]
 [4 5 6]
 [7 8 9]]

tf.squeeze

  以tensor形式,返回移除指定维后的tensor。

tf.squeeze(input, axis=None, name=None, squeeze_dims=None)

  • axis=None 时: Removes dimensions of size 1 from the shape of a tensor. 将tensor中 维度为1所有维 全部移除
  • axis=[2, 4] 时: 将tensor中 维度为1第2维第4维 移除
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tensorflow as tf

t = tf.ones(shape=[1, 2, 1, 3, 1, 1], dtype=tf.int32)

with tf.Session() as sess:
    print (sess.run(tf.shape(t)))
    print
    print (sess.run(tf.shape(tf.squeeze(t))))
    print
    print (sess.run(tf.shape(tf.squeeze(t, axis=[2, 4]))))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[1 2 1 3 1 1]

[2 3]

[1 2 3 1]

tf.expand_dims

  以tensor形式,返回插入指定维后的tensor。

tf.expand_dims(input, axis=None, name=None, dim=None)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tensorflow as tf

t = tf.ones(shape=[2, 3, 5], dtype=tf.int32)

with tf.Session() as sess:
    print (sess.run(tf.shape(t)))
    print
    print (sess.run(tf.shape(tf.expand_dims(t, 0))))
    print
    print (sess.run(tf.shape(tf.expand_dims(t, 1))))
    print
    print (sess.run(tf.shape(tf.expand_dims(t, 2))))
    print
    print (sess.run(tf.shape(tf.expand_dims(t, 3))))
    print
    print (sess.run(tf.shape(tf.expand_dims(t, -1))))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[2 3 5]

[1 2 3 5]

[2 1 3 5]

[2 3 1 5]

[2 3 5 1]

[2 3 5 1]


本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年09月23日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
tensorflow | 维度转换
学习维度转换 shape 计算维度 tf.shape(input,name = None) 案例1 a = tf.constant([i for i in range(20)],shape =[2,2,5]) with tf.Session() as sess: print (sess.run(tf.shape(a))) 结果:[2 2 5] size 计算元素个数 tf.size(input,name = None) 案例2 a = tf.constant([i for i in range
努力在北京混出人样
2018/05/14
1.2K0
TensorFlow基础:常量
例如 tf.zeros,tf.ones,tf.zeros_like,tf.diag ...
lyhue1991
2020/07/20
3640
通俗易懂!使用Excel和TF实现Transformer!
中文词表:[机、器、学、习] 英文词表[deep、machine、learning、chinese]
石晓文
2019/06/17
4860
通俗易懂!使用Excel和TF实现Transformer!
斯坦福tensorflow教程(二) tensorflow相关运算1.认识下TensorBoard2.常量op3. 数学运算数据类型
1.认识下TensorBoard TensorFlow不仅是一个软件库,而是一整套包括TensorFlow、TensorBoard、Tensor Serving在内的软件包。为了更大程度地利用TensorFlow,我们应该了解如何将它们串联起来应用。在和一部分,我们来探索下TensorBoard。 TensorBoard是一个图(graph)可视化软件,在(安装TensorFlow的时候会默认安装)。下面是谷歌的介绍: The computations you'll use TensorFlow for
致Great
2018/06/14
8100
基于TensorFlow的深度学习系列教程 2——常量Constant
在tensorflow中,数据分为几种类型: 常量Constant、变量Variable、占位符Placeholder。其中:
用户1154259
2019/07/02
7650
tensorflow编程: Constants, Sequences, and Random Values
  注意: start 和 stop 参数都必须是 浮点型;     取值范围也包括了 stop; tf.lin_space 等同于 tf.linspace。
JNingWei
2018/09/28
4510
TensorFlow2 一小时学会基本操作 2
https://iamarookie.blog.csdn.net/article/details/117651502
润森
2022/09/22
3520
TensorFlow2 一小时学会基本操作 2
tensorflow编程: Running Graphs
  A class for running TensorFlow operations.   这是一个类,执行 tensorflow 中的 op 。它里面定义了 run()、extend()、close()、__init__() 等方法。
JNingWei
2018/09/28
5530
使用一维数据构造简单卷积神经网络
神经网络对于一维数据非常重要,时序数据集、信号处理数据集和一些文本嵌入数据集都是一维数据,会频繁的使用到神经网络。我们在此利用一组一维数据构造卷积层-最大池化层-全连接层的卷积神经网络。希望给大家使用CNN处理一维数据一些帮助。
演化计算与人工智能
2020/08/14
1.7K0
教程 | 维度、广播操作与可视化:如何高效使用TensorFlow
选自GitHub 机器之心编译 参与:Nurhachu Null、李泽南 本文从 Tensorflow 基础、理解静态维度和动态维度、广播操作(Broadingcast 的好处和坏处)、使用 Python 操作的原型内核和先进的可视化等几个方面详细梳理如何高效使用TensorFlow。 Tensorflow 基础 TensorFlow 和其他诸如 numpy 之类的数学计算库的根本区别在于:在 TensorFlow 中,运算操作是符号化的。这是一个强大的思想,它能够让 TensorFlow 做任何事情(例如
机器之心
2018/05/09
1.5K0
Tensorflow技术点整理(二)
这里跟PyTorch不同的是序号定义的不同,PyTorch是上下定义位置,而Tensorflow是左右定义位置。
算法之名
2022/03/24
4670
Tensorflow技术点整理(二)
tensorflow运行mnist的一些
最近在tensorflow环境下用CNN来实现mnist,里面设计了一些tensorflow的函数,在之后的学习中肯定会经常使用,因此记录整理下来。
py3study
2020/01/20
5330
TensorFlow2.0(1):基本数据结构——张量
TensorFlow2.0版本已经发布,虽然不是正式版,但预览版都发布了,正式版还会远吗?相比于1.X,2.0版的TensorFlow修改的不是一点半点,这些修改极大的弥补了1.X版本的反人类设计,提升了框架的整体易用性,绝对好评!
Ai学习的老章
2019/12/23
1.6K0
使用二维数据构造简单卷积神经网络
使用二维数据构造简单卷积神经网络 图像和一些时序数据集都可以用二维数据的形式表现,我们此次使用随机分布的二位数据构造一个简单的CNN—网络卷积-最大池化-全连接 参考代码 # Implementing Different Layers # --------------------------------------- # # We will illustrate how to use different types # of layers in TensorFlow # # The layers of i
演化计算与人工智能
2020/08/14
8680
张量拼接_调整维度_切片
tf.concat的作用主要是将向量按指定维连起来,其余维度不变;而1.0版本以后,函数的用法变成:
狼啸风云
2019/07/01
1.3K0
TF入门02-TensorFlow Ops
我们首先介绍一下TensorBoard的使用,然后介绍TensorFlow的基本ops,之后介绍张量的数据类型,最后介绍一下如何将自己的输入导入模型。
公众号-不为谁写的歌
2020/07/23
1.7K0
[阿里DIN]从模型源码梳理TensorFlow的形状相关操作
本文基于阿里推荐 DIN 和 DIEN 代码,梳理了下深度学习一些概念,以及TensorFlow中的相关实现。
罗西的思考
2020/11/24
9180
【tensorflow2.0】张量的结构操作
张量数学运算主要有:标量运算,向量运算,矩阵运算。另外我们会介绍张量运算的广播机制。
西西嘛呦
2020/08/26
2.3K0
[阿里DIN] 从模型源码梳理TensorFlow的乘法相关概念
本文基于阿里推荐 DIN 和 DIEN 代码,梳理了下深度学习一些概念,以及TensorFlow中的相关实现。
罗西的思考
2020/11/11
1.8K0
相关推荐
tensorflow | 维度转换
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档