Skip to content

Commit

Permalink
[onert-micro] Bring up SparseCrossEntropyAccuracy Metric
Browse files Browse the repository at this point in the history
This commit adds SparseCrossEntropyAccuracy metric that can test
accuracy of SparseCrossEntropy loss function.

Signed-off-by: Jungwoo Lee <[email protected]>
  • Loading branch information
Jungwoo Lee committed Jul 26, 2024
1 parent 8f37c80 commit 0d06253
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions onert-micro/onert-micro/include/OMConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum OMMetrics
MAE_METRICS,
CROSS_ENTROPY_METRICS,
ACCURACY,
SPARSE_CROSS_ENTROPY_ACCURACY,
};

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef ONERT_MICRO_TRAIN_METRICS_SPARSE_CROSS_ENTROPY_ACCURACY_H
#define ONERT_MICRO_TRAIN_METRICS_SPARSE_CROSS_ENTROPY_ACCURACY_H

#include "OMStatus.h"

#include <cstdint>

namespace onert_micro
{
namespace train
{
namespace metrics
{

// Accuracy metric
struct SparseCrossEntropyAccuracy
{
// Calculate accuracy metric between calculated and target data
static float calculateValue(const uint32_t flat_size, const float *calculated_data,
const float *target_data);
};

} // namespace metrics
} // namespace train
} // namespace onert_micro

#endif // ONERT_MICRO_TRAIN_METRICS_SPARSE_CROSS_ENTROPY_ACCURACY_H
15 changes: 15 additions & 0 deletions onert-micro/onert-micro/src/core/train/OMTrainingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "train/metrics/CrossEntropy.h"
#include "train/metrics/Accuracy.h"
#include "train/metrics/MAE.h"
#include "train/metrics/SparseCrossEntropyAccuracy.h"

using namespace onert_micro::core::train;
using namespace onert_micro::core;
Expand Down Expand Up @@ -222,6 +223,12 @@ OMStatus OMTrainingHandler::evaluateMetric(OMMetrics metric, void *metric_val,
assert(calculated_data != nullptr);

// Get target data
/** NOTE:
* This offset will always return 0 if the MODEL OUTPUT is returning 1 value of prediction.
* (forward_output->size() == length of output vector.)
* one-hot: size == target_numbers
* Sparse cross : size == 1
*/
size_t offset = batch_num * sizeof(core::OMDataType(forward_output_tensor->type())) * flat_size;
uint8_t *target_data = _training_storage.getTargetData(i) + offset;

Expand Down Expand Up @@ -261,6 +268,14 @@ OMStatus OMTrainingHandler::evaluateMetric(OMMetrics metric, void *metric_val,
reinterpret_cast<float *>(target_data));
break;
}
case SPARSE_CROSS_ENTROPY_ACCURACY:
{
// Note: sum up new calculated value for current sample
*f_metric_val += metrics::SparseCrossEntropyAccuracy::calculateValue(
flat_size, reinterpret_cast<float *>(calculated_data),
reinterpret_cast<float *>(target_data));
break;
}
default:
{
assert(false && "Unsupported loss type");
Expand Down
1 change: 1 addition & 0 deletions onert-micro/onert-micro/src/train/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(SOURCES
metrics/MAE.cpp
metrics/MSE.cpp
metrics/Accuracy.cpp
metrics/SparseCrossEntropyAccuracy.cpp
train_optimizers/SGD.cpp
train_optimizers/Adam.cpp
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "train/metrics/SparseCrossEntropyAccuracy.h"

using namespace onert_micro;
using namespace onert_micro::train;
using namespace onert_micro::train::metrics;

/*
* return 1.0 if predicted class equals to target
* return 0.0 otherwise
*/
float SparseCrossEntropyAccuracy::calculateValue(const uint32_t flat_size,
const float *calculated_data,
const float *target_data)
{
// Find target class
uint32_t target_class = static_cast<uint32_t>(target_data[0]);

// Find predicted class
float pred_class = 0.f;
float pred_max_val = calculated_data[0];
for (uint32_t i = 0; i < flat_size; ++i)
{
if (pred_max_val < calculated_data[i])
{
pred_max_val = calculated_data[i];
pred_class = static_cast<float>(i);
}
}

return pred_class == target_class ? 1.0f : 0.0f;
}

0 comments on commit 0d06253

Please sign in to comment.