ONE - On-device Neural Engine
Loading...
Searching...
No Matches
CrossEntropy.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
18
19#include <cmath>
20
21using namespace onert_micro;
22using namespace onert_micro::train;
23using namespace onert_micro::train::metrics;
24
25/*
26 * Y - calculated value
27 * Y_t - target value
28 * E(Y, Y_t) = -SUM(Y_t_i * log(Y_i))
29 * SUM - sum among i = (0 ... N - 1), N - flat_size
30 */
31float CrossEntropy::calculateValue(const uint32_t flat_size, const float *calculated_data,
32 const float *target_data)
33{
34 float result_value = 0.f;
35
36 for (uint32_t i = 0; i < flat_size; ++i)
37 {
38 result_value += target_data[i] * std::log(calculated_data[i] + float(10.0e-32));
39 }
40
41 return -result_value;
42}
static float calculateValue(const uint32_t flat_size, const float *calculated_data, const float *target_data)