Tensorflow获取张量Tensor的具体维数实例

yipeiwu_com5年前Python基础

获取Tensor的维数

>>> import tensorflow as tf

>>> tf.__version__
'1.2.0-rc1'

>>> x=tf.placeholder(dtype=float32,shape=[1,2,3,4])
>>> x=tf.placeholder(dtype=tf.float32,shape=[1,2,3,4])

>>> x.shape
TensorShape([Dimension(1), Dimension(2), Dimension(3), Dimension(4)])
>>> x.get_shape()
TensorShape([Dimension(1), Dimension(2), Dimension(3), Dimension(4)])
# 返回tuple

>>> x.shape[2]
Dimension(3)
>>> x.get_shape()[2]
Dimension(3)

# 获取具体维度数值
>>> x.shape[2].value
3
>>> x.get_shape()[2].value
3
# 也可以将TensorShape变量转化为list类型,然后直接按照索引取值
>>> x.shape.as_list()
[1, 2, 3, 4]
>>> x.shape.as_list()
[1, 2, 3, 4]

# 可以与int型数值比较
>>> x.shape[2] == 3
True
>>> x.get_shape()[2] == 3
True

以上这篇Tensorflow获取张量Tensor的具体维数实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

pytorch 自定义参数不更新方式

nn.Module中定义参数:不需要加cuda,可以求导,反向传播 class BiFPN(nn.Module): def __init__(self, fpn_sizes):...

python中的插值 scipy-interp的实现代码

python中的插值 scipy-interp的实现代码

具体代码如下所示: import numpy as np from matplotlib import pyplot as plt from scipy.interpolate im...

python中enumerate函数用法实例分析

本文实例讲述了python中enumerate函数用法。分享给大家供大家参考。具体分析如下: 今日发现一个新函数 enumerate 。一般情况下对一个列表或数组既要遍历索引又要遍历元素...

python网络编程学习笔记(七):HTML和XHTML解析(HTMLParser、BeautifulSoup)

一、利用HTMLParser进行网页解析 具体HTMLParser官方文档可参考http://docs.python.org/library/htmlparser.html#HTMLPa...

python实现的简单文本类游戏实例

本文实例讲述了python实现的简单文本类游戏实现方法。分享给大家供大家参考。具体实现方法如下: ##########################################...