Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtschump committed Sep 11, 2024
2 parents 42f01d0 + db85c0f commit fd3daa8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
52 changes: 50 additions & 2 deletions CImg.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

// Set version number of the library.
#ifndef cimg_version
#define cimg_version 342
#define cimg_version 343

/*-----------------------------------------------------------
#
Expand Down Expand Up @@ -22379,6 +22379,19 @@ namespace cimg_library {
_cimg_mp_const_scalar(is_scalar(arg1)?0:size(arg1));
}

if (!std::strncmp(ss,"softmax(",8)) { // Softmax
_cimg_mp_op("Function 'softmax()'");
s1 = ss8; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
arg1 = compile(ss8,s1,depth1,0,block_flags);
arg2 = s1<se1?compile(++s1,se1,depth1,0,block_flags):1;
_cimg_mp_check_type(arg2,2,1,0);
p1 = size(arg1);
if (p1>0) pos = is_comp_vector(arg1)?arg1:((return_comp = true), vector(p1));
else _cimg_mp_return(1);
CImg<ulongT>::vector((ulongT)mp_vector_softmax,pos,arg1,p1,arg2).move_to(code);
_cimg_mp_return(pos);
}

if (!std::strncmp(ss,"solve(",6)) { // Solve square linear system
_cimg_mp_op("Function 'solve()'");
s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
Expand Down Expand Up @@ -22798,7 +22811,7 @@ namespace cimg_library {
s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
arg1 = compile(s0,s1,depth1,0,block_flags);
arg2 = s1<se1?compile(++s1,se1,depth1,0,block_flags):2;
_cimg_mp_check_type(arg2,0,1,0);
_cimg_mp_check_type(arg2,2,1,0);
p1 = size(arg1);
if (p1>0) pos = is_comp_vector(arg1)?arg1:((return_comp = true), vector(p1));
else {
Expand Down Expand Up @@ -29241,6 +29254,19 @@ namespace cimg_library {
return _mp_arg(1);
}

static double mp_vector_softmax(_cimg_math_parser& mp) {
const unsigned int siz = (unsigned int)mp.opcode[3];
const double temperature = _mp_arg(4);
if (siz>0) { // Vector-valued argument
double *const ptrd = &_mp_arg(1) + 1;
const double *const ptrs = &_mp_arg(2) + 1;
CImg<doubleT>(ptrd,siz,1,1,1,true) = CImg<doubleT>(ptrs,siz,1,1,1,true).get_softmax(temperature);
return cimg::type<double>::nan();
}
// Scalar-valued argument.
return 1;
}

static double mp_vector_unitnorm(_cimg_math_parser& mp) {
const unsigned int siz = (unsigned int)mp.opcode[3];
const double p = _mp_arg(4);
Expand Down Expand Up @@ -30211,6 +30237,28 @@ namespace cimg_library {
return (+*this).ror(img);
}

//! Softmax operator.
CImg<T>& softmax(const float temperature=1) {
return get_softmax(temperature).move_to(*this);
}

//! Softmax operator \newinstance.
CImg<Tfloat> get_softmax(const float temperature=1) const {
if (is_empty()) return CImg<Tfloat>();
CImg<Tfloat> res(_width,_height,_depth,_spectrum);
const T val_max = max();
Tfloat sum = 0;
cimg_pragma_openmp(parallel reduction(+:sum) cimg_openmp_if_size(size(),4096)) {
cimg_pragma_openmp(for)
cimg_rofoff(*this,off) {
const Tfloat val = std::exp(((Tfloat)_data[off] - val_max)/temperature);
res[off] = val;
sum+=val;
}
}
return res/=sum;
}

//! Pointwise min operator between instance image and a value.
/**
\param val Value used as the reference argument of the min operator.
Expand Down
2 changes: 1 addition & 1 deletion html/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<div class="header">
<a href="index.html"><img alt="Logo" src="img/logo_header.jpg" class="center_image" style="margin-top:1em;"/></a>
<h2 style="padding-bottom: 1em">
Latest stable version: <b><a href="http://cimg.eu/files/CImg_.zip">3.4.2</a></b> (2024/09/04)
Latest stable version: <b><a href="http://cimg.eu/files/CImg_.zip">3.4.2</a></b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Current pre-release: <b><a href="http://cimg.eu/files/CImg_latest.zip">3.4.3</a></b> (2024/09/09)
</h2>

<hr/>
Expand Down
2 changes: 1 addition & 1 deletion html/header_doxygen.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<div class="header">
<a href="../index.html"><img alt="Logo" src="../img/logo_header.jpg" class="center_image" style="margin-top:1em;"/></a>
<h2 style="padding-bottom: 1em">
Latest stable version: <b><a href="http://cimg.eu/files/CImg_.zip">3.4.2</a></b> (2024/09/04)
Latest stable version: <b><a href="http://cimg.eu/files/CImg_.zip">3.4.2</a></b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Current pre-release: <b><a href="http://cimg.eu/files/CImg_latest.zip">3.4.3</a></b> (2024/09/09)
</h2>

<hr/>
Expand Down

0 comments on commit fd3daa8

Please sign in to comment.