『TensorFlow』张量尺寸获取
tf.shape(a)和a.get_shape()比较
相同点:都可以得到tensor a的尺寸
不同点:tf.shape()中a 数据的类型可以是tensor, list, array
a.get_shape()中a的数据类型只能是tensor,且返回的是一个元组(tuple)
import tensorflow as tf
import numpy as np x=tf.constant([[1,2,3],[4,5,6]]
y=[[1,2,3],[4,5,6]]
z=np.arange(24).reshape([2,3,4])) sess=tf.Session()
# tf.shape()
x_shape=tf.shape(x) # x_shape 是一个tensor
y_shape=tf.shape(y) # <tf.Tensor 'Shape_2:0' shape=(2,) dtype=int32>
z_shape=tf.shape(z) # <tf.Tensor 'Shape_5:0' shape=(3,) dtype=int32>
print sess.run(x_shape) # 结果:[2 3]
print sess.run(y_shape) # 结果:[2 3]
print sess.run(z_shape) # 结果:[2 3 4] # a.get_shape()
# 返回的是TensorShape([Dimension(2), Dimension(3)]),
# 不能使用 sess.run() 因为返回的不是tensor 或string,而是元组
x_shape=x.get_shape()
x_shape=x.get_shape().as_list() # 可以使用 as_list()得到具体的尺寸,x_shape=[2 3]
y_shape=y.get_shape() # AttributeError: 'list' object has no attribute 'get_shape'
z_shape=z.get_shape() # AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'
或者a.shape.as_list()
tf.shape(x)
tf.shape()中x数据类型可以是tensor,list,array,返回是一个tensor.
shape=tf.placeholder(tf.float32, shape=[None, 227,227,3] )
我们经常会这样来feed数据,如果在运行的时候想知道None到底是多少,这时候,只能通过tf.shape(x)[0]这种方式来获得.
由于返回的时tensor,所以我们可以使用其他tensorflow节点操作进行处理,如下面的转置卷积中,使用stack来合并各个shape的分量,
def conv2d_transpose(x, input_filters, output_filters, kernel, strides):
with tf.variable_scope('conv_transpose'): shape = [kernel, kernel, output_filters, input_filters]
weight = tf.Variable(tf.truncated_normal(shape, stddev=0.1), name='weight') batch_size = tf.shape(x)[0]
height = tf.shape(x)[1] * strides
width = tf.shape(x)[2] * strides
output_shape = tf.stack([batch_size, height, width, output_filters])
return tf.nn.conv2d_transpose(x, weight, output_shape, strides=[1, strides, strides, 1], name='conv_transpose')
tensor.get_shape()
只有tensor有这个方法, 返回是一个tuple。也正是由于返回的是TensorShape([Dimension(2), Dimension(3)])这样的元组,所以可以调用as_list化为[2, 3]样list,或者get_shape()[i].value得到具体值.
tensor.set_shape()
设置tensor的shape,一般不会用到,在tfrecode中,由于解析出来的tensor不会被设置shape,后续的函数是需要shape的维度等相关属性的,所以这里会使用.
『TensorFlow』张量尺寸获取的更多相关文章
- 『TensorFlow』张量拼接_调整维度_切片
1.tf.concat tf.concat的作用主要是将向量按指定维连起来,其余维度不变:而1.0版本以后,函数的用法变成: t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, ...
- 『TensorFlow』专题汇总
TensorFlow:官方文档 TensorFlow:项目地址 本篇列出文章对于全零新手不太合适,可以尝试TensorFlow入门系列博客,搭配其他资料进行学习. Keras使用tf.Session训 ...
- 『TensorFlow』流程控制
『PyTorch』第六弹_最小二乘法对比PyTorch和TensorFlow TensorFlow 控制流程操作 TensorFlow 提供了几个操作和类,您可以使用它们来控制操作的执行并向图中添加条 ...
- 『TensorFlow』模型保存和载入方法汇总
『TensorFlow』第七弹_保存&载入会话_霸王回马 一.TensorFlow常规模型加载方法 保存模型 tf.train.Saver()类,.save(sess, ckpt文件目录)方法 ...
- 『TensorFlow』TFR数据预处理探究以及框架搭建
一.TFRecord文件书写效率对比(单线程和多线程对比) 1.准备工作 # Author : Hellcat # Time : 18-1-15 ''' import os os.environ[&q ...
- 『TensorFlow』SSD源码学习_其五:TFR数据读取&数据预处理
Fork版本项目地址:SSD 一.TFR数据读取 创建slim.dataset.Dataset对象 在train_ssd_network.py获取数据操作如下,首先需要slim.dataset.Dat ...
- 『TensorFlow』滑动平均
滑动平均会为目标变量维护一个影子变量,影子变量不影响原变量的更新维护,但是在测试或者实际预测过程中(非训练时),使用影子变量代替原变量. 1.滑动平均求解对象初始化 ema = tf.train.Ex ...
- 『TensorFlow』读书笔记_降噪自编码器
『TensorFlow』降噪自编码器设计 之前学习过的代码,又敲了一遍,新的收获也还是有的,因为这次注释写的比较详尽,所以再次记录一下,具体的相关知识查阅之前写的文章即可(见上面链接). # Aut ...
- 『TensorFlow』梯度优化相关
tf.trainable_variables可以得到整个模型中所有trainable=True的Variable,也是自由处理梯度的基础 基础梯度操作方法: tf.gradients 用来计算导数.该 ...
随机推荐
- linux sftp远程上传文件
1.打开xshell 点击“新建文件传输”,如下图: 中间可能会出现下面的提示框,直接关掉即可: 2.切换到远程你要传输文件的目的地 命令:cd 你的路径 3.切换到本地文件所在目录 命令:lcd ...
- C# 并发编程 · 经典实例
http://www.cnblogs.com/savorboard/p/csharp-concurrency-cookbook.html 异步基础 任务暂停,休眠 异步方式暂停或者休眠任务,可以使用 ...
- 数据模型model设置、生成数据迁移文件、执行数据迁移文件
一.model的配置 1.创建数据库 2.安装pymysql 3.修改配置文件 数据库连接配置 DATABASES = {'default': {'ENGINE': 'django.db.backen ...
- c#之如何操作excel
可使用EPPlus类库,下载地址如下: http://epplus.codeplex.com/ 也可以在这里下载: https://files.cnblogs.com/files/jietian331 ...
- noip单词接龙
看了许多题解都好长啊,自不量力的来贴一下代码 (震惊于这都能ac...) 这道题的思路是先从字符串中找有重部分然后直接比较剩下的部分,比较的数据也可以用来计算数值 其实满水的题 总之看注释啦(竟然能耐 ...
- (Review cs231n) Training of Neural Network2
FFDNet---matlab 调用并批处理 format compact; global sigmas; % input noise level or input noise level map a ...
- bzoj2880
打公式好麻烦 QAQ 为了节省时间去复习,原谅我引用一下别人的博客...http://blog.csdn.net/acdreamers/article/details/8542292 #include ...
- Docker Swarm nginx 集群搭建
环境1: 系统:Linux Centos 7.4 x64 内核:Linux docker 3.10.0-693.2.2.el7.x86_64 Docker 版本:18.09.1 redis 版本:ng ...
- 进程管理工具supervisor的使用
centos 6.5, python 2.6, supervisor 3.3.1: Linux下后台运行程序通常的做法是用nohub,然后配以进程的检测来实现服务式的操作,但其实有更好的选择super ...
- ldap 集成harbor
harbor: 1.6 默认配置文件在harbor.cfg,我们可以先不添加配置,直接在harbor web界面进行配置(harbor 1.6 如果db 启动失败提示postgresql 数据目录已存 ...