如何在图构建时获取张量(在 TensorFlow 中)的维度? [英] How to get the dimensions of a tensor (in TensorFlow) at graph construction time?
本文介绍了如何在图构建时获取张量(在 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)
所以我需要知道张量 embed
的尺寸.我知道它可以在运行时完成,但是对于这样一个简单的操作来说工作量太大了.什么是更简单的方法?
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屋!
查看全文