2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <cker/train/operation/Loss.h>
19 #include <gtest/gtest.h>
22 TEST(CKer_Operation, LossMSE)
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};
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());
34 EXPECT_EQ(output[0], expected[0]);
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};
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());
47 EXPECT_FLOAT_EQ(output[0], expected[0]);
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};
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());
60 EXPECT_FLOAT_EQ(output[0], expected[0]);
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};
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());
75 EXPECT_FLOAT_EQ(output[0], expected[0]);
79 TEST(CKer_Operation, neg_LossMSE)
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};
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());
91 EXPECT_NE(output[0], expected[0]);
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};
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()));
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};
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()));
119 TEST(CKer_Operation, LossMSEGrad)
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};
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());
131 for (size_t i = 0; i < deriv_y_pred.size(); ++i)
132 EXPECT_EQ(deriv_y_pred[i], expected[i]);
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};
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());
145 for (size_t i = 0; i < deriv_y_pred.size(); ++i)
146 EXPECT_FLOAT_EQ(deriv_y_pred[i], expected[i]);
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};
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());
159 for (size_t i = 0; i < deriv_y_pred.size(); ++i)
160 EXPECT_FLOAT_EQ(deriv_y_pred[i], expected[i]);
164 TEST(CKer_Operation, neg_LossMSEGrad)
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.};
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());
176 for (size_t i = 0; i < deriv_y_pred.size(); ++i)
177 EXPECT_NE(deriv_y_pred[i], expected[i]);
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);
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()));
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);
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()));