Tensorflow python:访问张量中的单个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35146444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tensorflow python : Accessing individual elements in a tensor
提问by cipher42
This question is with respect to accessing individual elements in a tensor, say [[1,2,3]]. I need to access the inner element [1,2,3] (This can be performed using .eval() or sess.run()) but it takes longer when the size of the tensor is huge)
这个问题是关于访问张量中的单个元素,比如 [[1,2,3]]。我需要访问内部元素 [1,2,3](这可以使用 .eval() 或 sess.run() 执行)但是当张量的大小很大时需要更长的时间)
Is there any method to do the same faster?
有什么方法可以更快地做同样的事情吗?
Thanks in Advance.
提前致谢。
回答by Sorin
I suspect it's the rest of the computation that takes time, rather than accessing one element.
我怀疑是其余的计算需要时间,而不是访问一个元素。
Also the result might require a copy from whatever memory is stored in, so if it's on the graphics card it will need to be copied back to RAM first and then you get access to your element. If this is the case you might skip it by adding an tensorflow operation to take the first element, and only return that.
此外,结果可能需要从存储在任何内存中的副本进行复制,因此如果它在图形卡上,则需要先将其复制回 RAM,然后才能访问您的元素。如果是这种情况,您可以通过添加 tensorflow 操作来获取第一个元素并仅返回该元素来跳过它。
回答by mrry
There are two main ways to access subsets of the elements in a tensor, either of which should work for your example.
有两种主要方法可以访问张量中元素的子集,这两种方法都适用于您的示例。
Use the indexing operator (based on
tf.slice()
) to extract a contiguous slice from the tensor.input = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) output = input[0, :] print sess.run(output) # ==> [1 2 3]
The indexing operator supports many of the same slice specifications as NumPy does.
Use the
tf.gather()
op to select a non-contiguous slice from the tensor.input = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) output = tf.gather(input, 0) print sess.run(output) # ==> [1 2 3] output = tf.gather(input, [0, 2]) print sess.run(output) # ==> [[1 2 3] [7 8 9]]
Note that
tf.gather()
only allows you to select whole slices in the 0th dimension (whole rows in the example of a matrix), so you may need totf.reshape()
ortf.transpose()
your input to obtain the appropriate elements.
使用索引运算符(基于
tf.slice()
)从张量中提取连续切片。input = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) output = input[0, :] print sess.run(output) # ==> [1 2 3]
索引运算符支持许多与 NumPy 相同的切片规范。
使用
tf.gather()
op 从张量中选择一个不连续的切片。input = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) output = tf.gather(input, 0) print sess.run(output) # ==> [1 2 3] output = tf.gather(input, [0, 2]) print sess.run(output) # ==> [[1 2 3] [7 8 9]]
请注意,
tf.gather()
仅允许您选择第 0 维中的整个切片(矩阵示例中的整行),因此您可能需要tf.reshape()
或tf.transpose()
您的输入来获取适当的元素。
回答by Oleksandr Khryplyvenko
You simply can't get valueof 0th element of [[1,2,3]] without run()-ning or eval()-ing an operation which would be getting it. Because before you 'run' or 'eval', you have only a description how to get this inner element(because TF uses symbolic graphs/calculations). So even if you would use tf.gather/tf.slice, you still would have to get valuesof these operations via eval/run. See @mrry's answer.
如果没有 run()-ning 或 eval()-ing 的操作,您根本无法获得[[1,2,3]] 的第 0 个元素的值。因为在您“运行”或“评估”之前,您只有一个描述如何获取此内部元素(因为 TF 使用符号图/计算)。因此,即使您使用 tf.gather/tf.slice,您仍然必须通过 eval/run获取这些操作的值。请参阅@mrry 的回答。