Changeset 05808d5


Ignore:
Timestamp:
Dec 29, 2021, 5:51:59 PM (3 weeks ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/crepe
Children:
9b05ea9
Parents:
49688ef
git-author:
Paul Brossier <piem@piem.org> (01/17/19 23:50:44)
git-committer:
Paul Brossier <piem@piem.org> (12/29/21 17:51:59)
Message:

[batchnorm] generalise to any number of dimensions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ai/batchnorm.c

    r49688ef r05808d5  
    7979    aubio_tensor_t *input, uint_t *shape)
    8080{
     81  uint_t i;
     82
    8183  AUBIO_ASSERT(c && input && shape);
     84  AUBIO_ASSERT(c->n_outputs == input->shape[input->ndim - 1]);
    8285
    83   shape[0] = input->shape[0];
    84   shape[1] = input->shape[1];
    85   shape[2] = input->shape[2];
     86  for (i = 0; i < input->ndim; i++) {
     87    shape[i] = input->shape[i];
     88  }
    8689
    8790  aubio_batchnorm_debug(c, input);
     
    9396    aubio_tensor_t *activations)
    9497{
    95   uint_t i, j, k;
    96   uint_t jj;
    9798  smpl_t s;
     99  uint_t i, j;
     100  uint_t ii = 0;
     101  uint_t length = activations->shape[activations->ndim - 1];
     102  uint_t height = activations->size / length;
     103
    98104  AUBIO_ASSERT(c);
    99105  AUBIO_ASSERT_EQUAL_SHAPE(input_tensor, activations);
    100   if (input_tensor->ndim == 3) {
    101     for (i = 0; i < activations->shape[0]; i++) {
    102       jj = 0;
    103       for (j = 0; j < activations->shape[1]; j++) {
    104         for (k = 0; k < activations->shape[2]; k++) {
    105           s = input_tensor->data[i][jj + k];
    106           s -= c->moving_mean->data[k];
    107           s *= c->gamma->data[k];
    108           s /= SQRT(c->moving_variance->data[k] + 1.e-4);
    109           s += c->beta->data[k];
    110           activations->data[i][jj + k] = s;
    111         }
    112         jj += activations->shape[2];
    113       }
     106  AUBIO_ASSERT(length == c->n_outputs);
     107  AUBIO_ASSERT(height * length == activations->size);
     108
     109  for (i = 0; i < height; i++) {
     110    for (j = 0; j < length; j++) {
     111      s = input_tensor->buffer[ii + j];
     112      s -= c->moving_mean->data[j];
     113      s *= c->gamma->data[j];
     114      s /= SQRT(c->moving_variance->data[j] + 1.e-4);
     115      s += c->beta->data[j];
     116      activations->buffer[ii + j] = s;
    114117    }
    115   } else if (input_tensor->ndim == 2) {
    116     for (i = 0; i < activations->shape[0]; i++) {
    117       for (j = 0; j < activations->shape[1]; j++) {
    118         s = input_tensor->data[i][j];
    119         s -= c->moving_mean->data[j];
    120         s *= c->gamma->data[j];
    121         s /= SQRT(c->moving_variance->data[j] + 1.e-4);
    122         s += c->beta->data[j];
    123         activations->data[i][j] = s;
    124       }
    125     }
     118    ii += length;
    126119  }
    127120}
Note: See TracChangeset for help on using the changeset viewer.