Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/layers/core/Lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Layer from '../../Layer'

/**
* Lambda layer class
* This layer requires you to re-implement lambda nodes in javascript,
* as we do not have a python runtime available.
*/
export default class Lambda extends Layer {

/**
* Creates a Lambda layer
*
* @param {Object} [attrs] - layer config attributes
*/
constructor(attrs = {}) {
super(attrs);
this.layerClass = 'Lambda';

if(this.functions[attrs.name]) {
this._call = this.functions[attrs.name].bind(this);
} else {
console.log("Missing lambda, using No_op! Implement it by defining require(\"keras-js/layers/core/Lambda\").functions["+attrs.name+"]");
this._call = x => ({output: x, inputShape: x.tensor.shape});
}

if(this.initializers[attrs.name]) {
this.initializers[attrs.name].bind(this)();
} else {
console.log("No lambda initializer. Disable this warning by defining require(\"keras-js/layers/core/Lambda\").initializers["+attrs.name+"]");
}
}

/**
* Method for layer computational logic
*
* @param {Tensor} x
* @returns {Tensor}
*/
call(x) {
return Object.assign(this, this._call(x)).output;
}

}
Lambda.prototype.functions = {};
Lambda.prototype.initializers = {}

exports.default = Lambda;
2 changes: 2 additions & 0 deletions src/layers/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SpatialDropout1D from './SpatialDropout1D'
import SpatialDropout2D from './SpatialDropout2D'
import SpatialDropout3D from './SpatialDropout3D'
import Flatten from './Flatten'
import Lambda from './Lambda'
import Reshape from './Reshape'
import Permute from './Permute'
import RepeatVector from './RepeatVector'
Expand All @@ -17,6 +18,7 @@ export {
SpatialDropout2D,
SpatialDropout3D,
Flatten,
Lambda,
Reshape,
Permute,
RepeatVector
Expand Down