| 461 | typename EXP |
| 462 | > |
| 463 | const matrix_op<op_softmax<EXP> > soft_max ( |
| 464 | const matrix_exp<EXP>& m |
| 465 | ) |
| 466 | { |
| 467 | // you can only compute softmax on matrices that contain floats, doubles or long doubles. |
| 468 | COMPILE_TIME_ASSERT(( |
| 469 | is_same_type<typename EXP::type,float>::value == true || |
| 470 | is_same_type<typename EXP::type,double>::value == true || |
| 471 | is_same_type<typename EXP::type,long double>::value == true |
| 472 | )); |
| 473 | typedef op_softmax<EXP> op; |
| 474 | typename EXP::type max_val = max(m); |
| 475 | typename EXP::type temp = static_cast<typename EXP::type>(1) / sum(exp(m - max_val)); |
| 476 | return matrix_op<op>(op(m.ref(), temp, max_val)); |
| 477 | } |
| 478 | |
| 479 | } |
| 480 | |