|
|
|
@ -20,11 +20,10 @@ class TestSGDOp(OpTest):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestSparseSGDOp(unittest.TestCase):
|
|
|
|
|
def test_sparse_sgd(self):
|
|
|
|
|
def check_with_place(self, place):
|
|
|
|
|
scope = core.Scope()
|
|
|
|
|
|
|
|
|
|
# create and initialize Grad Variable
|
|
|
|
|
place = core.CPUPlace()
|
|
|
|
|
height = 10
|
|
|
|
|
rows = [0, 4, 7]
|
|
|
|
|
row_numel = 12
|
|
|
|
@ -35,6 +34,7 @@ class TestSparseSGDOp(unittest.TestCase):
|
|
|
|
|
np_array = np.ones((len(rows), row_numel)).astype("float32")
|
|
|
|
|
np_array[0, 0] = 2.0
|
|
|
|
|
np_array[2, 8] = 4.0
|
|
|
|
|
|
|
|
|
|
grad_tensor = grad_selected_rows.get_tensor()
|
|
|
|
|
grad_tensor.set(np_array, place)
|
|
|
|
|
|
|
|
|
@ -76,6 +76,13 @@ class TestSparseSGDOp(unittest.TestCase):
|
|
|
|
|
# rows[2] = 7, 5.0 - 2.0 * 4.0
|
|
|
|
|
self.assertAlmostEqual(-3.0, result_array[rows[2], 8])
|
|
|
|
|
|
|
|
|
|
def test_sparse_sgd(self):
|
|
|
|
|
places = [core.CPUPlace()]
|
|
|
|
|
if core.is_compile_gpu():
|
|
|
|
|
places.append(core.GPUPlace(0))
|
|
|
|
|
for place in places:
|
|
|
|
|
self.check_with_place(place)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|
|
|
|
|