Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compute / cker / src / train / Loss.test.cc
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <cker/train/operation/Loss.h>
18
19 #include <gtest/gtest.h>
20 #include <vector>
21
22 TEST(CKer_Operation, LossMSE)
23 {
24   {
25     // Shape: {1, 10} -> m_rows:10, m_cols:1
26     std::vector<int> y_pred = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
27     std::vector<int> y_true = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
28     std::vector<int> output(1);
29     std::vector<int> expected = {1};
30
31     nnfw::cker::train::MSE(nnfw::cker::Shape{1, 10}, y_pred.data(), nnfw::cker::Shape{1, 10},
32                            y_true.data(), nnfw::cker::Shape{1}, output.data());
33
34     EXPECT_EQ(output[0], expected[0]);
35   }
36
37   {
38     // Shape: {1, 10} -> m_rows:10, m_cols:1
39     std::vector<float> y_pred = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
40     std::vector<float> y_true = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9.};
41     std::vector<float> output(1);
42     std::vector<float> expected = {1.0};
43
44     nnfw::cker::train::MSE(nnfw::cker::Shape{1, 10}, y_pred.data(), nnfw::cker::Shape{1, 10},
45                            y_true.data(), nnfw::cker::Shape{1}, output.data());
46
47     EXPECT_FLOAT_EQ(output[0], expected[0]);
48   }
49
50   {
51     // Shape: {2, 3} -> m_rows:3, m_cols:2
52     std::vector<float> y_pred = {27.2, 31.8, 51.9, 10.2, 34.2, 12.4};
53     std::vector<float> y_true = {31.3, 40.3, 29.7, 12.9, 25.8, 11.9};
54     std::vector<float> output(1);
55     std::vector<float> expected = {110.0};
56
57     nnfw::cker::train::MSE(nnfw::cker::Shape{2, 3}, y_pred.data(), nnfw::cker::Shape{2, 3},
58                            y_true.data(), nnfw::cker::Shape{1}, output.data());
59
60     EXPECT_FLOAT_EQ(output[0], expected[0]);
61   }
62
63   {
64     // Shape: {2, 3, 4} -> m_rows:4, m_cols:6
65     std::vector<float> y_pred = {1., 2., 3., 4., 1., 2., 3., 4., 1., 2., 3., 4.,
66                                  1., 2., 3., 4., 1., 2., 3., 4., 1., 2., 3., 4.};
67     std::vector<float> y_true = {1., 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.,
68                                  1., 1., 1., 1., 2., 2., 2., 2., 3., 3., 3., 3.};
69     std::vector<float> output(1);
70     std::vector<float> expected = {2.1666667};
71
72     nnfw::cker::train::MSE(nnfw::cker::Shape{2, 3, 4}, y_pred.data(), nnfw::cker::Shape{2, 3, 4},
73                            y_true.data(), nnfw::cker::Shape{1}, output.data());
74
75     EXPECT_FLOAT_EQ(output[0], expected[0]);
76   }
77 }
78
79 TEST(CKer_Operation, neg_LossMSE)
80 {
81   {
82     // Invalid expected value
83     std::vector<float> y_pred = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
84     std::vector<float> y_true = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9.};
85     std::vector<float> output(1);
86     std::vector<float> expected = {-1.0};
87
88     nnfw::cker::train::MSE(nnfw::cker::Shape{2, 3, 4}, y_pred.data(), nnfw::cker::Shape{2, 3, 4},
89                            y_true.data(), nnfw::cker::Shape{1}, output.data());
90
91     EXPECT_NE(output[0], expected[0]);
92   }
93
94   {
95     // Invalid output shape
96     std::vector<float> y_pred = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
97     std::vector<float> y_true = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9.};
98     std::vector<float> output(3);
99     std::vector<float> expected = {1.0};
100
101     EXPECT_ANY_THROW(nnfw::cker::train::MSE(nnfw::cker::Shape{2, 3, 4}, y_pred.data(),
102                                             nnfw::cker::Shape{2, 3, 4}, y_true.data(),
103                                             nnfw::cker::Shape{3}, output.data()));
104   }
105
106   {
107     // Different y_pread and y_true shape
108     std::vector<float> y_pred = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
109     std::vector<float> y_true = {0., 1., 2., 3., 4., 5.};
110     std::vector<float> output(1);
111     std::vector<float> expected = {1.0};
112
113     EXPECT_ANY_THROW(nnfw::cker::train::MSE(nnfw::cker::Shape{2, 3, 4}, y_pred.data(),
114                                             nnfw::cker::Shape{2, 3}, y_true.data(),
115                                             nnfw::cker::Shape{1}, output.data()));
116   }
117 }
118
119 TEST(CKer_Operation, LossMSEGrad)
120 {
121   {
122     // Shape: {1, 10} -> m_rows:10, m_cols:1
123     std::vector<int> y_pred = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
124     std::vector<int> y_true = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
125     std::vector<int> deriv_y_pred(10);
126     std::vector<int> expected = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
127
128     nnfw::cker::train::MSEGrad(nnfw::cker::Shape{1, 10}, y_pred.data(), nnfw::cker::Shape{1, 10},
129                                y_true.data(), nnfw::cker::Shape{1, 10}, deriv_y_pred.data());
130
131     for (size_t i = 0; i < deriv_y_pred.size(); ++i)
132       EXPECT_EQ(deriv_y_pred[i], expected[i]);
133   }
134
135   {
136     // Shape: {1, 10} -> m_rows:10, m_cols:1
137     std::vector<float> y_pred = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
138     std::vector<float> y_true = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9.};
139     std::vector<float> deriv_y_pred(10);
140     std::vector<float> expected = {0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2};
141
142     nnfw::cker::train::MSEGrad(nnfw::cker::Shape{1, 10}, y_pred.data(), nnfw::cker::Shape{1, 10},
143                                y_true.data(), nnfw::cker::Shape{1, 10}, deriv_y_pred.data());
144
145     for (size_t i = 0; i < deriv_y_pred.size(); ++i)
146       EXPECT_FLOAT_EQ(deriv_y_pred[i], expected[i]);
147   }
148
149   {
150     // Shape: {2, 3} -> m_rows:3, m_cols:2
151     std::vector<float> y_pred = {27.2, 31.8, 51.9, 10.2, 34.2, 12.4};
152     std::vector<float> y_true = {31.3, 40.3, 29.7, 12.9, 25.8, 11.9};
153     std::vector<float> deriv_y_pred(6);
154     std::vector<float> expected = {-1.3666667, -2.8333333, 7.4, -0.9, 2.8, 0.1666667};
155
156     nnfw::cker::train::MSEGrad(nnfw::cker::Shape{2, 3}, y_pred.data(), nnfw::cker::Shape{2, 3},
157                                y_true.data(), nnfw::cker::Shape{2, 3}, deriv_y_pred.data());
158
159     for (size_t i = 0; i < deriv_y_pred.size(); ++i)
160       EXPECT_FLOAT_EQ(deriv_y_pred[i], expected[i]);
161   }
162 }
163
164 TEST(CKer_Operation, neg_LossMSEGrad)
165 {
166   {
167     // Invalid expected value
168     std::vector<float> y_pred = {27.2, 31.8, 51.9, 10.2, 34.2, 12.4};
169     std::vector<float> y_true = {31.3, 40.3, 29.7, 12.9, 25.8, 11.9};
170     std::vector<float> deriv_y_pred(6);
171     std::vector<float> expected = {1., 1., 1., 1., 1., 1.};
172
173     nnfw::cker::train::MSEGrad(nnfw::cker::Shape{2, 3}, y_pred.data(), nnfw::cker::Shape{2, 3},
174                                y_true.data(), nnfw::cker::Shape{2, 3}, deriv_y_pred.data());
175
176     for (size_t i = 0; i < deriv_y_pred.size(); ++i)
177       EXPECT_NE(deriv_y_pred[i], expected[i]);
178   }
179
180   {
181     // Different y_pred and y_true shape
182     std::vector<float> y_pred = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
183     std::vector<float> y_true = {0., 1., 2., 3., 4., 5.};
184     std::vector<float> deriv_y_pred(10);
185
186     EXPECT_ANY_THROW(nnfw::cker::train::MSEGrad(nnfw::cker::Shape{1, 10}, y_pred.data(),
187                                                 nnfw::cker::Shape{2, 3}, y_true.data(),
188                                                 nnfw::cker::Shape{1, 10}, deriv_y_pred.data()));
189   }
190
191   {
192     // Different y_pred and deriv_y_pred shape
193     std::vector<float> y_pred = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
194     std::vector<float> y_true = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9.};
195     std::vector<float> deriv_y_pred(6);
196
197     EXPECT_ANY_THROW(nnfw::cker::train::MSEGrad(nnfw::cker::Shape{1, 10}, y_pred.data(),
198                                                 nnfw::cker::Shape{1, 10}, y_true.data(),
199                                                 nnfw::cker::Shape{2, 3}, deriv_y_pred.data()));
200   }
201 }