espressif / esp-dl

Espressif deep-learning library for AIoT applications
MIT License
548 stars 118 forks source link

Matrix multiplication #44

Closed PureHing closed 3 years ago

PureHing commented 3 years ago

@yehangyang Hi,How to use mul2d to multiply two matrices(AxB)?

    int16_t element1[] = {5, 6, 7, 8};
    int16_t element2[] = {1, 2, 3, 4};
    Tensor<int16_t> input1;
    input1.set_element((int16_t *)element1).set_exponent(0).set_shape({2,2,1}).set_auto_free(false);
    Tensor<int16_t> input2;
    input2.set_element((int16_t *)element2).set_exponent(0).set_shape({2, 2, 1}).set_auto_free(false);
    Tensor<int16_t> output;
    dl::nn::mul2d(output,input1,input2);

Is this reasonable?

yehangyang commented 3 years ago

Hi @PureHing

In your code, it actually calls this void mul2d(Tensor &output, Tensor &input0, Tensor &input1, const Activation *const activation = NULL, const std::vector &assign_core = CONFIG_DEFAULT_ASSIGN_CORE);. In this way, we have make sure the output has been initialized with shape, exponent, etc. Take dl_layer_mul2d.hpp for reference.

Recommend Option-1: If you just only have this operation(won't mix with other padding-required operations, such as Conv2D)

#include <stdint.h>
#include "dl_variable.hpp"
#include "dl_constant.hpp"
#include "dl_nn_mul2d.hpp"

using namespace dl;
using namespace nn;
extern "C" void app_main(void)
{
    int16_t element1[] = {5, 6, 7, 8};
    int16_t element2[] = {1, 2, 3, 4};
    Tensor<int16_t> input1;
    input1.set_element((int16_t *)element1).set_exponent(0).set_shape({2, 2, 1}).set_auto_free(false);
    Tensor<int16_t> input2;
    input2.set_element((int16_t *)element2).set_exponent(0).set_shape({2, 2, 1}).set_auto_free(false);
    Tensor<int16_t> output = dl::nn::mul2d(0, input1, input2, (Activation<int16_t> *)NULL);
    output.print(0, 2, 0, 2, 0, 1, "output", 2);
}

you'll get:

output[0:2, 0:2, 0:1] | shape = (0 + 2 + 0, 0 + 2 + 0, 1(1))
c = 0
           0      1
    0      5     12
    1     21     32

Recommend Option-2: If this operation is mixed with other padding-required operations, I recommend you dl::layer::Mul2D.

PureHing commented 3 years ago

@yehangyang Thanks. BTW, How to achieve the same function as numpy.dot

yehangyang commented 3 years ago

@PureHing It's not implemented yet. We planed to support it later.