阿对对队的一次提交尝试(进行了小部分的代码评注) #8
|
|
@ -26,13 +26,31 @@ namespace lite {
|
|||
constexpr int METRICS_CLASSIFICATION = 0;
|
||||
constexpr int METRICS_MULTILABEL = 1;
|
||||
|
||||
/// \brief AccuracyMetrics is the training Accuracy Class of MindSpore Lite.
|
||||
/// By inheriting the Metric base class, a custom training metric class can be created. The clear, update, and eval methods need to be overridden.
|
||||
/// By calling the model.predict() interface, the network output can be obtained, and the results can be calculated according to the custom evaluation criteria.
|
||||
class AccuracyMetrics : public Metrics {
|
||||
public:
|
||||
/// \brief Constructor of AccuracyMetrics
|
||||
/// \param accuracy_metrics is the supported classification metrics.
|
||||
/// \param input_indexes is the input mapping vectors size.
|
||||
/// \param output_indexes is the output mapping vectors size.
|
||||
explicit AccuracyMetrics(int accuracy_metrics = METRICS_CLASSIFICATION, const std::vector<int> &input_indexes = {1},
|
||||
const std::vector<int> &output_indexes = {0});
|
||||
|
||||
/// \brief Destructor of AccuracyMetrics
|
||||
virtual ~AccuracyMetrics() = default;
|
||||
|
||||
/// \brief Method to reset the training accuracy to zero.
|
||||
void Clear() override { total_accuracy_ = total_steps_ = 0.0; }
|
||||
|
||||
/// \brief Method for model validation.
|
||||
/// \return the model validation accuracy, which is a floating-point number.
|
||||
float Eval() override;
|
||||
|
||||
/// \brief Method for updating the input and output data of the model.
|
||||
/// \param inputs is the vector of model input MSTensor.
|
||||
/// \param outputs is the vector of model output MSTensor.
|
||||
void Update(std::vector<tensor::MSTensor *> inputs, std::vector<tensor::MSTensor *> outputs) override;
|
||||
|
||||
protected:
|
||||
|
|
|
|||
|
|
@ -22,15 +22,21 @@
|
|||
|
||||
namespace mindspore {
|
||||
namespace lite {
|
||||
|
||||
/// \brief Constructor of AccuracyMetrics
|
||||
AccuracyMetrics::AccuracyMetrics(int accuracy_metrics, const std::vector<int> &input_indexes,
|
||||
const std::vector<int> &output_indexes)
|
||||
: Metrics() {
|
||||
|
||||
// When the size of the input and output mapping vectors are equal, assign values to the protected data members in AccuracyMetrics, which are the input mapping vector and output mapping vector.
|
||||
if (input_indexes.size() == output_indexes.size()) {
|
||||
input_indexes_ = input_indexes;
|
||||
output_indexes_ = output_indexes;
|
||||
} else {
|
||||
MS_LOG(WARNING) << "input to output mapping vectors sizes do not match";
|
||||
}
|
||||
|
||||
// Throw an exception when the classification metric in the function parameters is not equal to METRICS_CLASSIFICATION.
|
||||
if (accuracy_metrics != METRICS_CLASSIFICATION) {
|
||||
MS_LOG(WARNING) << "Only classification metrics is supported";
|
||||
} else {
|
||||
|
|
@ -38,6 +44,7 @@ AccuracyMetrics::AccuracyMetrics(int accuracy_metrics, const std::vector<int> &i
|
|||
}
|
||||
}
|
||||
|
||||
/// \brief Method for updating the input and output data of the model.
|
||||
void AccuracyMetrics::Update(std::vector<tensor::MSTensor *> inputs, std::vector<tensor::MSTensor *> outputs) {
|
||||
for (unsigned int i = 0; i < input_indexes_.size(); i++) {
|
||||
if ((inputs.size() <= static_cast<unsigned int>(input_indexes_[i])) ||
|
||||
|
|
@ -57,13 +64,14 @@ void AccuracyMetrics::Update(std::vector<tensor::MSTensor *> inputs, std::vector
|
|||
}
|
||||
}
|
||||
|
||||
/// \brief Method for model validation.
|
||||
float AccuracyMetrics::Eval() {
|
||||
if (total_steps_ == 0.0) {
|
||||
if (total_steps_ == 0.0) { // If the sample size is 0, it will trigger a runtime error.
|
||||
MS_LOG(WARNING) << "Accuary can not be calculated, because the number of samples is 0.";
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return (total_accuracy_ / total_steps_);
|
||||
return (total_accuracy_ / total_steps_); // The default method returns (total_accuracy_ / total_steps_) as the value for model validation.
|
||||
}
|
||||
} // namespace lite
|
||||
} // namespace mindspore
|
||||
|
|
|
|||
Loading…
Reference in New Issue