基于tf.shape(tensor)和tensor.shape()的区别说明


Posted in Python onJune 30, 2020

#tf.shape(tensor)和tensor.shape()的区别

a=tf.zeros([4,5,4,5,6])
print(type(a.shape))
print(a.shape.ndims)#多少个维度
print(a.shape.as_list())#返回列表
print(type(tf.shape(a)))
print(type(tf.shape(a)[0]))
b=a.shape.as_list()
c=tf.shape(a)
b[1]=tf.shape(a)[1]
print(b)
sess=tf.Session()
d=sess.run(c)
print(d)
outputs:
<class 'tensorflow.python.framework.tensor_shape.TensorShape'>
5
[4, 5, 4, 5, 6]
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'tensorflow.python.framework.ops.Tensor'>
[4, <tf.Tensor 'strided_slice_1:0' shape=() dtype=int32>, 4, 5, 6]
[4 5 4 5 6]

其中tf.shape(tensor)使用的是动态的,即必须要在session中运行后才能显示出来,但是tensor.shape()是静态的,即通过定义的shape可以惊天的运行出来。

原因:在我们定义的时候,比如进行placeholder的时候我们可能会定义某些维度为None,在静态的时候是看不出来的,只能在运行的时候找到维度。

**使用:**可以在获得某些tensor的维度的时候进行检验,防止维度为None。

补充知识:tensorflow.python.framework.tensor_shape.TensorShape 类

TensorShape 是tensorflow中关于张量shape的类(class).

使用示例如下:

import tensorflow.compat.v1 as tf
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import constant_op
 
tensor_test1=[10,10,10]
tensor_test2 = [None,10,10]
 
p1 = tensor_shape.as_shape(tensor_test1) # 得到的是一个类实例,该类实例包含一个属性,是 tensor_test1 的value
const = constant_op.constant(p1.as_list())
 
print("type(p1) = ",type(p1))
print("p1 = ",p1) # 使用p1时会自动调用p1中的value属性
print("p1.is_fully_defined() = ",p1.is_fully_defined())# is_fully_defined 是 TensorShape 类的一个内部函数
print("p1.ndims = ",p1.ndims) # ndims 也是TensorShape的一个属性值
print("p1.as_list() = ",p1.as_list()) # 把TensorShape的value属性转换成python中的list类型
print("const = ",const)

结果如下:

type(p1) = <class 'tensorflow.python.framework.tensor_shape.TensorShape'>
p1 = (10, 10, 10)
p1.is_fully_defined() = True
p1.ndims = 3
p1.as_list() = [10, 10, 10]
const = Tensor("Const:0", shape=(3,), dtype=int32)

以上这篇基于tf.shape(tensor)和tensor.shape()的区别说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python正则分组的应用
Nov 10 Python
详解Python网络爬虫功能的基本写法
Jan 28 Python
mysql 之通过配置文件链接数据库
Aug 12 Python
TensorFlow 滑动平均的示例代码
Jun 19 Python
Python迭代器与生成器基本用法分析
Jul 26 Python
解决安装pycharm后不能执行python脚本的问题
Jan 19 Python
Python 实现王者荣耀中的敏感词过滤示例
Jan 21 Python
python2.7使用plotly绘制本地散点图和折线图
Apr 02 Python
numpy.ndarray 实现对特定行或列取值
Dec 05 Python
在Python3.74+PyCharm2020.1 x64中安装使用Kivy的详细教程
Aug 07 Python
利用python绘制中国地图(含省界、河流等)
Sep 21 Python
基于flask实现五子棋小游戏
May 25 Python
Tensorflow全局设置可见GPU编号操作
Jun 30 #Python
Python logging模块异步线程写日志实现过程解析
Jun 30 #Python
浅谈多卡服务器下隐藏部分 GPU 和 TensorFlow 的显存使用设置
Jun 30 #Python
Tensorflow中批量读取数据的案列分析及TFRecord文件的打包与读取
Jun 30 #Python
使用Tensorflow-GPU禁用GPU设置(CPU与GPU速度对比)
Jun 30 #Python
keras的backend 设置 tensorflow,theano操作
Jun 30 #Python
浅谈TensorFlow中读取图像数据的三种方式
Jun 30 #Python
You might like
一棵php的类树(支持无限分类)
2006/10/09 PHP
安装PHP可能遇到的问题“无法载入mysql扩展” 的解决方法
2007/04/16 PHP
php shell超强免杀、减少体积工具实现代码
2012/10/16 PHP
PHP提示Warning:phpinfo() has been disabled函数禁用的解决方法
2014/12/17 PHP
PHP正则表达式入门教程(推荐)
2016/05/18 PHP
laravel-admin表单提交隐藏一些数据,回调时获取数据的方法
2019/10/08 PHP
JQuery自适应IFrame高度(支持嵌套 兼容IE,ff,safafi,chrome)
2011/03/28 Javascript
js工具方法弹出蒙版
2013/05/08 Javascript
JS实现表格数据各种搜索功能的方法
2015/03/03 Javascript
使用 stylelint检查CSS_StyleLint
2016/04/28 Javascript
详解windows下vue-cli及webpack 构建网站(三)使用组件
2017/06/17 Javascript
在vue项目中使用Nprogress.js进度条的方法
2018/01/31 Javascript
详解Vue SPA项目优化小记
2018/07/03 Javascript
AngularJS ui-router刷新子页面路由的方法
2018/07/23 Javascript
Vue绑定内联样式问题
2018/10/17 Javascript
ES6学习笔记之字符串、数组、对象、函数新增知识点实例分析
2020/01/22 Javascript
Antd的table组件表格的序号自增操作
2020/10/27 Javascript
[04:54]DOTA2 2017国际邀请赛:上届冠军WINGS采访短片
2017/08/09 DOTA
python将文本转换成图片输出的方法
2015/04/28 Python
python中itertools模块zip_longest函数详解
2018/06/12 Python
python 输出所有大小写字母的方法
2019/01/02 Python
Python实现爬取马云的微博功能示例
2019/02/16 Python
python os.path.isfile()因参数问题判断错误的解决
2019/11/29 Python
python读取raw binary图片并提取统计信息的实例
2020/01/09 Python
python 6.7 编写printTable()函数表格打印(完整代码)
2020/03/25 Python
Python csv文件记录流程代码解析
2020/07/16 Python
css3实现背景颜色渐变让图片不再是唯一的实现方式
2012/12/18 HTML / CSS
阿迪达斯德国官方网站:adidas德国
2017/07/12 全球购物
凯撒娱乐:Caesars Entertainment
2018/02/23 全球购物
商场端午节活动方案
2014/01/29 职场文书
水利学院求职自荐书
2014/02/01 职场文书
六一亲子活动总结
2014/07/01 职场文书
我与祖国共奋进演讲稿
2014/09/13 职场文书
村党支部书记四风问题个人对照检查材料思想汇报
2014/10/06 职场文书
三下乡个人总结
2015/03/04 职场文书
Shell脚本一键安装Nginx服务自定义Nginx版本
2022/03/20 Servers