Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.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_List Comprehension_Tensor - Fatal编程技术网

Python tensorflow中的等价表理解

Python tensorflow中的等价表理解,python,tensorflow,list-comprehension,tensor,Python,Tensorflow,List Comprehension,Tensor,如何使用字典将包含N int的秩1张量修改为包含N个大小为M的向量的秩2张量 在python中,类似于: dict = {1 : [1,2,3] , 2 : [3,2,1]} array1 = np.array([1,1,2,1,2]) array2 = np.array([dict[x] for x in array1]) 但是我不能迭代张量(除了while循环,但我不认为这是最佳解决方案)如果你的dict是 dict = {1: tf.constant([1, 2, 3]), 2: tf.

如何使用字典将包含N int的秩1张量修改为包含N个大小为M的向量的秩2张量

在python中,类似于:

dict = {1 : [1,2,3] , 2 : [3,2,1]}
array1 = np.array([1,1,2,1,2])
array2 = np.array([dict[x] for x in array1])

但是我不能迭代张量(除了while循环,但我不认为这是最佳解决方案)

如果你的dict是

dict = {1: tf.constant([1, 2, 3]), 2: tf.constant([1, 2, 3])}
tensor1 = tf.constant([1, 1, 2, 1, 2])
那么像这样的东西应该可以完成这项工作:

vals = [dict[tensor1[k]] for k in range(tensor1.get_shape().as_list()[0])]
tensor2 = tf.stack(vals, axis=0)

如果你有未知的维度,它会变得更复杂。例如,如果张量1的形状未知,我看不到不涉及tf.while_loop的解决方案。

我想我遗漏了一些东西我得到了错误键错误:我得到了类型错误:如果启用了张量相等,张量是不可损坏的。相反,使用tensor.experimental_ref()作为键。