Skip to content

Commit

Permalink
concatenate2d (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
syt123450 committed Nov 25, 2018
1 parent 4faf17e commit 6a7f24c
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/merge/Concatenate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @author syt123450 / https://github.com/syt123450
*/

import { MergedLayer2d } from "../layer/abstract/MergedLayer2d";
import { MergedLayer3d } from "../layer/abstract/MergedLayer3d";

function Concatenate( layerList, config ) {
Expand Down Expand Up @@ -44,6 +45,14 @@ function Concatenate( layerList, config ) {

} else if ( layerList[ 0 ].layerDimension === 2 ) {

return new MergedLayer2d( {

operator: operatorType,
mergedElements: layerList,
userConfig: userConfig

} );

} else if ( layerList[ 0 ].layerDimension === 3 ) {

return new MergedLayer3d( {
Expand Down
3 changes: 2 additions & 1 deletion src/merge/factory/StrategyFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Subtract2d } from "../strategy/Subtract2d";
import { Maximum2d } from "../strategy/Maximum2d";
import { Average2d } from "../strategy/Average2d";
import { Multiply2d } from "../strategy/Multiply2d";
import { Concatenate2d } from "../strategy/Concatenate2d";

let StrategyFactory = ( function() {

Expand Down Expand Up @@ -59,7 +60,7 @@ let StrategyFactory = ( function() {

} else if ( operator === "concatenate" ) {


return new Concatenate2d( mergedElements );

} else if ( operator === "subtract" ) {

Expand Down
174 changes: 174 additions & 0 deletions src/merge/strategy/Concatenate2d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { Strategy2d } from "../abstract/Strategy2d";

function Concatenate2d( mergedElements ) {

Strategy2d.call( this, mergedElements );

this.strategyType = "Concatenate2d";

}

Concatenate2d.prototype = Object.assign( Object.create( Strategy2d.prototype ), {

validate: function() {

let inputShape = this.mergedElements[ 0 ].outputShape;

for ( let i = 0; i < this.mergedElements.length; i ++ ) {

let layerShape = this.mergedElements[ i ].outputShape;

if ( layerShape[ 0 ] !== inputShape[ 0 ] ) {

return false;

}

}

return true;

},

getOutputShape: function() {

let width = this.mergedElements[ 0 ].outputShape[ 0 ];
let depth = 0;

for (let i = 0; i < this.mergedElements.length; i ++) {

depth += this.mergedElements[ i ].outputShape[ 1 ];

}

return [ width, depth ];

},

getRelativeElements: function( selectedElement ) {

let curveElements = [];
let straightElements = [];

if ( selectedElement.elementType === "aggregationElement" ) {

let request = {

all: true

};

for ( let i = 0; i < this.mergedElements.length; i++ ) {

let relativeResult = this.mergedElements[ i ].provideRelativeElements( request );
let relativeElements = relativeResult.elementList;

if ( this.mergedElements[ i ].layerIndex === this.layerIndex - 1 ) {

for ( let j = 0; j < relativeElements.length; j ++ ) {

straightElements.push( relativeElements[ j ] );

}

} else {

if ( relativeResult.isOpen ) {

for ( let j = 0; j < relativeElements.length; j ++ ) {

straightElements.push( relativeElements[ j ] );

}

} else {

for ( let j = 0; j < relativeElements.length; j ++ ) {

curveElements.push( relativeElements[ j ] );

}

}

}

}

} else if ( selectedElement.elementType === "gridLine" ) {

let gridIndex = selectedElement.gridIndex;

let relativeLayer;

for ( let i = 0; i < this.mergedElements.length; i ++ ) {

let layerDepth = this.mergedElements[ i ].outputShape[ 1 ];

if ( layerDepth > gridIndex ) {

relativeLayer = this.mergedElements[ i ];
break;

} else {

gridIndex -= layerDepth;

}

}

let request = {

index: gridIndex

};

let relativeResult = relativeLayer.provideRelativeElements( request );
let relativeElements = relativeResult.elementList;

if ( relativeLayer.layerIndex === this.layerIndex - 1 ) {

for ( let i = 0; i < relativeElements.length; i ++ ) {

straightElements.push( relativeElements[ i ] );

}

} else {

if ( relativeResult.isOpen ) {

for ( let i = 0; i < relativeElements.length; i ++ ) {

straightElements.push( relativeElements[ i ] );

}

} else {

for ( let i = 0; i < relativeElements.length; i ++ ) {

curveElements.push( relativeElements[ i ] );

}

}

}


}

return {

straight: straightElements,
curve: curveElements

};

}

} );

export { Concatenate2d };
2 changes: 1 addition & 1 deletion test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

model.add( conv1d2 );

let addLayer = TSP.layers.Multiply( [ conv1d1, conv1d2 ] );
let addLayer = TSP.layers.Concatenate( [ conv1d1, conv1d2 ] );

model.add( addLayer );

Expand Down

0 comments on commit 6a7f24c

Please sign in to comment.