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

时间:2022-10-13 17:46:46

获取Tensor的维数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
>>> 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的具体维数实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/tcx1992/article/details/80249992