-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathMSECriterion-test.c
103 lines (73 loc) · 2.11 KB
/
MSECriterion-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// MSECriterion-test.c
#include "MSECriterion.h"
#include "UnitTest.h"
//////////////////////////////////////// testBackward
static void testBackward()
{
MSECriterion_T mc = MSECriterion_new();
Tensor_T input = Tensor_newLinSpace(1, 3, 3);
Tensor_T target = Tensor_new1(3);
Tensor_fill(target, 10.0);
Tensor_T result = MSECriterion_backward(mc, input, target);
EXPECT_EQ_UNSIGNED(1, Tensor_nDimensions(result));
EXPECT_EQ_UNSIGNED(3, Tensor_size0(result));
EXPECT_EQ_DOUBLE((2.0 / 3.0) * (1 - 10) * 1, Tensor_get1(result, 0), 0);
EXPECT_EQ_DOUBLE((2.0 / 3.0) * (2 - 10) * 2, Tensor_get1(result, 1), 0);
EXPECT_EQ_DOUBLE((2.0 / 3.0) * (3 - 10) * 3, Tensor_get1(result, 2), 0);
// free
MSECriterion_free(&mc);
EXPECT_NULL(mc);
Tensor_free(&result);
}
//////////////////////////////////////// testForward
static void testForward()
{
// 1d size 1
{
MSECriterion_T mc = MSECriterion_new();
Tensor_T input = Tensor_new1(1);
Tensor_fill(input, 2);
Tensor_T target = Tensor_new1(1);
Tensor_fill(target, 3);
double result = MSECriterion_forward(mc, input, target);
EXPECT_EQ_DOUBLE(1.0, result, 0);
// free
MSECriterion_free(&mc);
EXPECT_NULL(mc);
}
// TODO: size 6 and size 2 3
{
MSECriterion_T mc = MSECriterion_new();
Tensor_T input = Tensor_newLinSpace(1.0, 6.0, 6);
EXPECT_EQ_DOUBLE(1.0, Tensor_get1(input, 0), 0);
Tensor_T target = Tensor_new2(2, 3);
Tensor_fill(target, 3);
double result = MSECriterion_forward(mc, input, target);
EXPECT_EQ_DOUBLE(19.0 / 6.0, result, 0);
// free
MSECriterion_free(&mc);
EXPECT_NULL(mc);
}
}
//////////////////////////////////////// testFree
static void testFree()
{
MSECriterion_T mc = MSECriterion_new();
MSECriterion_free(&mc);
EXPECT_NULL(mc);
}
//////////////////////////////////////// testNew
static void testNew()
{
MSECriterion_T mc = MSECriterion_new();
EXPECT_NOT_NULL(mc);
}
//////////////////////////////////////// main
int main(int argc, char **argv)
{
testBackward();
testForward();
testFree();
testNew();
UnitTest_report();
}