tiny_dnn 1.0.0
A header only, dependency-free deep learning framework in C++11
Loading...
Searching...
No Matches
math_functions.h
1/*
2Copyright (c) 2016, Taiga Nomi
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7* Redistributions of source code must retain the above copyright
8notice, this list of conditions and the following disclaimer.
9* Redistributions in binary form must reproduce the above copyright
10notice, this list of conditions and the following disclaimer in the
11documentation and/or other materials provided with the distribution.
12* Neither the name of the <organization> nor the
13names of its contributors may be used to endorse or promote products
14derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27#pragma once
28#include <algorithm>
29
30#include "tiny_dnn/util/util.h"
31
32namespace tiny_dnn {
33
34// x = x / denom
35inline void vector_div(vec_t& x, float_t denom) {
36 std::transform(x.begin(), x.end(), x.begin(), [=](float_t x) { return x / denom; });
37}
38
42inline void moments(const tensor_t& in, serial_size_t spatial_dim, serial_size_t channels, vec_t *mean, vec_t *variance) {
43 serial_size_t num_examples = static_cast<serial_size_t>(in.size());
44
45 assert(in[0].size() == spatial_dim * channels);
46
47 mean->resize(channels);
48 std::fill(mean->begin(), mean->end(), (float_t)0.0);
49
50 if (variance != nullptr) {
51 variance->resize(channels);
52 std::fill(variance->begin(), variance->end(), (float_t)0.0);
53 }
54
55 // calculate mean
56 for (serial_size_t i = 0; i < num_examples; i++) {
57 for (serial_size_t j = 0; j < channels; j++) {
58 float_t* pmean = &mean->at(j);
59 const float_t* X = &in[i][j*spatial_dim];
60
61 for (serial_size_t k = 0; k < spatial_dim; k++) {
62 *pmean += *X++;
63 }
64 }
65 }
66
67 vector_div(*mean, (float_t)num_examples*spatial_dim);
68
69 // calculate variance
70 if (variance != nullptr) {
71 for (serial_size_t i = 0; i < num_examples; i++) {
72 for (serial_size_t j = 0; j < channels; j++) {
73 float_t* pvar = &variance->at(j);
74 const float_t* X = &in[i][j*spatial_dim];
75 float_t EX = (*mean)[j];
76
77 for (serial_size_t k = 0; k < spatial_dim; k++) {
78 *pvar += pow(*X++ - EX, (float_t)2.0);
79 }
80 }
81 }
82
83 vector_div(*variance, std::max(1.0f, num_examples*spatial_dim-1.0f));
84 }
85}
86
87}