/theano/sandbox/gpuarray/tests/test_nnet.py

https://github.com/yosinski/Theano · Python · 159 lines · 104 code · 29 blank · 26 comment · 9 complexity · 2df7371cddb8cb299e2cf5efefdc174f MD5 · raw file

  1. from nose.plugins.skip import SkipTest
  2. import numpy
  3. import theano
  4. from theano.gof.python25 import any
  5. import theano.tensor as T
  6. import theano.tests.unittest_tools as utt
  7. from theano.sandbox import gpuarray
  8. # We let that import do the init of the back-end if needed.
  9. from theano.sandbox.gpuarray.tests.test_basic_ops import (mode_with_gpu,
  10. mode_without_gpu)
  11. from theano.sandbox.gpuarray.nnet import (
  12. GpuCrossentropySoftmaxArgmax1HotWithBias,
  13. GpuCrossentropySoftmax1HotWithBiasDx)
  14. def test_GpuCrossentropySoftmaxArgmax1HotWithBias():
  15. """
  16. This is basic test for GpuCrossentropySoftmaxArgmax1HotWithBias
  17. We check that we loop when their is too much threads
  18. """
  19. n_in = 1000
  20. batch_size = 4097
  21. n_out = 1250
  22. if not isinstance(mode_with_gpu, theano.compile.DebugMode):
  23. n_in = 4098
  24. n_out = 4099
  25. x = T.fmatrix('x')
  26. y = T.lvector('y')
  27. b = T.fvector('b')
  28. #W = T.fmatrix('W')
  29. #we precompute the dot with big shape before to allow the test of
  30. #GpuCrossentropySoftmax1HotWithBiasDx to don't fail with the error
  31. #(the launch timed out and was terminated) on GPU card not
  32. #powerful enough. We need the big shape to check for corner
  33. #case.
  34. dot_result = T.fmatrix('dot_result')
  35. # Seed numpy.random with config.unittests.rseed
  36. utt.seed_rng()
  37. xx = numpy.asarray(numpy.random.rand(batch_size, n_in),
  38. dtype=numpy.float32)
  39. #?????yy = numpy.ones((batch_size,),dtype='float32')
  40. yy = numpy.ones((batch_size,), dtype='int32')
  41. b_values = numpy.zeros((n_out,), dtype='float32')
  42. W_values = numpy.asarray(numpy.random.rand(n_in, n_out), dtype='float32')
  43. dot_value = numpy.asarray(numpy.dot(xx, W_values), dtype='float32')
  44. del W_values
  45. p_y_given_x = T.nnet.softmax(dot_result + b)
  46. y_pred = T.argmax(p_y_given_x, axis=-1)
  47. loss = -T.mean(T.log(p_y_given_x)[T.arange(y.shape[0]), y])
  48. dW = T.grad(loss, dot_result)
  49. classify = theano.function(inputs=[y, b, dot_result],
  50. outputs=[loss, y_pred, dW],
  51. mode=mode_without_gpu)
  52. classify_gpu = theano.function(inputs=[y, b, dot_result],
  53. outputs=[loss, y_pred, dW],
  54. mode=mode_with_gpu)
  55. #theano.printing.debugprint(classify)
  56. #theano.printing.debugprint(classify_gpu)
  57. assert any([isinstance(node.op,
  58. T.nnet.CrossentropySoftmaxArgmax1HotWithBias)
  59. for node in classify.maker.fgraph.toposort()])
  60. assert any([isinstance(node.op,
  61. GpuCrossentropySoftmaxArgmax1HotWithBias)
  62. for node in classify_gpu.maker.fgraph.toposort()])
  63. out = classify(yy, b_values, dot_value)
  64. gout = classify_gpu(yy, b_values, dot_value)
  65. assert len(out) == len(gout) == 3
  66. assert numpy.allclose(out[0], gout[0])
  67. assert numpy.allclose(out[2], gout[2], atol=3e-6), numpy.absolute(
  68. gout[2] - out[2]).max()
  69. assert numpy.allclose(out[1], gout[1]), [(id, out[1][id], gout[1][id], val)
  70. for id, val in enumerate(out[1] -
  71. gout[1])
  72. if val != 0]
  73. def test_GpuCrossentropySoftmax1HotWithBiasDx():
  74. """
  75. This is basic test for GpuCrossentropySoftmax1HotWithBiasDx
  76. We check that we loop when their is too much threads
  77. """
  78. n_in = 1000
  79. batch_size = 4097
  80. n_out = 1250
  81. if not isinstance(mode_with_gpu, theano.compile.DebugMode):
  82. n_in = 4098
  83. n_out = 4099
  84. # Seed numpy.random with config.unittests.rseed
  85. utt.seed_rng()
  86. softmax_output_value = numpy.random.rand(batch_size,
  87. n_out).astype('float32')
  88. dnll_value = numpy.asarray(numpy.random.rand(batch_size), dtype='float32')
  89. y_idx_value = numpy.random.randint(low=0, high=5, size=batch_size)
  90. softmax_output = T.fmatrix()
  91. softmax_output /= softmax_output.sum(axis=1).reshape(
  92. softmax_output.shape[1], 1)
  93. op = theano.tensor.nnet.crossentropy_softmax_1hot_with_bias_dx(
  94. dnll_value,
  95. softmax_output,
  96. y_idx_value)
  97. cpu_f = theano.function([softmax_output], op, mode=mode_without_gpu)
  98. gpu_f = theano.function([softmax_output], op, mode=mode_with_gpu)
  99. #theano.printing.debugprint(cpu_f)
  100. #theano.printing.debugprint(gpu_f)
  101. assert any([isinstance(node.op, T.nnet.CrossentropySoftmax1HotWithBiasDx)
  102. for node in cpu_f.maker.fgraph.toposort()])
  103. assert any([isinstance(node.op,
  104. GpuCrossentropySoftmax1HotWithBiasDx)
  105. for node in gpu_f.maker.fgraph.toposort()])
  106. cpu_out = cpu_f(softmax_output_value)
  107. gpu_out = gpu_f(softmax_output_value)
  108. rtol = 1e-5
  109. atol = 1e-6
  110. if not numpy.allclose(cpu_out, gpu_out, rtol=rtol, atol=atol):
  111. abs_err, rel_err = T.numeric_grad.abs_rel_err(cpu_out, gpu_out)
  112. scaled_err = numpy.minimum(abs_err / atol, rel_err / rtol)
  113. max_i = scaled_err.argmax()
  114. print 'max err index:', max_i, max_i / batch_size,
  115. print max_i % batch_size, max_i / n_out, max_i & n_out
  116. print 'At that index:'
  117. print 'err:', scaled_err.flatten()[max_i]
  118. print 'absolute error:', abs_err.flatten()[max_i]
  119. print 'relative error:', rel_err.flatten()[max_i]
  120. print 'cpu_out:', cpu_out.flatten()[max_i]
  121. print 'gpu_out:', gpu_out.flatten()[max_i]
  122. print 'softmax_output_value:', softmax_output_value.flatten()[max_i]
  123. print 'dnll_value:', dnll_value[max_i / n_out]
  124. print 'y_idx_value:', y_idx_value[max_i / n_out]
  125. assert False, "numpy.allclose(cpu_out, gpu_out, rtol=%s, atol=%s)" % (
  126. rtol, atol)