spm_softmax.m 778 B

123456789101112131415161718192021222324
  1. function [y] = spm_softmax(x,k)
  2. % softmax (e.g., neural transfer) function over columns
  3. % FORMAT [y] = spm_softmax(x,k)
  4. %
  5. % x - numeric array array
  6. % k - precision, sensitivity or inverse temperature (default k = 1)
  7. %
  8. % y = exp(k*x)/sum(exp(k*x))
  9. %
  10. % NB: If supplied with a matrix this rotine will return the softmax
  11. % function over colums - so that spm_softmax([x1,x2,..]) = [1,1,...]
  12. %__________________________________________________________________________
  13. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  14. % Karl Friston
  15. % $Id: spm_softmax.m 7306 2018-05-07 13:42:02Z karl $
  16. % apply
  17. %--------------------------------------------------------------------------
  18. if nargin > 1, x = k*x; end
  19. x = exp(bsxfun(@minus,x,max(x)));
  20. y = bsxfun(@rdivide,x,sum(x));