From 507c3a35e3bfbba2af102fbead0c2fb41e9db9b0 Mon Sep 17 00:00:00 2001 From: soumith Date: Thu, 5 Nov 2015 17:44:15 -0500 Subject: integrating changes from master --- test/test.lua | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'test') diff --git a/test/test.lua b/test/test.lua index 0a2fb01..6dbd0d8 100644 --- a/test/test.lua +++ b/test/test.lua @@ -730,6 +730,73 @@ function cudnntest.LogSoftMax_batch() precision_backward, 'error on state (backward) ') end +function cudnntest.SpatialLogSoftMax() + -- batch + local numLabels = math.random(5,10) + local h = math.random(5,10) + local w = math.random(5,10) + local bsz = math.random(3, 7) + local input = torch.zeros(bsz, numLabels, h, w):normal():cuda() + local target = torch.zeros(bsz, numLabels, h, w):normal():cuda() + + local cri = cudnn.SpatialLogSoftMax():cuda() + local gcri = nn.LogSoftMax():cuda() + + local op = cri:forward(input, target) + local gi = cri:backward(input, target) + + local gop = op:clone():zero() + local ggi = gi:clone():zero() + + for i=1,h do + for j=1,w do + local i1 = input[{{}, {}, {i}, {j}}]:contiguous():squeeze() + local t1 = target[{{}, {}, {i}, {j}}]:contiguous():squeeze() + local gop1 = gcri:forward(i1, t1) + local ggi1 = gcri:backward(i1, t1) + gop[{{}, {}, {i}, {j}}]:copy(gop1) + ggi[{{}, {}, {i}, {j}}]:copy(ggi1) + end + end + local err = (gi - ggi):abs():max() + mytester:assertlt(err, precision_backward, 'error in difference between central difference and :backward') + local err = (op - gop):abs():max() + mytester:assertlt(err, precision_backward, 'error in difference between central difference and :backward') +end + +function cudnntest.SpatialCrossEntropyCriterion() + -- batch + local numLabels = math.random(5,10) + local h = math.random(5,10) + local w = math.random(5,10) + local bsz = math.random(3, 7) + local input = torch.zeros(bsz, numLabels, h, w):normal():cuda() + local target = torch.Tensor(bsz, h, w):random(1, numLabels):cuda() + + local cri = cudnn.SpatialCrossEntropyCriterion():cuda() + + local gcri = nn.CrossEntropyCriterion():cuda() + + local op = cri:forward(input, target) + local gi = cri:backward(input, target) + + local ggi = gi:clone():zero() + + for i=1,h do + for j=1,w do + local i1 = input[{{}, {}, {i}, {j}}]:contiguous():squeeze() + local t1 = target[{{}, {i}, {j}}]:contiguous():squeeze() + local gop1 = gcri:forward(i1, t1) + local ggi1 = gcri:backward(i1, t1) + ggi[{{}, {}, {i}, {j}}]:copy(ggi1) + end + end + local err = (gi - ggi):abs():max() + mytester:assertlt(err, precision_backward, 'error in difference between central difference and :backward') + +end + + function cudnntest.functional_bias2D() local bs = math.random(1,32) local from = math.random(1,32) -- cgit v1.2.3