move all layer params into attrs object

This commit is contained in:
Leon Chen
2016-09-18 23:20:59 -04:00
parent b1af2a7751
commit fd20f1b7b1
47 changed files with 285 additions and 162 deletions
+56
View File
@@ -184,7 +184,9 @@ export default class Model {
})
this.modelLayersMap.set(inputName, layer)
this.modelDAG[inputName] = {
layerClass: 'Input',
name: inputName,
inbound: [],
outbound: []
}
this.inputTensors[inputName] = new Tensor([], inputShape)
@@ -192,6 +194,9 @@ export default class Model {
if (layerClass in layers) {
const attrs = mapKeys(layerConfig, (v, k) => camelCase(k))
if ('activation' in attrs) {
attrs.activation = camelCase(attrs.activation)
}
const layer = new layers[layerClass](attrs)
// layer weights
@@ -214,13 +219,16 @@ export default class Model {
this.modelLayersMap.set(layerConfig.name, layer)
this.modelDAG[layerConfig.name] = {
layerClass,
name: layerConfig.name,
inbound: [],
outbound: []
}
if (index === 0) {
this.modelDAG[inputName].outbound.push(layerConfig.name)
} else {
const prevLayerConfig = modelConfig[index - 1].config
this.modelDAG[layerConfig.name].inbound.push(prevLayerConfig.name)
this.modelDAG[prevLayerConfig.name].outbound.push(layerConfig.name)
}
} else {
@@ -230,6 +238,39 @@ export default class Model {
}
}
/**
* Generator function for recursively traversing the DAG
*/
* traverseDAG (nodes) {
if (nodes.length === 0) {
return true
} else if (nodes.length === 1) {
const node = nodes[0]
const { layerClass, inbound, outbound } = this.modelDAG[node]
if (layerClass !== 'Input') {
let currentLayer = this.modelLayersMap.get(node)
console.log(currentLayer)
const inboundLayers = inbound.map(n => this.modelLayersMap.get(n))
while (!every(inboundLayers.map(layer => layer.hasResult))) {
yield
}
if (layerClass === 'Merge') {
currentLayer.result = currentLayer.call(inboundLayers.map(layer => layer.result))
currentLayer.hasResult = true
} else {
if (inboundLayers.length !== 1) {
throw new Error(`Layer name ${currentLayer.name} has ${inboundLayers.length} inbound nodes, but is not a Merge layer.`)
}
currentLayer.result = currentLayer.call(inboundLayers[0].result)
currentLayer.hasResult = true
}
}
yield * this.traverseDAG(outbound)
} else {
yield * nodes.map(node => this.traverseDAG([node]))
}
}
/**
* Predict API
*/
@@ -242,8 +283,23 @@ export default class Model {
throw new Error('predict() must take an object where the values are the flattened data as Float32Array.')
}
// reset hasResult flag in all layers
for (let layer of this.modelLayersMap.values()) {
layer.hasResult = false
}
// load data to input tensors
inputNames.forEach(inputName => {
this.inputTensors[inputName].tensor.data = inputData[inputName]
let inputLayer = this.modelLayersMap.get(inputName)
inputLayer.result = inputLayer.call(this.inputTensors[inputName])
this.modelLayersMap.get(inputName).hasResult = true
})
// start traversing DAG at input
let traversing = this.traverseDAG(inputNames)
while (!traversing.next().done) {
console.log('blah')
}
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ import * as layers from './layers'
let testUtils
if (process.env.NODE_ENV !== 'production') {
testUtils = require('./test-utils')
testUtils = require('./testUtils')
}
export {
+7 -2
View File
@@ -7,10 +7,15 @@ import cwise from 'cwise'
export default class ELU extends Layer {
/**
* Creates a ELU activation layer
* @param {number} alpha - scale for the negative factor
* @param {number} attrs.alpha - scale for the negative factor
*/
constructor (alpha = 1.0, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
alpha = 1.0
} = attrs
this.alpha = alpha
}
+7 -2
View File
@@ -7,10 +7,15 @@ import { relu } from '../../activations'
export default class LeakyReLU extends Layer {
/**
* Creates a LeakyReLU activation layer
* @param {number} alpha - negative slope coefficient
* @param {number} attrs.alpha - negative slope coefficient
*/
constructor (alpha = 0.3, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
alpha = 0.3
} = attrs
this.alpha = alpha
}
@@ -7,10 +7,15 @@ import cwise from 'cwise'
export default class ThresholdedReLU extends Layer {
/**
* Creates a ThresholdedReLU activation layer
* @param {number} theta - float >= 0. Threshold location of activation.
* @param {number} attrs.theta - float >= 0. Threshold location of activation.
*/
constructor (theta = 1.0, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
theta = 1.0
} = attrs
this.theta = theta
}
@@ -13,13 +13,13 @@ import flattenDeep from 'lodash/flattenDeep'
export default class AtrousConvolution2D extends Convolution2D {
/**
* Creates a AtrousConvolution2D layer
* @param {number} nbFilter - Number of convolution filters to use.
* @param {number} nbRow - Number of rows in the convolution kernel.
* @param {number} nbCol - Number of columns in the convolution kernel.
* @param {number} attrs.nbFilter - Number of convolution filters to use.
* @param {number} attrs.nbRow - Number of rows in the convolution kernel.
* @param {number} attrs.nbCol - Number of columns in the convolution kernel.
* @param {Object} [attrs] - layer attributes
*/
constructor (nbFilter, nbRow, nbCol, attrs = {}) {
super(nbFilter, nbRow, nbCol, attrs)
constructor (attrs = {}) {
super(attrs)
const {
atrousRate = [1, 1]
} = attrs
+9 -4
View File
@@ -9,13 +9,15 @@ import unsqueeze from 'ndarray-unsqueeze'
export default class Convolution1D extends Layer {
/**
* Creates a Convolution1D layer
* @param {number} nbFilter - Number of convolution filters to use.
* @param {number} filterLength - Length of 1D convolution kernel.
* @param {number} attrs.nbFilter - Number of convolution filters to use.
* @param {number} attrs.filterLength - Length of 1D convolution kernel.
* @param {Object} [attrs] - layer attributes
*/
constructor (nbFilter, filterLength, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
nbFilter = 1,
filterLength = 1,
activation = 'linear',
borderMode = 'valid',
subsampleLength = 1,
@@ -33,7 +35,10 @@ export default class Convolution1D extends Layer {
// Convolution1D is actually a shim on top of Convolution2D, where
// all of the computational action is performed
// Note that Keras uses `th` dim ordering here.
this._conv2d = new Convolution2D(nbFilter, filterLength, 1, {
this._conv2d = new Convolution2D({
nbFilter,
nbRow: filterLength,
nbCol: 1,
activation,
borderMode,
subsample: [subsampleLength, 1],
+7 -4
View File
@@ -12,14 +12,17 @@ import flattenDeep from 'lodash/flattenDeep'
export default class Convolution2D extends Layer {
/**
* Creates a Convolution2D layer
* @param {number} nbFilter - Number of convolution filters to use.
* @param {number} nbRow - Number of rows in the convolution kernel.
* @param {number} nbCol - Number of columns in the convolution kernel.
* @param {number} attrs.nbFilter - Number of convolution filters to use.
* @param {number} attrs.nbRow - Number of rows in the convolution kernel.
* @param {number} attrs.nbCol - Number of columns in the convolution kernel.
* @param {Object} [attrs] - layer attributes
*/
constructor (nbFilter, nbRow, nbCol, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
nbFilter = 1,
nbRow = 3,
nbCol = 3,
activation = 'linear',
borderMode = 'valid',
subsample = [1, 1],
+9 -5
View File
@@ -12,15 +12,19 @@ import flattenDeep from 'lodash/flattenDeep'
export default class Convolution3D extends Layer {
/**
* Creates a Convolution3D layer
* @param {number} nbFilter - Number of convolution filters to use.
* @param {number} kernelDim1 - Length of the first dimension in the convolution kernel.
* @param {number} kernelDim2 - Length of the second dimension in the convolution kernel.
* @param {number} kernelDim3 - Length of the third dimension in the convolution kernel.
* @param {number} attrs.nbFilter - Number of convolution filters to use.
* @param {number} attrs.kernelDim1 - Length of the first dimension in the convolution kernel.
* @param {number} attrs.kernelDim2 - Length of the second dimension in the convolution kernel.
* @param {number} attrs.kernelDim3 - Length of the third dimension in the convolution kernel.
* @param {Object} [attrs] - layer attributes
*/
constructor (nbFilter, kernelDim1, kernelDim2, kernelDim3, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
nbFilter = 1,
kernelDim1 = 1,
kernelDim2 = 1,
kernelDim3 = 1,
activation = 'linear',
borderMode = 'valid',
subsample = [1, 1, 1],
+9 -5
View File
@@ -12,16 +12,20 @@ import flattenDeep from 'lodash/flattenDeep'
export default class Deconvolution2D extends Layer {
/**
* Creates a Deconvolution2D layer
* @param {number} nbFilter - Number of convolution filters to use.
* @param {number} nbRow - Number of rows in the convolution kernel.
* @param {number} nbCol - Number of columns in the convolution kernel.
* @param {number[]} outputShape - Output shape of the transposed convolution operation.
* @param {number} attrs.nbFilter - Number of convolution filters to use.
* @param {number} attrs.nbRow - Number of rows in the convolution kernel.
* @param {number} attrs.nbCol - Number of columns in the convolution kernel.
* @param {number[]} attrs.outputShape - Output shape of the transposed convolution operation.
* Array of integers [nbFilter, outputRows, outputCols]
* @param {Object} [attrs] - layer attributes
*/
constructor (nbFilter, nbRow, nbCol, outputShape, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
nbFilter = 1,
nbRow = 1,
nbCol = 1,
outputShape = [],
activation = 'linear',
borderMode = 'valid',
subsample = [1, 1],
@@ -10,14 +10,17 @@ import ops from 'ndarray-ops'
export default class SeparableConvolution2D extends Layer {
/**
* Creates a SeparableConvolution2D layer
* @param {number} nbFilter - Number of convolution filters to use.
* @param {number} nbRow - Number of rows in the convolution kernel.
* @param {number} nbCol - Number of columns in the convolution kernel.
* @param {number} attrs.nbFilter - Number of convolution filters to use.
* @param {number} attrs.nbRow - Number of rows in the convolution kernel.
* @param {number} attrs.nbCol - Number of columns in the convolution kernel.
* @param {Object} [attrs] - layer attributes
*/
constructor (nbFilter, nbRow, nbCol, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
nbFilter = 1,
nbRow = 1,
nbCol = 1,
activation = 'linear',
borderMode = 'valid',
subsample = [1, 1],
@@ -53,10 +56,10 @@ export default class SeparableConvolution2D extends Layer {
// SeparableConvolution2D has two components: depthwise, and pointwise.
// Activation function and bias is applied at the end.
// Subsampling (striding) only performed on depthwise part, not the pointwise part.
const depthwiseConvAttrs = { activation: 'linear', borderMode, subsample, dimOrdering, bias: false }
const pointwiseConvAttrs = { activation: 'linear', borderMode, subsample: [1, 1], dimOrdering, bias: false }
this._depthwiseConv = new Convolution2D(this.depthMultiplier, nbRow, nbCol, depthwiseConvAttrs)
this._pointwiseConv = new Convolution2D(nbFilter, 1, 1, pointwiseConvAttrs)
const depthwiseConvAttrs = { nbFilter: this.depthMultiplier, nbRow, nbCol, activation: 'linear', borderMode, subsample, dimOrdering, bias: false }
const pointwiseConvAttrs = { nbFilter, nbRow: 1, nbCol: 1, activation: 'linear', borderMode, subsample: [1, 1], dimOrdering, bias: false }
this._depthwiseConv = new Convolution2D(depthwiseConvAttrs)
this._pointwiseConv = new Convolution2D(pointwiseConvAttrs)
}
/**
+5 -2
View File
@@ -8,10 +8,13 @@ import ops from 'ndarray-ops'
export default class UpSampling1D extends Layer {
/**
* Creates a UpSampling1D activation layer
* @param {number} length - upsampling factor
* @param {number} attrs.length - upsampling factor
*/
constructor (length = 2, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
length = 2
} = attrs
this.length = length
}
+3 -2
View File
@@ -8,11 +8,12 @@ import ops from 'ndarray-ops'
export default class UpSampling2D extends Layer {
/**
* Creates a UpSampling2D activation layer
* @param {number} size - upsampling factor
* @param {number} attrs.size - upsampling factor
*/
constructor (size = [2, 2], attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
size = [2, 2],
dimOrdering = 'tf'
} = attrs
+3 -2
View File
@@ -8,11 +8,12 @@ import ops from 'ndarray-ops'
export default class UpSampling3D extends Layer {
/**
* Creates a UpSampling3D activation layer
* @param {number} size - upsampling factor
* @param {number} attrs.size - upsampling factor
*/
constructor (size = [2, 2, 2], attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
size = [2, 2, 2],
dimOrdering = 'tf'
} = attrs
+5 -2
View File
@@ -8,10 +8,13 @@ import ops from 'ndarray-ops'
export default class ZeroPadding1D extends Layer {
/**
* Creates a ZeroPadding1D activation layer
* @param {number} padding - length of padding
* @param {number} attrs.padding - length of padding
*/
constructor (padding = 1, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
padding = 1
} = attrs
this.padding = padding
}
+3 -2
View File
@@ -8,11 +8,12 @@ import ops from 'ndarray-ops'
export default class ZeroPadding2D extends Layer {
/**
* Creates a ZeroPadding2D activation layer
* @param {number} padding - size of padding
* @param {number} attrs.padding - size of padding
*/
constructor (padding = [1, 1], attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
padding = [1, 1],
dimOrdering = 'tf'
} = attrs
+3 -2
View File
@@ -8,11 +8,12 @@ import ops from 'ndarray-ops'
export default class ZeroPadding3D extends Layer {
/**
* Creates a ZeroPadding3D activation layer
* @param {number} padding - size of padding
* @param {number} attrs.padding - size of padding
*/
constructor (padding = [1, 1, 1], attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
padding = [1, 1, 1],
dimOrdering = 'tf'
} = attrs
+7 -2
View File
@@ -7,10 +7,15 @@ import Layer from '../../Layer'
export default class Activation extends Layer {
/**
* Creates an Activation layer
* @param {string} activation - name of activation function
* @param {string} attrs.activation - name of activation function
*/
constructor (activation, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
activation = 'linear'
} = attrs
this.activation = activations[activation]
}
+3 -2
View File
@@ -10,12 +10,13 @@ import ops from 'ndarray-ops'
export default class Dense extends Layer {
/**
* Creates a Dense layer
* @param {number} outputDim - output dimension size
* @param {number} attrs.outputDim - output dimension size
* @param {Object} [attrs] - layer attributes
*/
constructor (outputDim, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
outputDim = 1,
activation = 'linear',
inputDim = null,
bias = true
+7 -2
View File
@@ -7,10 +7,15 @@ import Layer from '../../Layer'
export default class Dropout extends Layer {
/**
* Creates an Dropout layer
* @param {number} p - fraction of the input units to drop (between 0 and 1)
* @param {number} attrs.p - fraction of the input units to drop (between 0 and 1)
*/
constructor (p, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
p = 0.5
} = attrs
this.p = Math.min(Math.max(0, p), 1)
}
+3 -2
View File
@@ -13,12 +13,13 @@ import ops from 'ndarray-ops'
export default class MaxoutDense extends Layer {
/**
* Creates a MaxoutDense layer
* @param {number} outputDim - output dimension size
* @param {number} attrs.outputDim - output dimension size
* @param {Object} [attrs] - layer attributes
*/
constructor (outputDim, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
outputDim = 1,
inputDim = null,
bias = true
} = attrs
+5 -2
View File
@@ -8,10 +8,13 @@ import Layer from '../../Layer'
export default class Permute extends Layer {
/**
* Creates a Permute layer
* @param {number[]} dims
* @param {number[]} attrs.dims
*/
constructor (dims, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
dims = []
} = attrs
this.dims = dims.map(dim => dim - 1)
}
+5 -2
View File
@@ -10,10 +10,13 @@ import tile from 'ndarray-tile'
export default class RepeatVector extends Layer {
/**
* Creates a RepeatVector layer
* @param {number} n
* @param {number} attrs.n
*/
constructor (n, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
n = 1
} = attrs
this.n = n
}
+5 -2
View File
@@ -11,10 +11,13 @@ import flattenDeep from 'lodash/flattenDeep'
export default class Reshape extends Layer {
/**
* Creates a Reshape layer
* @param {number[]} shape
* @param {number[]} attrs.shape
*/
constructor (shape, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
shape = []
} = attrs
this.shape = shape
}
+3 -1
View File
@@ -9,9 +9,11 @@ export default class Embedding extends Layer {
/**
* Creates a Embedding layer
*/
constructor (inputDim, outputDim, attrs = {}) {
constructor (attrs = {}) {
super(attrs)
const {
inputDim = 1,
outputDim = 1,
inputLength = 0,
maskZero = false,
dropout = 0.0
@@ -24,7 +24,7 @@ describe('advanced activation layers', function () {
it('[advanced_activations.LeakyReLU.0] should produce expected values', function () {
const key = 'advanced_activations.LeakyReLU.0'
console.log(`\n%c[${key}] alpha=0.4`, styles.h3)
let testLayer = new layers.LeakyReLU(0.4)
let testLayer = new layers.LeakyReLU({ alpha: 0.4 })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -79,7 +79,7 @@ describe('advanced activation layers', function () {
it('[advanced_activations.ELU.0] should produce expected values', function () {
const key = 'advanced_activations.ELU.0'
console.log(`\n%c[${key}] alpha=1.1`, styles.h3)
let testLayer = new layers.ELU(1.1)
let testLayer = new layers.ELU({ alpha: 1.1 })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -134,7 +134,7 @@ describe('advanced activation layers', function () {
it('[advanced_activations.ThresholdedReLU.0] should produce expected values', function () {
const key = 'advanced_activations.ThresholdedReLU.0'
console.log(`\n%c[${key}] theta=0.9`, styles.h3)
let testLayer = new layers.ThresholdedReLU(0.9)
let testLayer = new layers.ThresholdedReLU({ theta: 0.9 })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
+2 -2
View File
@@ -57,7 +57,7 @@ describe('convolutional layer: AtrousConvolution2D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.AtrousConvolution2D(nbFilter, nbRow, nbCol, attrs)
let testLayer = new layers.AtrousConvolution2D(Object.assign({ nbFilter, nbRow, nbCol }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -91,7 +91,7 @@ describe('convolutional layer: AtrousConvolution2D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.AtrousConvolution2D(nbFilter, nbRow, nbCol, attrs)
let testLayer = new layers.AtrousConvolution2D(Object.assign({ nbFilter, nbRow, nbCol }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape, { useWeblas: true })
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
+2 -2
View File
@@ -52,7 +52,7 @@ describe('convolutional layer: Convolution1D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.Convolution1D(nbFilter, filterLength, attrs)
let testLayer = new layers.Convolution1D(Object.assign({ nbFilter, filterLength }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -86,7 +86,7 @@ describe('convolutional layer: Convolution1D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.Convolution1D(nbFilter, filterLength, attrs)
let testLayer = new layers.Convolution1D(Object.assign({ nbFilter, filterLength }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape, { useWeblas: true })
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
+2 -2
View File
@@ -67,7 +67,7 @@ describe('convolutional layer: Convolution2D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.Convolution2D(nbFilter, nbRow, nbCol, attrs)
let testLayer = new layers.Convolution2D(Object.assign({ nbFilter, nbRow, nbCol }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -101,7 +101,7 @@ describe('convolutional layer: Convolution2D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.Convolution2D(nbFilter, nbRow, nbCol, attrs)
let testLayer = new layers.Convolution2D(Object.assign({ nbFilter, nbRow, nbCol }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape, { useWeblas: true })
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
+2 -2
View File
@@ -57,7 +57,7 @@ describe('convolutional layer: Convolution3D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.Convolution3D(nbFilter, kernelDim1, kernelDim2, kernelDim3, attrs)
let testLayer = new layers.Convolution3D(Object.assign({ nbFilter, kernelDim1, kernelDim2, kernelDim3 }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -91,7 +91,7 @@ describe('convolutional layer: Convolution3D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.Convolution3D(nbFilter, kernelDim1, kernelDim2, kernelDim3, attrs)
let testLayer = new layers.Convolution3D(Object.assign({ nbFilter, kernelDim1, kernelDim2, kernelDim3 }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape, { useWeblas: true })
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
+2 -2
View File
@@ -69,7 +69,7 @@ describe('convolutional layer: Deconvolution2D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.Deconvolution2D(nbFilter, nbRow, nbCol, outputShape, attrs)
let testLayer = new layers.Deconvolution2D(Object.assign({ nbFilter, nbRow, nbCol, outputShape }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -104,7 +104,7 @@ describe('convolutional layer: Deconvolution2D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.Deconvolution2D(nbFilter, nbRow, nbCol, outputShape, attrs)
let testLayer = new layers.Deconvolution2D(Object.assign({ nbFilter, nbRow, nbCol, outputShape }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape, { useWeblas: true })
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
+2 -2
View File
@@ -67,7 +67,7 @@ describe('convolutional layer: SeparableConvolution2D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.SeparableConvolution2D(nbFilter, nbRow, nbCol, attrs)
let testLayer = new layers.SeparableConvolution2D(Object.assign({ nbFilter, nbRow, nbCol }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -101,7 +101,7 @@ describe('convolutional layer: SeparableConvolution2D', function () {
it(title, function () {
console.log(`\n%c${title}`, styles.h3)
let testLayer = new layers.SeparableConvolution2D(nbFilter, nbRow, nbCol, attrs)
let testLayer = new layers.SeparableConvolution2D(Object.assign({ nbFilter, nbRow, nbCol }, attrs))
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape, { useWeblas: true })
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
+2 -2
View File
@@ -15,7 +15,7 @@ describe('convolutional layer: UpSampling1D', function () {
it(`[convolutional.UpSampling1D.0] length 2 upsampling on 3x5 input`, function () {
const key = `convolutional.UpSampling1D.0`
console.log(`\n%c[${key}] length 2 upsampling on 3x5 input`, styles.h3)
let testLayer = new layers.UpSampling1D(2)
let testLayer = new layers.UpSampling1D({ length: 2 })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -32,7 +32,7 @@ describe('convolutional layer: UpSampling1D', function () {
it(`[convolutional.UpSampling1D.1] length 3 upsampling on 4x4 input`, function () {
const key = `convolutional.UpSampling1D.1`
console.log(`\n%c[${key}] length 3 upsampling on 4x4 input`, styles.h3)
let testLayer = new layers.UpSampling1D(3)
let testLayer = new layers.UpSampling1D({ length: 3 })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
+4 -4
View File
@@ -15,7 +15,7 @@ describe('convolutional layer: UpSampling2D', function () {
it(`[convolutional.UpSampling2D.0] size 2x2 upsampling on 3x3x3 input, dimOrdering=tf`, function () {
const key = `convolutional.UpSampling2D.0`
console.log(`\n%c[${key}] size 2x2 upsampling on 3x3x3 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.UpSampling2D([2, 2], { dimOrdering: 'tf' })
let testLayer = new layers.UpSampling2D({ size: [2, 2], dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -32,7 +32,7 @@ describe('convolutional layer: UpSampling2D', function () {
it(`[convolutional.UpSampling2D.1] size 2x2 upsampling on 3x3x3 input, dimOrdering=th`, function () {
const key = `convolutional.UpSampling2D.1`
console.log(`\n%c[${key}] size 2x2 upsampling on 3x3x3 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.UpSampling2D([2, 2], { dimOrdering: 'th' })
let testLayer = new layers.UpSampling2D({ size: [2, 2], dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -49,7 +49,7 @@ describe('convolutional layer: UpSampling2D', function () {
it(`[convolutional.UpSampling2D.2] size 3x2 upsampling on 4x2x2 input, dimOrdering=tf`, function () {
const key = `convolutional.UpSampling2D.2`
console.log(`\n%c[${key}] size 3x2 upsampling on 4x2x2 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.UpSampling2D([3, 2], { dimOrdering: 'tf' })
let testLayer = new layers.UpSampling2D({ size: [3, 2], dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -66,7 +66,7 @@ describe('convolutional layer: UpSampling2D', function () {
it(`[convolutional.UpSampling2D.3] size 1x3 upsampling on 4x3x2 input, dimOrdering=th`, function () {
const key = `convolutional.UpSampling2D.3`
console.log(`\n%c[${key}] size 1x3 upsampling on 4x3x2 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.UpSampling2D([1, 3], { dimOrdering: 'th' })
let testLayer = new layers.UpSampling2D({ size: [1, 3], dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
+4 -4
View File
@@ -15,7 +15,7 @@ describe('convolutional layer: UpSampling3D', function () {
it(`[convolutional.UpSampling3D.0] size 2x2x2 upsampling on 2x2x2x3 input, dimOrdering=tf`, function () {
const key = `convolutional.UpSampling3D.0`
console.log(`\n%c[${key}] size 2x2x2 upsampling on 2x2x2x3 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.UpSampling3D([2, 2, 2], { dimOrdering: 'tf' })
let testLayer = new layers.UpSampling3D({ size: [2, 2, 2], dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -32,7 +32,7 @@ describe('convolutional layer: UpSampling3D', function () {
it(`[convolutional.UpSampling3D.1] size 2x2x2 upsampling on 2x2x2x3 input, dimOrdering=th`, function () {
const key = `convolutional.UpSampling3D.1`
console.log(`\n%c[${key}] size 2x2x2 upsampling on 2x2x2x3 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.UpSampling3D([2, 2, 2], { dimOrdering: 'th' })
let testLayer = new layers.UpSampling3D({ size: [2, 2, 2], dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -49,7 +49,7 @@ describe('convolutional layer: UpSampling3D', function () {
it(`[convolutional.UpSampling3D.2] size 1x3x2 upsampling on 2x1x3x2 input, dimOrdering=tf`, function () {
const key = `convolutional.UpSampling3D.2`
console.log(`\n%c[${key}] size 1x3x2 upsampling on 2x1x3x2 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.UpSampling3D([1, 3, 2], { dimOrdering: 'tf' })
let testLayer = new layers.UpSampling3D({ size: [1, 3, 2], dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -66,7 +66,7 @@ describe('convolutional layer: UpSampling3D', function () {
it(`[convolutional.UpSampling3D.3] 2x1x2 upsampling on 2x1x3x3 input, dimOrdering=th`, function () {
const key = `convolutional.UpSampling3D.3`
console.log(`\n%c[${key}] 2x1x2 upsampling on 2x1x3x3 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.UpSampling3D([2, 1, 2], { dimOrdering: 'th' })
let testLayer = new layers.UpSampling3D({ size: [2, 1, 2], dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
+2 -2
View File
@@ -15,7 +15,7 @@ describe('convolutional layer: ZeroPadding1D', function () {
it(`[convolutional.ZeroPadding1D.0] padding 1 on 3x5 input`, function () {
const key = `convolutional.ZeroPadding1D.0`
console.log(`\n%c[${key}] padding 1 on 3x5 input`, styles.h3)
let testLayer = new layers.ZeroPadding1D(1)
let testLayer = new layers.ZeroPadding1D({ padding: 1 })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -32,7 +32,7 @@ describe('convolutional layer: ZeroPadding1D', function () {
it(`[convolutional.ZeroPadding1D.1] padding 3 on 4x4 input`, function () {
const key = `convolutional.ZeroPadding1D.1`
console.log(`\n%c[${key}] padding 3 on 4x4 input`, styles.h3)
let testLayer = new layers.ZeroPadding1D(3)
let testLayer = new layers.ZeroPadding1D({ padding: 3 })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
+4 -4
View File
@@ -15,7 +15,7 @@ describe('convolutional layer: ZeroPadding2D', function () {
it(`[convolutional.ZeroPadding2D.0] padding 1,1 on 3x5x2 input, dimOrdering=tf`, function () {
const key = `convolutional.ZeroPadding2D.0`
console.log(`\n%c[${key}] padding 1,1 on 3x5x2 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.ZeroPadding2D([1, 1], { dimOrdering: 'tf' })
let testLayer = new layers.ZeroPadding2D({ padding: [1, 1], dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -32,7 +32,7 @@ describe('convolutional layer: ZeroPadding2D', function () {
it(`[convolutional.ZeroPadding2D.1] padding 1,1 on 3x5x2 input, dimOrdering=th`, function () {
const key = `convolutional.ZeroPadding2D.1`
console.log(`\n%c[${key}] padding 1,1 on 3x5x2 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.ZeroPadding2D([1, 1], { dimOrdering: 'th' })
let testLayer = new layers.ZeroPadding2D({ padding: [1, 1], dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -49,7 +49,7 @@ describe('convolutional layer: ZeroPadding2D', function () {
it(`[convolutional.ZeroPadding2D.2] padding 3,2 on 2x6x4 input, dimOrdering=tf`, function () {
const key = `convolutional.ZeroPadding2D.2`
console.log(`\n%c[${key}] padding 3,2 on 2x6x4 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.ZeroPadding2D([3, 2], { dimOrdering: 'tf' })
let testLayer = new layers.ZeroPadding2D({ padding: [3, 2], dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -66,7 +66,7 @@ describe('convolutional layer: ZeroPadding2D', function () {
it(`[convolutional.ZeroPadding2D.3] padding 3,2 on 2x6x4 input, dimOrdering=th`, function () {
const key = `convolutional.ZeroPadding2D.3`
console.log(`\n%c[${key}] padding 3,2 on 2x6x4 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.ZeroPadding2D([3, 2], { dimOrdering: 'th' })
let testLayer = new layers.ZeroPadding2D({ padding: [3, 2], dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
+4 -4
View File
@@ -15,7 +15,7 @@ describe('convolutional layer: ZeroPadding3D', function () {
it(`[convolutional.ZeroPadding3D.0] padding 1,1,1 on 3x5x2x2 input, dimOrdering=tf`, function () {
const key = `convolutional.ZeroPadding3D.0`
console.log(`\n%c[${key}] padding 1,1,1 on 3x5x2x2 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.ZeroPadding3D([1, 1, 1], { dimOrdering: 'tf' })
let testLayer = new layers.ZeroPadding3D({ padding: [1, 1, 1], dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -32,7 +32,7 @@ describe('convolutional layer: ZeroPadding3D', function () {
it(`[convolutional.ZeroPadding3D.1] padding 1,1,1 on 3x5x2x2 input, dimOrdering=th`, function () {
const key = `convolutional.ZeroPadding3D.1`
console.log(`\n%c[${key}] padding 1,1,1 on 3x5x2x2 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.ZeroPadding3D([1, 1, 1], { dimOrdering: 'th' })
let testLayer = new layers.ZeroPadding3D({ padding: [1, 1, 1], dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -49,7 +49,7 @@ describe('convolutional layer: ZeroPadding3D', function () {
it(`[convolutional.ZeroPadding3D.2] padding 3,2,2 on 3x2x1x4 input, dimOrdering=tf`, function () {
const key = `convolutional.ZeroPadding3D.2`
console.log(`\n%c[${key}] padding 3,2,2 on 3x2x1x4 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.ZeroPadding3D([3, 2, 2], { dimOrdering: 'tf' })
let testLayer = new layers.ZeroPadding3D({ padding: [3, 2, 2], dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -66,7 +66,7 @@ describe('convolutional layer: ZeroPadding3D', function () {
it(`[convolutional.ZeroPadding3D.3] padding 3,2,2 on 3x2x1x4 input, dimOrdering=th`, function () {
const key = `convolutional.ZeroPadding3D.3`
console.log(`\n%c[${key}] padding 3,2,2 on 3x2x1x4 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.ZeroPadding3D([3, 2, 2], { dimOrdering: 'th' })
let testLayer = new layers.ZeroPadding3D({ padding: [3, 2, 2], dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
+4 -4
View File
@@ -15,12 +15,12 @@ describe('core layer: Activation', function () {
it('[core.Activation.0] should produce expected values for tanh activation following Dense layer', function () {
const key = 'core.Activation.0'
console.log(`\n%c[${key}] test 1 (tanh)`, styles.h3)
let testLayer1 = new layers.Dense(2)
let testLayer1 = new layers.Dense({ outputDim: 2 })
testLayer1.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
t = testLayer1.call(t)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
let testLayer2 = new layers.Activation('tanh')
let testLayer2 = new layers.Activation({ activation: 'tanh' })
const startTime = performance.now()
t = testLayer2.call(t)
const endTime = performance.now()
@@ -35,12 +35,12 @@ describe('core layer: Activation', function () {
it('[core.Activation.1] should produce expected values for hardSigmoid activation following Dense layer', function () {
const key = 'core.Activation.1'
console.log(`\n%c[${key}] test 2 (hardSigmoid)`, styles.h3)
let testLayer1 = new layers.Dense(2)
let testLayer1 = new layers.Dense({ outputDim: 2 })
testLayer1.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
t = testLayer1.call(t)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
let testLayer2 = new layers.Activation('hardSigmoid')
let testLayer2 = new layers.Activation({ activation: 'hardSigmoid' })
const startTime = performance.now()
t = testLayer2.call(t)
const endTime = performance.now()
+6 -6
View File
@@ -24,7 +24,7 @@ describe('core layer: Dense', function () {
it('[core.Dense.0] [CPU] should produce expected values', function () {
const key = 'core.Dense.0'
console.log(`\n%c[${key}] [CPU] test 1`, styles.h3)
let testLayer = new layers.Dense(2)
let testLayer = new layers.Dense({ outputDim: 2 })
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -42,7 +42,7 @@ describe('core layer: Dense', function () {
it('[core.Dense.1] [CPU] should produce expected values, with sigmoid activation function', function () {
const key = 'core.Dense.1'
console.log(`\n%c[${key}] [CPU] test 2 (with sigmoid activation)`, styles.h3)
let testLayer = new layers.Dense(2, { activation: 'sigmoid' })
let testLayer = new layers.Dense({ outputDim: 2, activation: 'sigmoid' })
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -60,7 +60,7 @@ describe('core layer: Dense', function () {
it('[core.Dense.2] [CPU] should produce expected values, with softplus activation function and no bias', function () {
const key = 'core.Dense.2'
console.log(`\n%c[${key}] [CPU] test 3 (with softplus activation and no bias)`, styles.h3)
let testLayer = new layers.Dense(2, { activation: 'softplus', bias: false })
let testLayer = new layers.Dense({ outputDim: 2, activation: 'softplus', bias: false })
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -88,7 +88,7 @@ describe('core layer: Dense', function () {
it('[core.Dense.3] [GPU] should produce expected values', function () {
const key = 'core.Dense.3'
console.log(`\n%c[${key}] [GPU] test 1`, styles.h3)
let testLayer = new layers.Dense(2)
let testLayer = new layers.Dense({ outputDim: 2 })
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape, { useWeblas: true })
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -106,7 +106,7 @@ describe('core layer: Dense', function () {
it('[core.Dense.4] [GPU] should produce expected values, with sigmoid activation function', function () {
const key = 'core.Dense.4'
console.log(`\n%c[${key}] [GPU] test 2 (with sigmoid activation)`, styles.h3)
let testLayer = new layers.Dense(2, { activation: 'sigmoid' })
let testLayer = new layers.Dense({ outputDim: 2, activation: 'sigmoid' })
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape, { useWeblas: true })
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -124,7 +124,7 @@ describe('core layer: Dense', function () {
it('[core.Dense.5] [GPU] should produce expected values, with softplus activation function and no bias', function () {
const key = 'core.Dense.5'
console.log(`\n%c[${key}] [GPU] test 3 (with softplus activation and no bias)`, styles.h3)
let testLayer = new layers.Dense(2, { activation: 'softplus', bias: false })
let testLayer = new layers.Dense({ outputDim: 2, activation: 'softplus', bias: false })
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape, { useWeblas: true })
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
+2 -2
View File
@@ -15,12 +15,12 @@ describe('core layer: Dropout', function () {
it('[core.Dropout.0] should just pass through tensor during test time', function () {
const key = 'core.Dropout.0'
console.log(`\n%c[${key}] should pass through`, styles.h3)
let testLayer1 = new layers.Dense(2)
let testLayer1 = new layers.Dense({ outputDim: 2 })
testLayer1.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
t = testLayer1.call(t)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
let testLayer2 = new layers.Dropout(0.5)
let testLayer2 = new layers.Dropout({ p: 0.5 })
const startTime = performance.now()
t = testLayer2.call(t)
const endTime = performance.now()
+2 -2
View File
@@ -15,7 +15,7 @@ describe('core layer: MaxoutDense', function () {
it('[core.MaxoutDense.0] should produce expected values, nbFeature=4, bias=true', function () {
const key = 'core.MaxoutDense.0'
console.log(`\n%c[${key}] nbFeature=4, bias=true`, styles.h3)
let testLayer = new layers.MaxoutDense(3)
let testLayer = new layers.MaxoutDense({ outputDim: 3 })
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
@@ -33,7 +33,7 @@ describe('core layer: MaxoutDense', function () {
it('[core.MaxoutDense.1] should produce expected values, nbFeature=7, bias=false', function () {
const key = 'core.MaxoutDense.1'
console.log(`\n%c[${key}] nbFeature=7, bias=false`, styles.h3)
let testLayer = new layers.MaxoutDense(3, { bias: false })
let testLayer = new layers.MaxoutDense({ outputDim: 3, bias: false })
testLayer.setWeights(TEST_DATA[key].weights.map(w => new KerasJS.Tensor(w.data, w.shape)))
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
+42 -42
View File
@@ -24,8 +24,8 @@ describe('core layer: Merge', function () {
it('[core.Merge.0] should produce expected values in sum mode', function () {
const key = 'core.Merge.0'
console.log(`\n%c[${key}] mode: sum`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer1b = new layers.Dense(2)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2 = new layers.Merge({ mode: 'sum' })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -59,8 +59,8 @@ describe('core layer: Merge', function () {
it('[core.Merge.1] should produce expected values in mul mode', function () {
const key = 'core.Merge.1'
console.log(`\n%c[${key}] mode: mul`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer1b = new layers.Dense(2)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2 = new layers.Merge({ mode: 'mul' })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -94,8 +94,8 @@ describe('core layer: Merge', function () {
it('[core.Merge.2] should produce expected values in ave mode', function () {
const key = 'core.Merge.2'
console.log(`\n%c[${key}] mode: ave`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer1b = new layers.Dense(2)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2 = new layers.Merge({ mode: 'ave' })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -129,8 +129,8 @@ describe('core layer: Merge', function () {
it('[core.Merge.3] should produce expected values in max mode', function () {
const key = 'core.Merge.3'
console.log(`\n%c[${key}] mode: max`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer1b = new layers.Dense(2)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2 = new layers.Merge({ mode: 'max' })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -164,8 +164,8 @@ describe('core layer: Merge', function () {
it('[core.Merge.4] should produce expected values in concat mode (1D)', function () {
const key = 'core.Merge.4'
console.log(`\n%c[${key}] mode: concat (1D)`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer1b = new layers.Dense(2)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2 = new layers.Merge({ mode: 'concat', concatAxis: -1 })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -189,10 +189,10 @@ describe('core layer: Merge', function () {
it('[core.Merge.5] should produce expected values in concat mode (2D, concatAxis=-1)', function () {
const key = 'core.Merge.5'
console.log(`\n%c[${key}] mode: concat (2D, concatAxis=-1)`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer2a = new layers.RepeatVector(3)
let testLayer1b = new layers.Dense(2)
let testLayer2b = new layers.RepeatVector(3)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer2a = new layers.RepeatVector({ n: 3 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2b = new layers.RepeatVector({ n: 3 })
let testLayer3 = new layers.Merge({ mode: 'concat', concatAxis: -1 })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -218,10 +218,10 @@ describe('core layer: Merge', function () {
it('[core.Merge.6] should produce expected values in concat mode (2D, concatAxis=-2)', function () {
const key = 'core.Merge.6'
console.log(`\n%c[${key}] mode: concat (2D, concatAxis=-2)`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer2a = new layers.RepeatVector(3)
let testLayer1b = new layers.Dense(2)
let testLayer2b = new layers.RepeatVector(3)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer2a = new layers.RepeatVector({ n: 3 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2b = new layers.RepeatVector({ n: 3 })
let testLayer3 = new layers.Merge({ mode: 'concat', concatAxis: -2 })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -247,10 +247,10 @@ describe('core layer: Merge', function () {
it('[core.Merge.7] should produce expected values in concat mode (2D, concatAxis=1)', function () {
const key = 'core.Merge.7'
console.log(`\n%c[${key}] mode: concat (2D, concatAxis=1)`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer2a = new layers.RepeatVector(3)
let testLayer1b = new layers.Dense(2)
let testLayer2b = new layers.RepeatVector(3)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer2a = new layers.RepeatVector({ n: 3 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2b = new layers.RepeatVector({ n: 3 })
let testLayer3 = new layers.Merge({ mode: 'concat', concatAxis: 1 })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -276,10 +276,10 @@ describe('core layer: Merge', function () {
it('[core.Merge.8] should produce expected values in concat mode (2D, concatAxis=2)', function () {
const key = 'core.Merge.8'
console.log(`\n%c[${key}] mode: concat (2D, concatAxis=2)`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer2a = new layers.RepeatVector(3)
let testLayer1b = new layers.Dense(2)
let testLayer2b = new layers.RepeatVector(3)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer2a = new layers.RepeatVector({ n: 3 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2b = new layers.RepeatVector({ n: 3 })
let testLayer3 = new layers.Merge({ mode: 'concat', concatAxis: 2 })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -315,10 +315,10 @@ describe('core layer: Merge', function () {
it('[core.Merge.9] should produce expected values in dot mode (2D x 2D, dotAxes=1)', function () {
const key = 'core.Merge.9'
console.log(`\n%c[${key}] mode: dot (2D x 2D, dotAxes=1)`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer2a = new layers.RepeatVector(3)
let testLayer1b = new layers.Dense(2)
let testLayer2b = new layers.RepeatVector(3)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer2a = new layers.RepeatVector({ n: 3 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2b = new layers.RepeatVector({ n: 3 })
let testLayer3 = new layers.Merge({ mode: 'dot', dotAxes: 1 })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -344,10 +344,10 @@ describe('core layer: Merge', function () {
it('[core.Merge.10] should produce expected values in dot mode (2D x 2D, dotAxes=2)', function () {
const key = 'core.Merge.10'
console.log(`\n%c[${key}] mode: dot (2D x 2D, dotAxes=2)`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer2a = new layers.RepeatVector(3)
let testLayer1b = new layers.Dense(2)
let testLayer2b = new layers.RepeatVector(3)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer2a = new layers.RepeatVector({ n: 3 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2b = new layers.RepeatVector({ n: 3 })
let testLayer3 = new layers.Merge({ mode: 'dot', dotAxes: 2 })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -383,10 +383,10 @@ describe('core layer: Merge', function () {
it('[core.Merge.11] should produce expected values in cos mode (2D x 2D, dotAxes=1)', function () {
const key = 'core.Merge.11'
console.log(`\n%c[${key}] mode: cos (2D x 2D, dotAxes=1)`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer2a = new layers.RepeatVector(3)
let testLayer1b = new layers.Dense(2)
let testLayer2b = new layers.RepeatVector(3)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer2a = new layers.RepeatVector({ n: 3 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2b = new layers.RepeatVector({ n: 3 })
let testLayer3 = new layers.Merge({ mode: 'cos', dotAxes: 1 })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
@@ -412,10 +412,10 @@ describe('core layer: Merge', function () {
it('[core.Merge.12] should produce expected values in cos mode (2D x 2D, dotAxes=2)', function () {
const key = 'core.Merge.12'
console.log(`\n%c[${key}] mode: cos (2D x 2D, dotAxes=2)`, styles.h3)
let testLayer1a = new layers.Dense(2)
let testLayer2a = new layers.RepeatVector(3)
let testLayer1b = new layers.Dense(2)
let testLayer2b = new layers.RepeatVector(3)
let testLayer1a = new layers.Dense({ outputDim: 2 })
let testLayer2a = new layers.RepeatVector({ n: 3 })
let testLayer1b = new layers.Dense({ outputDim: 2 })
let testLayer2b = new layers.RepeatVector({ n: 3 })
let testLayer3 = new layers.Merge({ mode: 'cos', dotAxes: 2 })
testLayer1a.setWeights(TEST_DATA[key].weights.slice(0, 2).map(w => new KerasJS.Tensor(w.data, w.shape)))
testLayer1b.setWeights(TEST_DATA[key].weights.slice(2, 4).map(w => new KerasJS.Tensor(w.data, w.shape)))
+2 -2
View File
@@ -15,7 +15,7 @@ describe('core layer: Permute', function () {
it('[core.Permute.0] should be able to go from shape [3, 2] -> [2, 3]', function () {
const key = 'core.Permute.0'
console.log(`\n%c[${key}] shape [3, 2] -> [2, 3]`, styles.h3)
let testLayer = new layers.Permute([2, 1])
let testLayer = new layers.Permute({ dims: [2, 1] })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -32,7 +32,7 @@ describe('core layer: Permute', function () {
it('[core.Permute.1] should be able to go from shape [2, 3, 4] -> [4, 3, 2]', function () {
const key = 'core.Permute.1'
console.log(`\n%c[${key}] shape [2, 3, 4] -> [4, 3, 2]`, styles.h3)
let testLayer = new layers.Permute([3, 2, 1])
let testLayer = new layers.Permute({ dims: [3, 2, 1] })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
+1 -1
View File
@@ -15,7 +15,7 @@ describe('core layer: RepeatVector', function () {
it('[core.RepeatVector.0] should be able to go from shape [6] -> [7, 6]', function () {
const key = 'core.RepeatVector.0'
console.log(`\n%c[${key}] repeat vector, shape [6] -> [7, 6]`, styles.h3)
let testLayer = new layers.RepeatVector(7)
let testLayer = new layers.RepeatVector({ n: 7 })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
+3 -3
View File
@@ -15,7 +15,7 @@ describe('core layer: Reshape', function () {
it('[core.Reshape.0] should be able to go from shape [6] -> [2, 3]', function () {
const key = 'core.Reshape.0'
console.log(`\n%c[${key}] shape [6] -> [2, 3]`, styles.h3)
let testLayer = new layers.Reshape([2, 3])
let testLayer = new layers.Reshape({ shape: [2, 3] })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -32,7 +32,7 @@ describe('core layer: Reshape', function () {
it('[core.Reshape.1] should be able to go from shape [3, 2] -> [6]', function () {
const key = 'core.Reshape.1'
console.log(`\n%c[${key}] shape [3, 2] -> [6]`, styles.h3)
let testLayer = new layers.Reshape([6])
let testLayer = new layers.Reshape({ shape: [6] })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
@@ -49,7 +49,7 @@ describe('core layer: Reshape', function () {
it('[core.Reshape.2] should be able to go from shape [3, 2, 2] -> [4, 3]', function () {
const key = 'core.Reshape.2'
console.log(`\n%c[${key}] shape [3, 2, 2] -> [4, 3]`, styles.h3)
let testLayer = new layers.Reshape([4, 3])
let testLayer = new layers.Reshape({ shape: [4, 3] })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()