toy_regression_test.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import numpy as np
  2. import unittest
  3. from caffe2.python import core, workspace, test_util
  4. class TestToyRegression(test_util.TestCase):
  5. def testToyRegression(self):
  6. """Tests a toy regression end to end.
  7. The test code carries a simple toy regression in the form
  8. y = 2.0 x1 + 1.5 x2 + 0.5
  9. by randomly generating gaussian inputs and calculating the ground
  10. truth outputs in the net as well. It uses a standard SGD to then
  11. train the parameters.
  12. """
  13. workspace.ResetWorkspace()
  14. init_net = core.Net("init")
  15. W = init_net.UniformFill([], "W", shape=[1, 2], min=-1., max=1.)
  16. B = init_net.ConstantFill([], "B", shape=[1], value=0.0)
  17. W_gt = init_net.GivenTensorFill(
  18. [], "W_gt", shape=[1, 2], values=[2.0, 1.5])
  19. B_gt = init_net.GivenTensorFill([], "B_gt", shape=[1], values=[0.5])
  20. LR = init_net.ConstantFill([], "LR", shape=[1], value=-0.1)
  21. ONE = init_net.ConstantFill([], "ONE", shape=[1], value=1.)
  22. ITER = init_net.ConstantFill([], "ITER", shape=[1], value=0,
  23. dtype=core.DataType.INT64)
  24. train_net = core.Net("train")
  25. X = train_net.GaussianFill([], "X", shape=[64, 2], mean=0.0, std=1.0)
  26. Y_gt = X.FC([W_gt, B_gt], "Y_gt")
  27. Y_pred = X.FC([W, B], "Y_pred")
  28. dist = train_net.SquaredL2Distance([Y_gt, Y_pred], "dist")
  29. loss = dist.AveragedLoss([], ["loss"])
  30. # Get gradients for all the computations above. Note that in fact we
  31. # don't need to get the gradient the Y_gt computation, but we'll just
  32. # leave it there. In many cases, I am expecting one to load X and Y
  33. # from the disk, so there is really no operator that will calculate the
  34. # Y_gt input.
  35. input_to_grad = train_net.AddGradientOperators([loss], skip=2)
  36. # updates
  37. train_net.Iter(ITER, ITER)
  38. train_net.LearningRate(ITER, "LR", base_lr=-0.1,
  39. policy="step", stepsize=20, gamma=0.9)
  40. train_net.WeightedSum([W, ONE, input_to_grad[str(W)], LR], W)
  41. train_net.WeightedSum([B, ONE, input_to_grad[str(B)], LR], B)
  42. for blob in [loss, W, B]:
  43. train_net.Print(blob, [])
  44. # the CPU part.
  45. plan = core.Plan("toy_regression")
  46. plan.AddStep(core.ExecutionStep("init", init_net))
  47. plan.AddStep(core.ExecutionStep("train", train_net, 200))
  48. workspace.RunPlan(plan)
  49. W_result = workspace.FetchBlob("W")
  50. B_result = workspace.FetchBlob("B")
  51. np.testing.assert_array_almost_equal(W_result, [[2.0, 1.5]], decimal=2)
  52. np.testing.assert_array_almost_equal(B_result, [0.5], decimal=2)
  53. workspace.ResetWorkspace()
  54. if __name__ == '__main__':
  55. unittest.main()