importunittestfromop_test_utilimportOpTestMetafromgradient_checkerimportGradientChecker,create_opimportnumpyimportpaddle.v2.framework.coreascorefrompaddle.v2.framework.opimportOperatorclassTestScatterOp(unittest.TestCase):__metaclass__=OpTestMetadefsetUp(self):self.type="scatter"ref_np=numpy.ones((3,3)).astype("float32")index_np=numpy.array([1,2]).astype("int32")updates_np=numpy.random.random((2,3)).astype("float32")output_np=numpy.copy(ref_np)output_np[index_np]+=updates_npself.inputs={'Ref':ref_np,'Index':index_np,'Updates':updates_np}self.outputs={'Out':output_np}classTestScatterGradOp(GradientChecker):deftest_scatter_grad(self):op=create_op("scatter")# test data setupref_np=numpy.ones((3,10)).astype("float32")index_np=numpy.array([1,2]).astype("int32")updates_np=numpy.random.random((2,10)).astype("float32")output_np=numpy.copy(ref_np)output_np[index_np]+=updates_npinputs={'Ref':ref_np,'Index':index_np,'Updates':updates_np}# check gradientself.check_grad(op,inputs,set(["Updates","Ref"]),"Out")if__name__=="__main__":unittest.main()