source: src/ai/dense.c @ b5b0ddd

feature/cnnfeature/crepe
Last change on this file since b5b0ddd was 00d4f53, checked in by Paul Brossier <piem@piem.org>, 3 years ago

[dense] add first plain version

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2  Copyright (C) 2018 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21
22#include "aubio_priv.h"
23#include "fmat.h"
24#include "tensor.h"
25#include "dense.h"
26
27struct _aubio_dense_t {
28  uint_t n_units;
29  fmat_t *weights;
30  fvec_t *bias;
31};
32
33aubio_dense_t *new_aubio_dense(uint_t n_units) {
34  aubio_dense_t *c = AUBIO_NEW(aubio_dense_t);
35
36  AUBIO_GOTO_FAILURE((sint_t)n_units >= 1);
37
38  c->n_units = n_units;
39
40  return c;
41failure:
42  del_aubio_dense(c);
43  return NULL;
44}
45
46void del_aubio_dense(aubio_dense_t *c) {
47  AUBIO_ASSERT(c);
48  if (c->weights)
49    del_fmat(c->weights);
50  if (c->bias)
51    del_fvec(c->bias);
52  AUBIO_FREE(c);
53}
54
55void aubio_dense_debug(aubio_dense_t *c, aubio_tensor_t *input_tensor)
56{
57  char_t input_string[15];
58  snprintf(input_string, 15, "(%d)", input_tensor->shape[0]);
59  AUBIO_DBG("dense:     %15s ¤ (%d, %d) ->"
60      " (%d) (%d params)\n",
61      input_string,
62      c->n_units,
63      c->n_units,
64      input_tensor->shape[0] * c->n_units);
65}
66
67uint_t aubio_dense_get_output_shape(aubio_dense_t *c,
68    aubio_tensor_t *input, uint_t *shape)
69{
70  AUBIO_ASSERT (c && input && shape);
71  AUBIO_ASSERT (input->ndim == 1);
72  shape[0] = c->n_units;
73
74  if (c->weights) del_fmat(c->weights);
75  c->weights = new_fmat(input->shape[0], c->n_units);
76  if (!c->weights) return AUBIO_FAIL;
77
78  if (c->bias) del_fvec(c->bias);
79  c->bias = new_fvec(c->n_units);
80  if (!c->bias) return AUBIO_FAIL;
81
82  aubio_dense_debug(c, input);
83
84  return AUBIO_OK;
85}
86
87fmat_t *aubio_dense_get_weights(aubio_dense_t *c) {
88  return c->weights;
89}
90
91fvec_t *aubio_dense_get_bias(aubio_dense_t *c) {
92  return c->bias;
93}
94
95void aubio_dense_do(aubio_dense_t *c, aubio_tensor_t *input_tensor,
96    aubio_tensor_t *activations) {
97  AUBIO_ASSERT(c && input_tensor && activations);
98  AUBIO_ASSERT(input_tensor->ndim == 1);
99  AUBIO_ASSERT(activations->ndim == 1);
100  AUBIO_ASSERT(input_tensor->shape[0] == c->weights->height);
101  AUBIO_ASSERT(activations->shape[0] == c->weights->length);
102
103  fvec_t input_vec;
104  aubio_tensor_as_fvec(input_tensor, &input_vec);
105  fvec_t output_vec;
106  aubio_tensor_as_fvec(activations, &output_vec);
107
108  // compute x.W
109  fvec_matmul(&input_vec, c->weights, &output_vec);
110  // add bias
111  fvec_vecadd(&output_vec, c->bias);
112
113  // compute sigmoid
114  uint_t i;
115  for (i = 0; i < output_vec.length; i++) {
116    output_vec.data[i] = 1. / (1. + EXP( - output_vec.data[i] ));
117  }
118}
Note: See TracBrowser for help on using the repository browser.