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的维度等相关属性的,所以这里会使用.

最新文章

  1. Codeforces Round #361 (Div. 2) A
  2. Linux笔记之——Linux关机命令详解(转)
  3. Tomcat7下出现The requested resource(/)is not available
  4. code manager tools git的使用;
  5. 【ImageMagick】ImageMagick命令行工具
  6. 16 Socket通信(简单例子)
  7. oracle中导入导出数据备份数据库
  8. CLR的组成和运转
  9. “Cannot load php5apache2_4.dll into server”问题的解决方法
  10. Backbox Linux简介与配置内网IP
  11. SpringBoot使用ELK日志收集
  12. Vue:渲染、指令、事件、组件、Props
  13. ubuntu 禁用 nouveau 方法
  14. python的学习之路day3
  15. SpringBoot修改默认端口号,session超时时间
  16. HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)
  17. Flask 源代码阅读笔记
  18. java基础第6天
  19. STL之hashtable源代码剖析
  20. 虚拟机中Lvs配置

热门文章

  1. Navicat 用ssh通道连接时总是报错 (报错信息:SSH:expected key exchange group packet form serve
  2. 131A
  3. python学习之数组二
  4. Unable to register MBean [HikariDataSource (HikariPool-0)] with key &#39;dataSou rce&#39;; nested exception is javax.management.InstanceAlreadyExistsException: com.z axxer.hikari:name=dataSource,type=HikariDa
  5. jQuery实现购物车物品数量的加减
  6. percona-toolkit工具的使用
  7. EasyUI相关知识点整理
  8. linux中make的有关规则的特性
  9. SpringBoot使用HttpClient远程调用
  10. LeetCode #002# Add Two Numbers(js描述)