Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / compiler / nnc / utils / caffe_model_maker / Pyloss.py
1 """
2 COPYRIGHT
3
4 All contributions by the University of California:
5 Copyright (c) 2014-2017 The Regents of the University of California (Regents)
6 All rights reserved.
7
8 All other contributions:
9 Copyright (c) 2014-2017, the respective contributors
10 All rights reserved.
11
12 Caffe uses a shared copyright model: each contributor holds copyright over
13 their contributions to Caffe. The project versioning records all such
14 contribution and copyright details. If a contributor wants to further mark
15 their specific copyright on a particular contribution, they should indicate
16 their copyright solely in the commit message of the change when it is
17 committed.
18
19 LICENSE
20
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are met: 
23
24 1. Redistributions of source code must retain the above copyright notice, this
25    list of conditions and the following disclaimer. 
26 2. Redistributions in binary form must reproduce the above copyright notice,
27    this list of conditions and the following disclaimer in the documentation
28    and/or other materials provided with the distribution. 
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
34 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41 CONTRIBUTION AGREEMENT
42
43 By contributing to the BVLC/caffe repository through pull-request, comment,
44 or otherwise, the contributor releases their content to the
45 license and copyright terms herein.
46 """
47 import caffe
48 import numpy as np
49
50
51 class EuclideanLossLayer(caffe.Layer):
52     """
53     Compute the Euclidean Loss in the same manner as the C++ EuclideanLossLayer
54     to demonstrate the class interface for developing layers in Python.
55     """
56
57     def setup(self, bottom, top):
58         # check input pair
59         if len(bottom) != 2:
60             raise Exception("Need two inputs to compute distance.")
61
62     def reshape(self, bottom, top):
63         # check input dimensions match
64         if bottom[0].count != bottom[1].count:
65             raise Exception("Inputs must have the same dimension.")
66         # difference is shape of inputs
67         self.diff = np.zeros_like(bottom[0].data, dtype=np.float32)
68         # loss output is scalar
69         top[0].reshape(1)
70
71     def forward(self, bottom, top):
72         self.diff[...] = bottom[0].data - bottom[1].data
73         top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.
74
75     def backward(self, top, propagate_down, bottom):
76         for i in range(2):
77             if not propagate_down[i]:
78                 continue
79             if i == 0:
80                 sign = 1
81             else:
82                 sign = -1
83         bottom[i].diff[...] = sign * self.diff / bottom[i].num