| 733 | ) const { return labels.size(); } |
| 734 | |
| 735 | std::pair<result_type, scalar_type> predict ( |
| 736 | const sample_type& x |
| 737 | ) const |
| 738 | { |
| 739 | // make sure requires clause is not broken |
| 740 | DLIB_ASSERT(weights.size() > 0 && |
| 741 | weights.nr() == (long)number_of_classes() && |
| 742 | weights.nr() == b.size(), |
| 743 | "\t pair<result_type,scalar_type> multiclass_linear_decision_function::predict(x)" |
| 744 | << "\n\t This object must be properly initialized before you can use it." |
| 745 | << "\n\t weights.size(): " << weights.size() |
| 746 | << "\n\t weights.nr(): " << weights.nr() |
| 747 | << "\n\t number_of_classes(): " << number_of_classes() |
| 748 | ); |
| 749 | |
| 750 | // Rather than doing something like, best_idx = index_of_max(weights*x-b) |
| 751 | // we do the following somewhat more complex thing because this supports |
| 752 | // both sparse and dense samples. |
| 753 | scalar_type best_val = dot(rowm(weights,0),x) - b(0); |
| 754 | unsigned long best_idx = 0; |
| 755 | |
| 756 | for (unsigned long i = 1; i < labels.size(); ++i) |
| 757 | { |
| 758 | scalar_type temp = dot(rowm(weights,i),x) - b(i); |
| 759 | if (temp > best_val) |
| 760 | { |
| 761 | best_val = temp; |
| 762 | best_idx = i; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | return std::make_pair(labels[best_idx], best_val); |
| 767 | } |
| 768 | |
| 769 | result_type operator() ( |
| 770 | const sample_type& x |