-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[onert-micro] Bring up SparseCrossEntropyAccuracy Metric
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
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ enum OMMetrics | |
MAE_METRICS, | ||
CROSS_ENTROPY_METRICS, | ||
ACCURACY, | ||
SPARSE_CROSS_ENTROPY_ACCURACY, | ||
}; | ||
|
||
/* | ||
|
43 changes: 43 additions & 0 deletions
43
onert-micro/onert-micro/include/train/metrics/SparseCrossEntropyAccuracy.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
onert-micro/onert-micro/src/train/metrics/SparseCrossEntropyAccuracy.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |