如何在图构建时获取张量的尺寸(在TensorFlow中)? [英] How to get the dimensions of a tensor (in TensorFlow) at graph construction time?

查看:297
本文介绍了如何在图构建时获取张量的尺寸(在TensorFlow中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个操作不正常的操作。

I am trying an Op that is not behaving as expected.

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)

所以我需要知道张量嵌入的尺寸。我知道可以在运行时完成此操作,但是对于这样一个简单的操作而言,它的工作量很大。

So I need to know the dimensions of the Tensor embed. I know that it can be done at the run time but it's too much work for such a simple operation. What's the easier way to do it?

推荐答案

Tensor.get_shape()来自此帖子

摘自文档

c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
print(c.get_shape())
==> TensorShape([Dimension(2), Dimension(3)])

这篇关于如何在图构建时获取张量的尺寸(在TensorFlow中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆