-
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 SparseCrossEntropy Loss Function
This commit adds new loss function "SparseCrossEntropy" Since this function has different number compare to one-hot-encoded target, offset change condition was added. Signed-off-by: Jungwoo Lee <[email protected]>
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -52,6 +52,7 @@ enum OMLoss | |
CROSS_ENTROPY, | ||
MSE, | ||
MAE, | ||
SPARSE_CROSS_ENTROPY, | ||
}; | ||
|
||
/* | ||
|
43 changes: 43 additions & 0 deletions
43
onert-micro/onert-micro/include/train/losses_functions/SparseCrossEntropy.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_LOSSES_FUNCTIONS_SPARSE_CROSS_ENTROPY_H | ||
#define ONERT_MICRO_TRAIN_LOSSES_FUNCTIONS_SPARSE_CROSS_ENTROPY_H | ||
|
||
#include "OMStatus.h" | ||
|
||
#include <cstdint> | ||
|
||
namespace onert_micro | ||
{ | ||
namespace train | ||
{ | ||
namespace losses_functions | ||
{ | ||
|
||
// Cross Entropy | ||
struct SparseCrossEntropy | ||
{ | ||
// Calculate sparse cross entropy error backpropagation between calculated and target data | ||
static void calculateErrorBackpropagation(const uint32_t flat_size, const float *calculated_data, | ||
const float *target_data, float *output_grad); | ||
}; | ||
|
||
} // namespace losses_functions | ||
} // namespace train | ||
} // namespace onert_micro | ||
|
||
#endif // ONERT_MICRO_TRAIN_LOSSES_FUNCTIONS_SPARSE_CROSS_ENTROPY_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
44 changes: 44 additions & 0 deletions
44
onert-micro/onert-micro/src/train/losses_functions/SparseCrossEntropy.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,44 @@ | ||
/* | ||
* 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/losses_functions/SparseCrossEntropy.h" | ||
#include <cmath> | ||
|
||
using namespace onert_micro; | ||
using namespace onert_micro::train; | ||
using namespace onert_micro::train::losses_functions; | ||
|
||
/* | ||
* dE/dZi = (dE/dy) * (dy / dZi) | ||
* where Z - vector of logits, | ||
* y - probaility of target. | ||
* | ||
* Since dE/dy = -(1/y), | ||
* (true label) if i == y : dE/dZi = py - 1 = y - 1 | ||
* (wrong label) else : dE/dZi = pj | ||
* | ||
*/ | ||
void SparseCrossEntropy::calculateErrorBackpropagation(const uint32_t flat_size, | ||
const float *calculated_data, | ||
const float *target_data, float *output_grad) | ||
{ | ||
uint32_t label = static_cast<uint32_t>(target_data[0]); | ||
|
||
for (uint32_t i = 0; i < flat_size; ++i) | ||
{ | ||
output_grad[i] = (calculated_data[i] + float(10.0e-32)) - (i == label); | ||
} | ||
} |