Skip to content

Commit 7499f51

Browse files
committed
Add test
1 parent f64bb12 commit 7499f51

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed

tests/lib/model/layer/div.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('div', () => {
4242
c.map(v => Math.random() < 0 ? -v : v)
4343
const t = Matrix.random(1, 3, -0.1, 0.1)
4444

45-
for (let i = 0; i < 100; i++) {
45+
for (let i = 0; i < 1000; i++) {
4646
const loss = net.fit({ a, b, c }, t, 1000, 0.01)
4747
if (loss[0] < 1.0e-8) {
4848
break

tests/lib/model/layer/softargmax.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('softargmax', () => {
1010
const t = x.argmax(1)
1111
for (let i = 0; i < t.rows; i++) {
1212
for (let j = 0; j < t.cols; j++) {
13-
expect(y.at(i, j)).toBeCloseTo(t.at(i, j))
13+
expect(y.at(i, j)).toBeCloseTo(t.at(i, j), 1)
1414
}
1515
}
1616
})

tests/lib/util/math.test.js

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,39 @@ describe('Matrix', () => {
12931293
})
12941294
})
12951295

1296-
test.todo('sort')
1296+
describe('sort', () => {
1297+
test('axis 0', () => {
1298+
const org = Matrix.randn(10, 5)
1299+
const mat = org.copy()
1300+
const p = mat.sort(0)
1301+
for (let i = 0; i < org.rows; i++) {
1302+
let comp = true
1303+
for (let j = 0; j < org.cols; j++) {
1304+
expect(mat.at(i, j)).toBe(org.at(p[i], j))
1305+
if (i > 0 && comp) {
1306+
expect(mat.at(i, j)).toBeGreaterThanOrEqual(mat.at(i - 1, j))
1307+
comp &= mat.at(i, j) === mat.at(i - 1, j)
1308+
}
1309+
}
1310+
}
1311+
})
1312+
1313+
test('axis 1', () => {
1314+
const org = Matrix.randn(5, 10)
1315+
const mat = org.copy()
1316+
const p = mat.sort(1)
1317+
for (let j = 0; j < org.cols; j++) {
1318+
let comp = true
1319+
for (let i = 0; i < org.rows; i++) {
1320+
expect(mat.at(i, j)).toBe(org.at(i, p[j]))
1321+
if (j > 0 && comp) {
1322+
expect(mat.at(i, j)).toBeGreaterThanOrEqual(mat.at(i, j - 1))
1323+
comp &= mat.at(i, j) === mat.at(i, j - 1)
1324+
}
1325+
}
1326+
}
1327+
})
1328+
})
12971329

12981330
test.todo('shuffle')
12991331

@@ -2181,11 +2213,75 @@ describe('Matrix', () => {
21812213
test.todo('fail')
21822214
})
21832215

2184-
test.todo('invLowerTriangular')
2216+
describe('invLowerTriangular', () => {
2217+
test.each([0, 1, 2, 3, 10])('sizes[%i]', n => {
2218+
const mat = Matrix.randn(n, n)
2219+
for (let i = 0; i < n; i++) {
2220+
for (let j = i + 1; j < n; j++) {
2221+
mat.set(i, j, 0)
2222+
}
2223+
}
2224+
const inv = mat.invLowerTriangular()
2225+
2226+
const eye = mat.dot(inv)
2227+
for (let i = 0; i < n; i++) {
2228+
for (let j = 0; j < n; j++) {
2229+
if (i === j) {
2230+
expect(eye.at(i, j)).toBeCloseTo(1)
2231+
} else {
2232+
expect(eye.at(i, j)).toBeCloseTo(0)
2233+
}
2234+
}
2235+
}
2236+
})
2237+
2238+
test.todo('fail')
2239+
})
2240+
2241+
describe('invUpperTriangular', () => {
2242+
test.each([0, 1, 2, 3, 10])('sizes[%i]', n => {
2243+
const mat = Matrix.randn(n, n)
2244+
for (let i = 0; i < n; i++) {
2245+
for (let j = 0; j < i; j++) {
2246+
mat.set(i, j, 0)
2247+
}
2248+
}
2249+
const inv = mat.invUpperTriangular()
2250+
2251+
const eye = mat.dot(inv)
2252+
for (let i = 0; i < n; i++) {
2253+
for (let j = 0; j < n; j++) {
2254+
if (i === j) {
2255+
expect(eye.at(i, j)).toBeCloseTo(1)
2256+
} else {
2257+
expect(eye.at(i, j)).toBeCloseTo(0)
2258+
}
2259+
}
2260+
}
2261+
})
2262+
2263+
test.todo('fail')
2264+
})
2265+
2266+
describe('invRowReduction', () => {
2267+
test.each([0, 1, 2, 3, 10])('symmetric sizes[%i]', n => {
2268+
const mat = Matrix.randn(n, n).gram()
2269+
const inv = mat.invRowReduction()
21852270

2186-
test.todo('invUpperTriangular')
2271+
const eye = mat.dot(inv)
2272+
for (let i = 0; i < n; i++) {
2273+
for (let j = 0; j < n; j++) {
2274+
if (i === j) {
2275+
expect(eye.at(i, j)).toBeCloseTo(1)
2276+
} else {
2277+
expect(eye.at(i, j)).toBeCloseTo(0)
2278+
}
2279+
}
2280+
}
2281+
})
21872282

2188-
test.todo('invRowReduction')
2283+
test.todo('fail')
2284+
})
21892285

21902286
test.todo('invLU')
21912287

0 commit comments

Comments
 (0)