FT_DSP.gitlink.net/master/html/_sources/functionlib/dsplib/binarycrossentropy.rst.txt

120 lines
4.3 KiB
ReStructuredText
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Binarycrossentropy
=================
计算二元交叉熵损失。用于衡量二分类问题中预测值 (`input_x`) 和目标值 (`input_y`) 之间的差距。
.. math::
L_n = -w_n [y_n \cdot \log(x_n) + (1 - y_n) \cdot \log(1 - x_n)]
其中 :math:`x_n` 是预测值, :math:`y_n` 是目标值, :math:`w_n` 是可选的样本权重。最终输出由 `reduction` 参数决定:
- **None (0):** 不进行规约,输出每个元素的损失 :math:`L_n`
- **Mean (1):** 输出所有元素损失的平均值。
- **Sum (2):** 输出所有元素损失的总和。
输入:
- **input_x** - 预测值的张量数据地址通常是Sigmoid函数的输出。
- **input_y** - 目标值(标签)的张量数据地址。
- **weight** - (可选) 权重张量的数据地址,维度与 `input_x` 相同。
- **params** - 其他参数打包成数组。
- **core_mask** - 核掩码。
输出:
- **loss** - 写入最终损失值的数据地址。
支持平台:
``FT78NE``
``MT7004``
.. note::
- FT78NE 支持fp32, int8
- MT7004 支持fp16, fp32
**参数数组结构:**
.. code-block:: c
:linenos:
long long params[4];
params[0] = (long long)input_size; 输入张量的总元素数量
params[1] = (long long)reduction; 规约类型 (0: None, 1: Mean, 2: Sum)。
params[2] = (long long)tmp_loss; 用于存储逐元素损失的临时工作空间地址,大小必须为 `input_size`
params[3] = (long long)weight_defined; 权重是否有效的标志。若为非0`weight`参数必须提供。
**共享存储版本:**
.. c:function:: void fp_binary_cross_entropy_s(float* input_x, float* input_y, float* weight, float* loss, long long *params, int core_mask)
.. c:function:: void hp_binary_cross_entropy_s(half* input_x, half* input_y, half* weight, half* loss, long long *params, int core_mask)
.. c:function:: void i8_binary_cross_entropy_s(int8_t* input_x, int8_t* input_y, int8_t* weight, int8_t* loss, long long *params, int core_mask)
**C调用示例**
.. code-block:: c
:linenos:
:emphasize-lines: 16
//FT78NE示例
#include <stdio.h>
#include <binarycrossentropy.h>
int main(int argc, char* argv[]) {
float *input_x = (float *)0xA0000000; // input_x 在DDR空间
float *input_y = (float *)0xB0000000; // input_y
float *weight = (float *)0xC0000000; // weight
float *loss = (float *)0xD0000000; // loss output
float *tmp_loss = (float *)0xE0000000; // temp workspace
int input_size = 1024;
int reduction = 1; // Mean
int weight_defined = 1; // true
int core_mask = 0xff;
long long params[4];
params[0] = (long long)input_size;
params[1] = (long long)reduction;
params[2] = (long long)(uintptr_t)tmp_loss;
params[3] = (long long)weight_defined;
fp_binary_cross_entropy_s(input_x, input_y, weight, loss, params, core_mask);
return 0;
}
**私有存储版本:**
.. c:function:: void fp_binary_cross_entropy_p(float* input_x, float* input_y, float* weight, float* loss, long long *params)
.. c:function:: void hp_binary_cross_entropy_p(half* input_x, half* input_y, half* weight, half* loss, long long *params)
.. c:function:: void i8_binary_cross_entropy_p(int8_t* input_x, int8_t* input_y, int8_t* weight, int8_t* loss, long long *params)
**C调用示例**
.. code-block:: c
:linenos:
:emphasize-lines: 15
//FT78NE示例
#include <stdio.h>
#include <binarycrossentropy.h>
int main(int argc, char* argv[]) {
float *input_x = (float *)0x10000000; // input_x 在L2空间
float *input_y = (float *)0x11000000; // input_y
float *weight = (float *)0x12000000; // weight
float *loss = (float *)0x13000000; // loss output
float *tmp_loss = (float *)0x14000000; // temp workspace
int input_size = 1024;
int reduction = 1; // Mean
int weight_defined = 0; // false
long long params[4];
params[0] = (long long)input_size;
params[1] = (long long)reduction;
params[2] = (long long)(uintptr_t)tmp_loss;
params[3] = (long long)weight_defined;
fp_binary_cross_entropy_p(input_x, input_y, weight, loss, params);
return 0;
}