aboutsummaryrefslogtreecommitdiff
path: root/nerv/test/matrix_func.lua
blob: 90bb27fc7525fa2577feb8e446fd7e8b628695ea (plain) (blame)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
function _pattern_fill(mat_type, m, n)
    local a = mat_type(m, n)
    for i = 0, m - 1 do
        row = a[i]
        for j = 0, n - 1 do
            row[j] = i + j
        end
    end
    return a
end

function _test_all_shape(mat_type, m, n, k, fill)
    local a = fill(mat_type, m, n)
    local b = fill(mat_type, m, n)
    local c = fill(mat_type, m, n)
    for i = 0, m - 1 do
        for j = 0, n - 1 do
            a[i][j] = i + j
        end
    end
    -- test sigmoid
    b:sigmoid(a)
    print(a)
    print(b)
    -- test add
    c:add(a, b, 1.0, 2.0)
    print(c)
    c:add(a, b, 1.0, -2.0)
    print(c)
    c:add(a, b, 1.0, 0.0)
    print(c)
    c:add(a, b, 0.0, 1.0)
    print(c)
    -- test mul
    a = fill(mat_type, m, k)
    b = fill(mat_type, k, n)
    print(a)
    print(b)
    c:mul(a, b, 1.0, 0.0, 'N', 'N')
    print(c)
    c:mul(a, b, 1.0, 0.5, 'N', 'N')
    print(c)
    c = mat_type(n, m)
    c:mul(b, a, 1.0, 0.0, 'T', 'T')
    print(c)
    -- test colsum
    print(c:colsum())
    -- test colsame
    print(c:colsame(c))
    local d = c:create()
    d:copy_from(c)
    for j = 0, m - 1 do
        d[math.min(j, c:nrow() - 1)][j] = -1
    end
    print(c:colsame(d))
    -- test rowsum
    print(c:rowsum())
    -- test rowmax
    print(c:rowmax())
    d:copy_from(c)
    for i = 0, n - 1 do
        d[i][0] = 9999
    end
    print(d:rowmax())
    -- test fill
    d:fill(0)
    for i = 0, n - 1 do
        d[i][math.min(i, d:ncol() - 1)] = 1.0
    end
    print(d)
    -- test rowmax_idx
    x, y = d:rowmax_idx()
    print(x)
    print(y)
    -- test trans
    print(c)
    c:mul(b:trans(), a:trans(), 1.0, 0.0, 'N', 'N')
    print(c)
    -- test decompress
    c = mat_type(n, 1)
    for i = 0, n - 1 do
        c[i][0] = i
    end
    local e = c:decompress(n)
    print(e)
    -- test copy_from
    d = mat_type(n, n)
    d:copy_from(e)
    print(d)
    -- test add_row
    a = mat_type(1, n)
    for i = 0, n - 1 do
        a[0][i] = i
    end
    d:add_row(a, 0.5)
    print(d)
    -- test clip
    e:copy_from(d)
    e:clip(1, 2)
    print(e)
    -- test sigmoid_grad
    a = fill(mat_type, m, n)
    b = fill(mat_type, m, n)
    c = a:create()
    c:sigmoid_grad(a, b)
    print(c)
    -- test softmax
    for i = 0, m - 1 do
        a[i][math.min(i, a:ncol() - 1)] = a[i][0] * n
    end
    print(a)
    e = c:softmax(a)
    print(c)
    print(e)
    -- test mul_elem
    a:mul_elem(c, c)
    print(a)
    -- test log_elem
    a:log_elem(a)
    print(a)
    -- test copy_rows_from_by_idx
    local idx = mat_type(1, n)
    a = fill(mat_type, n, m)
    b = mat_type(n, m)
    for i = 0, n - 1 do
        idx[0][i] = n - 1 - i
    end
    print(a)
    b:copy_rows_from_by_idx(a, idx)
    b = mat_type(2, m)
    b:copy_rows_from_by_idx(a, idx, 2)
    print(a)
    print(b)
    -- test expand_frm
    a = mat_type(m, n)
    for i = 0, m - 1 do
        for j = 0, n - 1 do
            a[i][j] = i
        end
    end
    c = mat_type(m, n * (2 * k + 1))
    c:expand_frm(a, k)
    print(a)
    print(c)
    -- test rearrange_frm
    a = c:create()
    a:rearrange_frm(c, n)
    print(a)
    -- test scale_rows_by_row
    a = mat_type(n, m)
    a:fill(2)
    b = fill(mat_type, 1, m)
    c = a:create()
    print(a)
    a:scale_rows_by_row(b)
    print(a)
    -- test scale_rows_by_col
    a = fill(mat_type, m, n)
    b = fill(mat_type, m, 1)
    print(a)
    a:scale_rows_by_col(b)
    print(a)
    a = fill(mat_type, 3, 4)
    local c = a:create()
    c:tanh(a)
    print(c)
    a:add(a, c, 1.0, -3.0)
    c:relu(a)
    print(c)
end
function test_all(mat_type)
    _test_all_shape(mat_type, 3, 4, 2, _pattern_fill)
    _test_all_shape(mat_type, 30, 40, 20, _pattern_fill)
    _test_all_shape(mat_type, 10, 10, 10, _pattern_fill)
end