Skip to content

Commit

Permalink
fix typescript errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mubaidr committed Oct 25, 2020
1 parent 7bdc2b5 commit 3d001bb
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 47 deletions.
7 changes: 3 additions & 4 deletions __tests__/istanbul-linker-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ class Recurse {
* @param {Function} fn
* @returns string
*/
function istanbulLinkerUtil(fn) {
const source = fn.toString();
// eslint-disable-next-line @typescript-eslint/ban-types
export function istanbulLinkerUtil(fn: Function): string {
const source: string = fn.toString();
const links = new Set();
const ast = parse(`function fakeFunction() {${source}}`, {
ecmaVersion: 2020,
Expand All @@ -167,5 +168,3 @@ function istanbulLinkerUtil(fn) {
recurse.into(ast);
return Array.from(links).join('') + source;
}

export default istanbulLinkerUtil;
63 changes: 43 additions & 20 deletions __tests__/recurrent/unit.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { GPU } from 'gpu.js';
import { add, input, multiply, output, random, rnnCell } from '../../src/layer';
// TODO Recurrent was updated but tests not. Error with typing issues
import { ILayer } from '../../src/layer/base-layer';
import { Filter } from '../../src/layer/filter';
import { RecurrentInput } from '../../src/layer/recurrent-input';
import { Recurrent } from '../../src/recurrent';
import { Matrix } from '../../src/recurrent/matrix';

import { GPU } from 'gpu.js';
// TODO Recurrent was updated but tests not. Error with typing issues
// const { Recurrent, layer } = brain;
const { Recurrent, layer } = require('../../src');
const { add, input, multiply, output, random, rnnCell } = layer;
import { setup, teardown } from '../../src/utilities/kernel';
import { Filter } from '../../src/layer/filter';
import { injectIstanbulCoverage } from '../test-utils';

function copy2D(matrix: Partial<Matrix> & any[][]) {
Expand Down Expand Up @@ -42,7 +40,10 @@ describe('Recurrent Class: Unit', () => {

net.initialize();

const layers = net._layerSets[net._layerSets.length - 1];
const layers = net._layerSets
? net._layerSets[net._layerSets.length - 1]
: [];

expect(layers.map((l: ILayer) => l.constructor.name)).toEqual([
'Input',
'Random',
Expand Down Expand Up @@ -82,11 +83,16 @@ describe('Recurrent Class: Unit', () => {
net.initialize();
net.initializeDeep();

const spySets = net._layerSets.map((layerSet: ILayer[]) =>
layerSet.map((l) => jest.spyOn(l, 'predict'))
);
const spySets = net._layerSets
? net._layerSets.map((layerSet: ILayer[]) =>
layerSet.map((l) => jest.spyOn(l, 'predict'))
)
: [];

net.runInput([0, 1]);

if (!net._model) fail();

expect(net._model.length).toEqual(3);
for (let i = 0; i < spySets.length; i++) {
for (let j = 0; j < spySets[i].length; j++) {
Expand All @@ -95,6 +101,7 @@ describe('Recurrent Class: Unit', () => {
}
});
});

describe('.calculateDeltas()', () => {
test('back propagates values through deltas', () => {
const net = new Recurrent({
Expand All @@ -114,6 +121,10 @@ describe('Recurrent Class: Unit', () => {
net.initialize();
net.initializeDeep();
net.runInput([1, 1]);

if (!net._model) fail();
if (!net._layerSets) fail();

expect(net._model.length).toEqual(3);
expect(net._layerSets.length).toEqual(2);

Expand Down Expand Up @@ -172,6 +183,10 @@ describe('Recurrent Class: Unit', () => {
net.initialize();
net.initializeDeep();
net.runInput([1, 1]);

if (!net._model) fail();
if (!net._layerSets) fail();

expect(net._model.length).toEqual(3);
expect(net._layerSets[0].length).toEqual(10);
const weightSets = net._model.map((l: ILayer) =>
Expand All @@ -192,7 +207,7 @@ describe('Recurrent Class: Unit', () => {
for (let row = 0; row < weights.length; row++) {
for (let col = 0; col < weights[row].length; col++) {
expect(weights[row][col]).not.toEqual(
net._model[i].weights[row][col]
(net._model[i].weights as number[][])[row][col]
);
}
}
Expand All @@ -202,11 +217,16 @@ describe('Recurrent Class: Unit', () => {
describe('._trainPattern()', () => {
test('steps back through values correctly', () => {
class SuperLayer extends Filter {
errors?: number[][];
constructor(inputLayer: ILayer) {
super();
super(inputLayer);
this.inputLayer = inputLayer;
this.width = 1;
this.height = 1;

this.settings.width = 1;
this.settings.height = 1;

// this.width = 1;
// this.height = 1;
}

setupKernels() {}
Expand Down Expand Up @@ -255,19 +275,22 @@ describe('Recurrent Class: Unit', () => {
net.initialize();
net.initializeDeep();

const lastLayerSet = net._layerSets[net._layerSets.length - 1];
const lastLayerSet = net._layerSets
? net._layerSets[net._layerSets.length - 1]
: [];

const lastOutputLayer = lastLayerSet[lastLayerSet.length - 1];
expect(lastOutputLayer.weights).toEqual([new Float32Array([0])]);
net._trainPattern([1, 2], [3]);
net._trainPattern([1, 2], false);
const weights1 = lastOutputLayer.weights;
expect(weights1).not.toEqual([[0]]);
net._trainPattern([3, 2], [1]);
net._trainPattern([3, 2], false);
const weights2 = lastOutputLayer.weights;
expect(weights1).not.toEqual(weights2);
net._trainPattern([1, 1], [1]);
net._trainPattern([1, 1], false);
const weights3 = lastOutputLayer.weights;
expect(weights2).not.toEqual(weights3);
net._trainPattern([3, 3], [3]);
net._trainPattern([3, 3], false);
const weights4 = lastOutputLayer.weights;
expect(weights3).not.toEqual(weights4);
});
Expand Down
9 changes: 8 additions & 1 deletion src/feed-forward.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IKernelFunctionThis, KernelOutput, Texture } from 'gpu.js';
import { MeanSquaredError } from './estimator/mean-squared-error';
import { ILayer, ILayerJSON } from './layer/base-layer';
import { RecurrentInput } from './layer/recurrent-input';
import { Model } from './layer/types';
import {
InputOutputValue,
Expand Down Expand Up @@ -57,7 +58,9 @@ interface IFeedForwardTrainingOptions {
interface IFeedForwardOptions {
learningRate?: number;
binaryThresh?: number;
hiddenLayers?: Array<(inputLayer: ILayer, index: number) => ILayer>;
hiddenLayers?: Array<
(inputLayer: ILayer, recurrentInput: RecurrentInput) => ILayer
>;
inputLayer?: () => ILayer;
outputLayer?: (inputLayer: ILayer, index: number) => ILayer;
praxisOpts?: Partial<IPraxisSettings>;
Expand Down Expand Up @@ -258,13 +261,17 @@ export class FeedForward<
this._hiddenLayers = [];
const result: ILayer[] = [];
const { hiddenLayers } = this.options;

if (!hiddenLayers) throw new Error('hiddenLayers not defined');

for (let i = 0; i < hiddenLayers.length; i++) {
// @ts-expect-error not sure about the definiton Integer or ReccurrentInput?
const hiddenLayer = hiddenLayers[i](previousLayer, i);
result.push(hiddenLayer);
this._hiddenLayers.push(hiddenLayer);
previousLayer = hiddenLayer;
}

return result;
}

Expand Down
5 changes: 3 additions & 2 deletions src/layer/base-layer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { release, clear } from '../utilities/kernel';
import {
IKernelRunShortcut,
Input,
Kernel,
KernelOutput,
Texture,
TextureArrayOutput,
Input,
} from 'gpu.js';
import { IPraxis, IPraxisSettings } from '../praxis/base-praxis';
import { clear, release } from '../utilities/kernel';

export interface ILayerJSON {
width?: number;
Expand Down Expand Up @@ -45,6 +45,7 @@ export interface ILayer {
}

export interface ILayerSettings {
name?: string;
width?: number | null;
height?: number | null;
depth?: number | null;
Expand Down
2 changes: 1 addition & 1 deletion src/layer/filter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseLayer, ILayer, ILayerSettings } from './base-layer';
import { KernelOutput } from 'gpu.js';
import { BaseLayer, ILayer, ILayerSettings } from './base-layer';

export interface IFilterSettings extends ILayerSettings {
filterCount: number;
Expand Down
5 changes: 2 additions & 3 deletions src/layer/random.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { baseLayerDefaultSettings, ILayer, ILayerSettings } from './base-layer';

import { Model } from './types';
import { randos2D } from '../utilities/randos';
import { zeros2D } from '../utilities/zeros-2d';
import { baseLayerDefaultSettings, ILayer, ILayerSettings } from './base-layer';
import { Model } from './types';

export interface IRandomSettings extends ILayerSettings {
std?: number | null;
Expand Down
8 changes: 4 additions & 4 deletions src/layer/recurrent-input.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Internal } from './internal';
import { BaseLayer, ILayer } from './base-layer';
import { release } from '../utilities/kernel';
import { KernelOutput } from 'gpu.js';
import { IPraxis } from '../praxis/base-praxis';
import { release } from '../utilities/kernel';
import { BaseLayer, ILayer } from './base-layer';
import { Internal } from './internal';

export class RecurrentInput extends Internal implements ILayer {
export class RecurrentInput extends Internal {
recurrentInput: ILayer;
praxis: IPraxis | null = null;
predictKernel: any = null;
Expand Down
18 changes: 9 additions & 9 deletions src/layer/recurrent-zeros.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IPraxis } from '../praxis/base-praxis';
import { clear, release } from '../utilities/kernel';
import { zeros2D } from '../utilities/zeros-2d';
import { Internal } from './internal';
import { release, clear } from '../utilities/kernel';
import { ILayer, ILayerSettings } from './base-layer';
import { IPraxis } from '../praxis/base-praxis';
import { Internal } from './internal';

export class RecurrentZeros extends Internal implements ILayer {
praxis: IPraxis | null = null;
Expand Down Expand Up @@ -48,13 +48,13 @@ export class RecurrentZeros extends Internal implements ILayer {
clear(this.deltas);
}

validate(): void {
throw new Error(`${this.constructor.name}-validate is not yet implemented`);
}
// validate(): void {
// throw new Error(`${this.constructor.name}-validate is not yet implemented`);
// }

reset(): void {
throw new Error(`${this.constructor.name}-reset is not yet implemented`);
}
// reset(): void {
// throw new Error(`${this.constructor.name}-reset is not yet implemented`);
// }
}

export function recurrentZeros(): RecurrentZeros {
Expand Down
6 changes: 3 additions & 3 deletions src/layer/rnn-cell.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ILayer, ILayerSettings } from './base-layer';
import { RecurrentZeros } from './recurrent-zeros';
import { relu } from './relu';
import { add } from './add';
import { ILayer, ILayerSettings } from './base-layer';
import { multiply } from './multiply';
import { random } from './random';
import { RecurrentZeros } from './recurrent-zeros';
import { relu } from './relu';
import { zeros } from './zeros';

export function rnnCell(
Expand Down

0 comments on commit 3d001bb

Please sign in to comment.