要測試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函數的正確性。如果有更多的測試用例,可以進一步擴展測試代碼。