Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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
在tensorflow中是否有名为“index_select”的pytorch的等效函数_Tensorflow_Pytorch - Fatal编程技术网

在tensorflow中是否有名为“index_select”的pytorch的等效函数

在tensorflow中是否有名为“index_select”的pytorch的等效函数,tensorflow,pytorch,Tensorflow,Pytorch,我试着把pytorch代码翻译成tensorflow。所以我想知道tensorflow中是否有一个与pytorch等价的函数,名为index_select,我还没有发现类似的api可以直接实现它,但我们可以使用tf.slice来实现它 def tf_索引\u选择输入、尺寸、索引: 输入张量:输入张量 小尺寸:尺寸 指标列表:所选指标列表 shape=输入\获取\ shape.as\列表 如果dim==-1: 尺寸=透镜形状-1 形状[dim]=1 tmp=[] 对于索引中的idx: 开始=[0]

我试着把pytorch代码翻译成tensorflow。所以我想知道tensorflow中是否有一个与pytorch等价的函数,名为index_select,我还没有发现类似的api可以直接实现它,但我们可以使用tf.slice来实现它

def tf_索引\u选择输入、尺寸、索引: 输入张量:输入张量 小尺寸:尺寸 指标列表:所选指标列表 shape=输入\获取\ shape.as\列表 如果dim==-1: 尺寸=透镜形状-1 形状[dim]=1 tmp=[] 对于索引中的idx: 开始=[0]*lenshape 开始[dim]=idx tmp.appendtf.sliceinput、begin、shape res=tf.concattmp,轴=dim 返回res 下面是一个例子来说明等价性

导入tensorflow作为tf 进口火炬 将numpy作为np导入 a=np.arange2*3*4.重塑2,3,4 尺寸=1 指数=[0,2] 数组[[0,1,2,3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]] 皮托克 res=torch.tensora.index\u选择dim,torch.tensorindices 张量[[0,1,2,3], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [20, 21, 22, 23]]] 张量流 res=tf_index_selecttf.constanta,dim,index 张量[[0,1,2,3], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [20, 21, 22, 23]]]
我还没有发现类似的api可以直接实现它,但我们可以使用tf.slice来实现它

def tf_索引\u选择输入、尺寸、索引: 输入张量:输入张量 小尺寸:尺寸 指标列表:所选指标列表 shape=输入\获取\ shape.as\列表 如果dim==-1: 尺寸=透镜形状-1 形状[dim]=1 tmp=[] 对于索引中的idx: 开始=[0]*lenshape 开始[dim]=idx tmp.appendtf.sliceinput、begin、shape res=tf.concattmp,轴=dim 返回res 下面是一个例子来说明等价性

导入tensorflow作为tf 进口火炬 将numpy作为np导入 a=np.arange2*3*4.重塑2,3,4 尺寸=1 指数=[0,2] 数组[[0,1,2,3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]] 皮托克 res=torch.tensora.index\u选择dim,torch.tensorindices 张量[[0,1,2,3], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [20, 21, 22, 23]]] 张量流 res=tf_index_selecttf.constanta,dim,index 张量[[0,1,2,3], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [20, 21, 22, 23]]]
非常感谢,我发现tf.gather也是我需要的,不是吗?@ShawnChen是的,但你需要先生成索引。非常感谢,我发现tf.gather也是我需要的,不是吗?@ShawnChen是的,但你需要先生成索引。