Skip to content

Commit

Permalink
Added tests on convolutional layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Pravez committed May 29, 2018
1 parent 92dcd0f commit b206a93
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"setup": "npm install",
"clean": "rm -rf node_modules/ dist/",
"doc": "typedoc --theme markdown --out ./docs/ ./src/ --excludeExternals --externalPattern \"**/node_modules/**\" --ignoreCompilerErrors --name \"ReImproveJS\" --mode \"file\" --target ES5 --includeDeclarations",
"test": "mocha --reporter spec --compilers ts:ts-node/register 'test/*.spec.ts' --timeout 15000",
"test": "mocha --reporter spec --compilers ts:ts-node/register 'test/*.spec.ts' --timeout 120000",
"build": "./scripts/build-npm.sh",
"watch": "tsc -w -p ."
},
Expand Down
29 changes: 24 additions & 5 deletions test/model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,35 @@ describe('Old model', () => {
});

const network = new ConvolutionalNeuralNetwork();
network.InputShape = [5, 5, 1];
network.addConvolutionalLayers([32, 64]);
network.InputShape = [40, 40, 3];
network.addConvolutionalLayer(32);
network.addMaxPooling2DLayer();
network.addNeuralNetworkLayers([128, 128, 2]);
const nmodel = Model.FromNetwork(network);
nmodel.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
network.addConvolutionalLayer(64);
network.addMaxPooling2DLayer();
network.addNeuralNetworkLayers([128, {type: 'dense', activation:'softmax', units:2}]);
const nmodel = Model.FromNetwork(network, {stepsPerEpoch:10, epochs:1});
nmodel.compile({loss: tf.losses.softmaxCrossEntropy, optimizer: 'adam'});

describe('New model', () => {
it('should have the right output size', () => {
for (let i = 0; i < 10; ++i)
expect(nmodel.randomOutput()).to.be.within(0, numActions);
});

it('can be trained', async () => {
const x = tf.randomNormal([1, 40, 40, 3]);
const y = tf.tensor([[0, 1]]);

for(let i = 0;i < 5; ++i) {
await nmodel.fit(x, y);
}

let results = [];
for(let i = 0;i < 10; ++i)
results.push(nmodel.predict(x).getAction());

expect(results.reduce((p, c) => p + c)).to.be.greaterThan(7);
});
});


9 changes: 5 additions & 4 deletions test/network.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Networks', () => {
network.InputShape = [5, 5, 1];
network.addConvolutionalLayers([32, 64]);
network.addMaxPooling2DLayer({type: "maxpooling", strides: [5, 5]});
network.addNeuralNetworkLayers([128, 128, 2]);
network.addNeuralNetworkLayers([{type: 'dense', units: 256, name: 'test'}, 128, 2]);

const layers = network.getLayers();

Expand All @@ -29,10 +29,11 @@ describe('Networks', () => {
type: 'flatten'
});

expect(layers[5]).to.be.deep.equal({
expect(layers[4]).to.be.deep.equal({
type: 'dense',
units: 128,
activation: 'relu'
units: 256,
activation: 'relu',
name:'test'
});

});
Expand Down

0 comments on commit b206a93

Please sign in to comment.