Skip to content

Commit c58765c

Browse files
add a simple unit test for use in gpu layers and ensure there is a means to run tests on recurrent/matrix
1 parent 7e5ced2 commit c58765c

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
"scripts": {
1111
"test-base": "find ./test/base/ -name '*.js' | xargs mocha --compilers js:babel-core/register",
1212
"test-recurrent": "find ./test/recurrent/ -name '*.js' | xargs mocha --compilers js:babel-core/register",
13+
"test-recurrent-matrix": "find ./test/recurrent/matrix -name '*.js' | xargs mocha --compilers js:babel-core/register",
1314
"test-rnn": "find ./test/recurrent/ -name 'rnn.js' | xargs mocha --compilers js:babel-core/register",
1415
"test-gru": "find ./test/recurrent/ -name 'gru.js' | xargs mocha --compilers js:babel-core/register",
1516
"test-lstm": "find ./test/recurrent/ -name 'lstm.js' | xargs mocha --compilers js:babel-core/register",
1617
"test-utilities": "find ./test/utilities/ -name '*.js' | xargs mocha --compilers js:babel-core/register",
1718
"test-applications": "find ./test/applications -name '*.js' | xargs mocha --compilers js:babel-core/register",
1819
"test": "npm run test-base && npm run test-utilities",
19-
"test-experimental": "npm run test-recurrent && npm run test-utilities",
20+
"test-experimental": "npm run test-recurrent-matrix && npm run test-recurrent && npm run test-utilities",
2021
"dist": "babel src --out-dir dist --source-maps",
2122
"browser": "browserify ./index.js -p licensify -o browser.js",
2223
"browser-min": "browserify ./index.js -p licensify -g uglifyify -o browser.min.js",

src/recurrent/matrix/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,40 @@ export default class Matrix {
103103

104104
return m;
105105
}
106+
107+
weightsToArray() {
108+
const deltas = [];
109+
let row = 0;
110+
let column = 0;
111+
for (let i = 0; i < this.weights.length; i++) {
112+
if (column === 0) {
113+
deltas.push([]);
114+
}
115+
deltas[row].push(this.weights[i]);
116+
column++;
117+
if (column >= this.columns) {
118+
column = 0;
119+
row++;
120+
}
121+
}
122+
return deltas;
123+
}
124+
125+
deltasToArray() {
126+
const deltas = [];
127+
let row = 0;
128+
let column = 0;
129+
for (let i = 0; i < this.deltas.length; i++) {
130+
if (column === 0) {
131+
deltas.push([]);
132+
}
133+
deltas[row].push(this.deltas[i]);
134+
column++;
135+
if (column >= this.columns) {
136+
column = 0;
137+
row++;
138+
}
139+
}
140+
return deltas;
141+
}
106142
}

test/recurrent/matrix/multiply.js

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ describe('matrix', () => {
77
describe('multiply', () => {
88
context('when given a left and right matrix both of 2 rows and 2 columns', () => {
99
it('correctly multiplies the values', () => {
10-
var m1 = Matrix.fromArray([
10+
const m1 = Matrix.fromArray([
1111
[2, 2],
1212
[2, 2]
1313
]);
14-
var m2 = Matrix.fromArray([
14+
const m2 = Matrix.fromArray([
1515
[2, 2],
1616
[2, 2]
1717
]);
18-
var result = new Matrix(2, 2);
18+
const result = new Matrix(2, 2);
1919
multiply(result, m1, m2);
20-
var weights = [8, 8, 8, 8];
20+
const weights = [8, 8, 8, 8];
2121
assert.equal(result.weights.length, 4);
2222
result.weights.forEach((value, i) => {
2323
assert.equal(value, weights[i]);
@@ -29,15 +29,15 @@ describe('matrix', () => {
2929
describe('multiplyB', () => {
3030
context('when given a left and right matrix both of 2 rows and 2 columns', () => {
3131
it('correctly multiplies the values', () => {
32-
var m1 = Matrix.fromArray([
32+
const m1 = Matrix.fromArray([
3333
[3, 3],
3434
[3, 3]
3535
]);
36-
var m2 = Matrix.fromArray([
36+
const m2 = Matrix.fromArray([
3737
[3, 3],
3838
[3, 3]
3939
]);
40-
var result = Matrix.fromArray([
40+
const result = Matrix.fromArray([
4141
[3, 3],
4242
[3, 3]
4343
]);
@@ -50,5 +50,46 @@ describe('matrix', () => {
5050
});
5151
});
5252
});
53+
context('when given two different size left and right', () => {
54+
it('calculates both values in different sizes correctly', () => {
55+
const productWeights = [
56+
[0],
57+
[0],
58+
[0]
59+
];
60+
const productDeltas = [
61+
[1],
62+
[2],
63+
[3]
64+
];
65+
const leftInputWeights = [
66+
[1, 2],
67+
[3, 4],
68+
[5, 6]
69+
];
70+
const leftInputDeltas = [
71+
[1, 2],
72+
[3, 4],
73+
[5, 6]
74+
];
75+
const rightInputWeights = [
76+
[1],
77+
[2]
78+
];
79+
const rightInputDeltas = [
80+
[1],
81+
[2]
82+
];
83+
84+
const product = Matrix.fromArray(productWeights, productDeltas);
85+
const left = Matrix.fromArray(leftInputWeights, leftInputDeltas);
86+
const right = Matrix.fromArray(rightInputWeights, rightInputDeltas);
87+
88+
multiplyB(product, left, right);
89+
90+
assert.deepEqual(left.deltasToArray(), [ [2, 4], [5, 8], [8, 12] ]);
91+
assert.deepEqual(right.deltasToArray(), [ [23], [30] ]);
92+
});
93+
});
5394
});
5495
});

0 commit comments

Comments
 (0)