Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在图构造时获得张量(在TensorFlow中)的维数?_Python_Tensorflow_Deep Learning_Tensor - Fatal编程技术网

Python 如何在图构造时获得张量(在TensorFlow中)的维数?

Python 如何在图构造时获得张量(在TensorFlow中)的维数?,python,tensorflow,deep-learning,tensor,Python,Tensorflow,Deep Learning,Tensor,我正在尝试一个不符合预期的操作 graph = tf.Graph() with graph.as_default(): train_dataset = tf.placeholder(tf.int32, shape=[128, 2]) embeddings = tf.Variable( tf.random_uniform([50000, 64], -1.0, 1.0)) embed = tf.nn.embedding_lookup(embeddings, train_datas

我正在尝试一个不符合预期的操作

graph = tf.Graph()
with graph.as_default():
  train_dataset = tf.placeholder(tf.int32, shape=[128, 2])
  embeddings = tf.Variable(
    tf.random_uniform([50000, 64], -1.0, 1.0))
  embed = tf.nn.embedding_lookup(embeddings, train_dataset)
  embed = tf.reduce_sum(embed, reduction_indices=0)
所以我需要知道张量的维数
embed
。我知道这可以在运行时完成,但对于这样一个简单的操作来说,工作量太大了。做这件事最简单的方法是什么?

Tensor.get_shape()
from

:


只需打印构建后嵌入图(ops),无需运行:

import tensorflow as tf

...

train_dataset = tf.placeholder(tf.int32, shape=[128, 2])
embeddings = tf.Variable(
    tf.random_uniform([50000, 64], -1.0, 1.0))
embed = tf.nn.embedding_lookup(embeddings, train_dataset)
print (embed)
这将显示嵌入张量的形状:

Tensor("embedding_lookup:0", shape=(128, 2, 64), dtype=float32)
通常,在训练模型之前,最好检查所有张量的形状。

一个访问值的函数:

def shape(tensor):
    s = tensor.get_shape()
    return tuple([s[i].value for i in range(0, len(s))])
例如:

batch_size, num_feats = shape(logits)

我看到大多数人对
tf.shape(tensor)
tensor.get\u shape()
让我们说清楚:

  • tf.shape
  • tf.shape
    用于动态形状。如果张量的形状是可更改的,请使用它。 例如:输入是宽度和高度可变的图像,我们希望将其大小调整为其大小的一半,然后我们可以编写如下内容:
    new_height=tf.shape(图像)[0]/2

  • tensor.get_形
  • tensor.get_shape
    用于固定形状,这意味着可以在图中推导出张量的形状

    结论:
    tf.shape
    几乎可以在任何地方使用,但是
    t.get\u shape
    只能从图形中推断形状

    让我们把它说得非常简单。如果您想要一个单一的数字作为维度的数量,如
    2、3、4等,
    ,那么只需使用
    tf.rank()
    。但是,如果你想要张量的精确形状,那么就使用
    tensor.get\u shape()


    tf.shape方法是一种张量流静态方法。然而,对于张量类,还有一种方法是获得_形。看


    虽然我在你发布你的答案之前给出的答案是正确的,但你的答案提供了更多关于张量的信息,而不仅仅是它的形状,因此,我认为这是正确的答案;)如果有人想知道:
    tf.shape(c)
    返回表示
    c
    形状的一维整数张量。在这个答案给出的例子中,
    tf.shape(c)
    返回
    张量(“shape:0”,shape=(2,),dtype=int32)
    @nobar如果维度是
    None
    (即,如果未指定),您可能需要使用
    tf.shape(c)
    。例如,如果
    a=tf.placeholder(tf.int32,(None,2))
    ,并且您运行
    tf.Session().run(tf.constant(a.get_shape().as_list()[0]),{a:[[1,2]}
    您将得到错误,但您可以通过以下方式获得维度:
    tf.Session().run(tf.shape(a)[0],{a:[1,2]})
    。真的没有-我只是想尽可能简洁地解释;-)
    返回元组(tensor.get_shape().as_list())
    如果需要元组,或者直接返回python列表,如
    返回tensor.get_shape().as_list()
    batch_size, num_feats = shape(logits)
    
    with tf.Session() as sess:
       arr = tf.random_normal(shape=(10, 32, 32, 128))
       a = tf.random_gamma(shape=(3, 3, 1), alpha=0.1)
       print(sess.run([tf.rank(arr), tf.rank(a)]))
       print(arr.get_shape(), ", ", a.get_shape())     
    
    
    # for tf.rank()    
    [4, 3]
    
    # for tf.get_shape()
    Output: (10, 32, 32, 128) , (3, 3, 1)