张量的访问元素 [英] Access elements of a Tensor
问题描述
我有以下TensorFlow张量.
I have the following TensorFlow tensors.
tensor1 = tf.constant(np.random.randint(0,255, (2,512,512,1)), dtype='int32') #All elements in range [0,255]
tensor2 = tf.constant(np.random.randint(0,255, (2,512,512,1)), dtype='int32') #All elements in range [0,255]
tensor3 = tf.keras.backend.flatten(tensor1)
tensor4 = tf.keras.backend.flatten(tensor2)
tensor5 = tf.constant(np.random.randint(0,255, (255,255)), dtype='int32') #All elements in range [0,255]
我希望使用张量3和张量4中存储的值创建一个元组,并查询张量5中该元组给定位置的元素.例如,假设张量3中的第0个元素,即tensor3 [0 ] = 5和tensor4 [0] = 99. 因此,元组变为(5,99).我希望在张量5中查找元素(5,99)的值.我希望以批量处理的方式对Tensor3和Tensor4中的所有元素进行处理.那就是我不想遍历(len(Tensor3))范围内的所有值.为了达到这个目的,我做了以下工作.
I wish to use the values stored in tensor 3 and tensor 4 to make a tuple and query the element at position given by the tuple in tensor 5. For example, let's say 0th element in tensor 3, that is tensor3[0]=5 and tensor4[0]=99. So the tuple becomes (5,99). I wish to look up the value of element (5,99) in tensor 5. I wish to do it for all elements in Tensor3 and Tensor4 in a batch processing manner. That is I do not want to loop over all values in the range of (len(Tensor3)). I did the following to achieve this.
tensor6 = tensor5[tensor3[0],tensor4[0]]
但是tensor6的形状为(255,255),正如我希望得到的形状的张量(len(tensor3),len(tensor3))一样.我想在len(tensor3)中所有可能的位置评估tensor5.那是在(0,0),...(1000,1000),....(2000,2000),...
.我正在使用TensorFlow 1.12.0版本.我该如何实现?
But tensor6 has the shape (255,255) where as I was hoping to get a tensor of shape (len(tensor3),len(tensor3)). I wanted to evaluate tensor5 at all possible locations in len(tensor3). That is at (0,0),...(1000,1000),....(2000,2000),...
. I am using TensorFlow version 1.12.0. How can I achieve this?
推荐答案
我设法在Tensorflow v 1.12中工作了一些,但请告诉我它是否是预期的代码:
I have managed to get something working in Tensorflow v 1.12, but do let me know if it is the expected code:
import tensorflow as tf
print(tf.__version__)
import numpy as np
tensor1 = tf.constant(np.random.randint(0,255, (2,512,512,1)), dtype='int32') #All elements in range [0,255]
tensor2 = tf.constant(np.random.randint(0,255, (2,512,512,1)), dtype='int32') #All elements in range [0,255]
tensor3 = tf.keras.backend.flatten(tensor1)
tensor4 = tf.keras.backend.flatten(tensor2)
tensor5 = tf.constant(np.random.randint(0,255, (255,255)), dtype='int32') #All elements in range [0,255]
elems = (tensor3, tensor4)
a = tf.map_fn(lambda x: tensor5[x[0], x[1]], elems, dtype=tf.int32)
print(tf.Session().run(a))
基于下面的注释,我想对代码中使用的map_fn
添加一个说明.由于没有eager_execution,不支持for
循环,因此map_fn
等效于for
循环.
Based on the comment below I'd like to add an explanation for the map_fn
used in the code. Since for
loops are not supported without eager_execution, map_fn
is (sort of) equivalent to for
loops.
A map_fn
具有以下参数:operation_performed
,input_arguments
,optional_dtype
.实际情况是,沿着input_arguments
中的值的长度(必须包含一个可迭代的对象)运行for
循环,然后对每个获得的值执行operation_performed
.如需进一步的说明,请参阅 docs .
A map_fn
has the following parameters: operation_performed
, input_arguments
, optional_dtype
. What happens under the hood is that a for
loop is run along the length of the values in input_arguments
(which must contain an iterable object) and then for each value obtained operation_performed
is performed. For further clarification please refer docs.
函数的参数名称是我解释它们的方式,正如我想了解的那样,官方文档中未给出. :)
The names given to the arguments of the function is my way of interpreting them, as I'd like understand it, and is not given in the official docs. :)
这篇关于张量的访问元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!