Files
ESPDocs/public/exemplos/zh-CN/esp-nn.c
T

45 lines
1.0 KiB
C
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.
#include <stdio.h>
#include "esp_nn.h"
#include "esp_nn_defs.h"
#define INPUT_SIZE 4
#define OUTPUT_SIZE 3
void print_array(const char *label, int16_t *array, int size) {
printf("%s: ", label);
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
void app_main(void)
{
// 输入向量(例如上一层的输出)
int16_t input[INPUT_SIZE] = {1, 2, 3, 4};
// 全连接层权重(3x4)
int8_t weights[OUTPUT_SIZE * INPUT_SIZE] = {
1, -1, 2, 0,
0, 1, -1, 1,
2, 0, 1, -2
};
// 每个神经元的偏置
int16_t bias[OUTPUT_SIZE] = {10, -5, 0};
// 输出
int16_t output[OUTPUT_SIZE];
// 矩阵乘法:output = weights * input + bias
esp_nn_mul_mat_16x8_16(weights, input, bias, output,
OUTPUT_SIZE, INPUT_SIZE, /*shift=*/0);
print_array("Antes da ReLU", output, OUTPUT_SIZE);
// 应用 ReLU 激活函数
esp_nn_relu_16(output, OUTPUT_SIZE);
print_array("Depois da ReLU", output, OUTPUT_SIZE);
}