# paddlepaddle code index = fluid.layers.nonzero(active) aa = fluid.layers.gather(cls, index)
测试全代码
import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable import numpy as npdata = np.ones([300,4]).astype('float32') index = np.zeros([300]).astype('int') index[0]=1 index[2]=1 index[10]=1with fluid.dygraph.guard():data = to_variable(data)index = to_variable(index)index = fluid.layers.nonzero(index)data = fluid.layers.gather(data, index)# data = data[index, :]print(data.shape)
示例2
# pytorch code active = labels_weight >0 y = bbox_x[active]# paddlepaddle code active = labels_weight >0 index = fluid.layers.nonzero(active) y = fluid.layers.concat([fluid.layers.reshape(fluid.layers.gather(bbox_x[i,:], index[i,:]),[1,-1])for i inrange(index.shape[0])], axis=0)
TypeError: The type of &#39;shape&#39;in reshape must be list[int] or tuple(int)in Dygraph mode, but received <class &#39;list&#39;>, which contains Variable.