Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/nn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSoumith Chintala <soumith@gmail.com>2014-05-12 19:18:49 +0400
committerSoumith Chintala <soumith@gmail.com>2014-05-12 19:18:49 +0400
commit2353158c6db7b61f711ad3d93de0384390cc9040 (patch)
tree324bb4444d9fa1f0b6e9b771e6dd68a2e7bef9ee /test
parent3f10105da13bd9c081f177ad5b41779493ca564f (diff)
adding test for MSECriterion, fixed test for WeightedMSECriterion
Diffstat (limited to 'test')
-rw-r--r--test/test.lua53
1 files changed, 39 insertions, 14 deletions
diff --git a/test/test.lua b/test/test.lua
index 29f76da..0e85578 100644
--- a/test/test.lua
+++ b/test/test.lua
@@ -403,20 +403,45 @@ function nntest.WeightedEuclidean()
mytester:asserteq(berr, 0, torch.typename(module) .. ' - i/o backward err ')
end
---function nntest.WeightedMSECriterion()
--- local from = math.random(100,200)
--- local input = torch.Tensor(from):zero()
--- local target = torch.randn(from)
--- local weight = torch.randn(from)
--- local cri = nn.WeightedMSECriterion(weight)
--- local module = nn.CriterionModule(cri,target)
--- local err = jac.testJacobian(module, input)
--- mytester:assertlt(err, precision, 'error on state ')
-
--- local ferr, berr = jac.testIO(module, input)
--- mytester:asserteq(0, ferr, torch.typename(module) .. ' - i/o forward err ')
--- mytester:asserteq(0, berr, torch.typename(module) .. ' - i/o backward err ')
---end
+local function criterionJacobianTest(cri, input, target)
+ local eps = 1e-6
+ local fx = cri:forward(input, target)
+ local dfdx = cri:backward(input, target)
+ -- for each input perturbation, do central difference
+ local centraldiff_dfdx = torch.Tensor(100)
+ for i=1,100 do
+ -- f(xi + h)
+ input[i] = input[i] + eps
+ local fx1 = cri:forward(input, target)
+ -- f(xi - h)
+ input[i] = input[i] - 2*eps
+ local fx2 = cri:forward(input, target)
+ -- f'(xi) = (f(xi + h) - f(xi - h)) / 2h
+ local cdfx = (fx1 - fx2) / (2*eps)
+ -- store f' in appropriate place
+ centraldiff_dfdx[i] = cdfx
+ -- reset input[i]
+ input[i] = input[i] + eps
+ end
+
+ -- compare centraldiff_dfdx with :backward()
+ local err = (centraldiff_dfdx - dfdx):abs():max()
+ mytester:assertlt(err, precision, 'error in difference between central difference and :backward')
+end
+
+function nntest.MSECriterion()
+ local input = torch.rand(100)
+ local target = input:clone():add(torch.rand(100))
+ local cri = nn.MSECriterion()
+ criterionJacobianTest(cri, input, target)
+end
+
+function nntest.WeightedMSECriterion()
+ local input = torch.rand(100)
+ local target = input:clone():add(torch.rand(100))
+ local cri = nn.WeightedMSECriterion(torch.rand(100))
+ criterionJacobianTest(cri, input, target)
+end
function nntest.LogSigmoid()
local ini = math.random(10,20)