要测试Softmax函数的正确性,可以使用以下步骤:
#include <iostream>
#include <vector>
#include <cmath>
std::vector<double> softmax(std::vector<double> input) {
std::vector<double> output;
double sum = 0.0;
for (int i = 0; i < input.size(); i++) {
sum += exp(input[i]);
}
for (int i = 0; i < input.size(); i++) {
output.push_back(exp(input[i]) / sum);
}
return output;
}
int main() {
std::vector<double> input1 = {1.0, 2.0, 3.0};
std::vector<double> result1 = softmax(input1);
for (int i = 0; i < result1.size(); i++) {
std::cout << result1[i] << " ";
}
std::cout << std::endl;
std::vector<double> input2 = {3.0, 1.0, 0.5};
std::vector<double> result2 = softmax(input2);
for (int i = 0; i < result2.size(); i++) {
std::cout << result2[i] << " ";
}
std::cout << std::endl;
return 0;
}
通过这种方式,可以测试Softmax函数的正确性。如果有更多的测试用例,可以进一步扩展测试代码。