wkcn edited a comment on issue #16365: customOp Exception: unknown storage type: -1 URL: https://github.com/apache/incubator-mxnet/issues/16365#issuecomment-538747632 I could not reproduce this exception ```python import mxnet as mx import numpy as np class FocalLossOperator(mx.operator.CustomOp): def __init__(self, gamma, alpha): super(FocalLossOperator, self).__init__() self.gamma = gamma self.alpha = alpha def forward(self, is_train, req, in_data, out_data, aux): #print('forward') #print(in_data[0].shape) y = mx.nd.exp(in_data[0] - mx.nd.max_axis(in_data[0], axis=1).reshape((in_data[0].shape[0], 1, -1))) y /= mx.nd.sum(y, axis=1).reshape((in_data[0].shape[0],1, -1)) self.assign(out_data[0], req[0], y) def backward(self, req, out_grad, in_data, out_data, in_grad, aux): y_numpy = out_data[0].asnumpy().transpose((0,2,1)) label_numpy = in_data[1].asnumpy() y_numpy = y_numpy.reshape((-1,2)) label_numpy = label_numpy.reshape((-1)) #print(len(np.where(label_numpy == -1)[0])) indices = np.where(label_numpy == -1)[0] label_numpy[indices] = 0 self.pro_truth = mx.nd.array(y_numpy[np.arange(y_numpy.shape[0]), label_numpy.astype(np.int)]) # print(len(indices)) # i!=j pro_truth = (self.pro_truth + 1e-14).reshape((self.pro_truth.shape[0], 1)) grad = self.alpha * mx.nd.power(1 - pro_truth, self.gamma - 1) * \ (self.gamma * (-1 * pro_truth * mx.nd.array(y_numpy)) * mx.nd.log(pro_truth) + mx.nd.array(y_numpy) * (1 - pro_truth)) # i==j pro_truth = self.pro_truth + 1e-14 grad_numpy = grad.asnumpy() grad_numpy[np.arange(y_numpy.shape[0]), label_numpy.astype(np.int)] = ( self.alpha * mx.nd.power(1 - pro_truth, self.gamma) * ( self.gamma * pro_truth * mx.nd.log(pro_truth) + pro_truth - 1)).asnumpy() grad_numpy /= label_numpy.shape[0] grad_numpy[indices,:] = 0 #grad_numpy = grad_numpy.reshape((out_data[0].shape[0],-1,out_data[0].shape[1])).transpose((0,2,1)) grad = mx.nd.array(grad_numpy) grad = grad.reshape(out_data[0].shape[0],-1,out_data[0].shape[1]).transpose((0,2,1)) self.assign(in_grad[0], req[0], grad) @mx.operator.register('FocalLoss') class FocalLossProp(mx.operator.CustomOpProp): def __init__(self, gamma, alpha): super(FocalLossProp, self).__init__(need_top_grad=False) self.gamma = float(gamma) self.alpha = float(alpha) def list_arguments(self): return ['data', 'labels'] def list_outputs(self): return ['output'] def infer_shape(self, in_shape): data_shape = in_shape[0] labels_shape = in_shape[1] out_shape = data_shape return [data_shape, labels_shape], [out_shape], [] def create_operator(self, ctx, shapes, dtypes): return FocalLossOperator(self.gamma, self.alpha) class FocalLossGluon(mx.gluon.nn.HybridBlock): def hybrid_forward(self, F, x, label): return F.Custom(x, label, gamma=1, alpha=1, op_type='FocalLoss') if __name__ == '__main__': batch_size = 3 num_anchor = 4 x = mx.nd.zeros((batch_size, 2, num_anchor)) label = mx.nd.zeros((batch_size, num_anchor)) x.attach_grad() with mx.autograd.record(): y = mx.nd.Custom(x, label, gamma=1, alpha=1, op_type='FocalLoss') y.backward() print(y) print(x.grad) block = FocalLossGluon() block.hybridize() for _ in range(2): with mx.autograd.record(): y = block(x, label) y.backward() print(y) print(x.grad) ```
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
