/
Dense.js
192 lines (122 loc) · 4.29 KB
/
Dense.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* @author syt123450 / https://github.com/syt123450
*/
import { NativeLayer1d } from "../abstract/NativeLayer1d";
/**
* A dense (fully connected) layer.
*
* @param config, user's configuration for Dense layer
* @constructor
*/
function Dense( config ) {
// "Dense" inherits from abstract layer "NativeLayer1d".
NativeLayer1d.call( this, config );
this.layerType = "Dense";
}
Dense.prototype = Object.assign( Object.create( NativeLayer1d.prototype ), {
/**
* ============
*
* Functions below override base class NativeLayer1d's abstract method
*
* Dense overrides NativeLayer1d's function:
* assemble, loadModelConfig, getRelativeElements
*
* ============
*/
/**
* assemble() calculate the shape and parameters based on previous layer or pre-defined shape.
*/
assemble: function() {
// Load user's Dense configuration.
this.loadLayerConfig( this.config );
// Unit length is the same as last layer, use unit length to calculate actualWidth which is used to create three.js object.
this.unitLength = this.lastLayer.unitLength;
this.actualWidth = this.width * this.unitLength;
// Calculate aggregation actual size.
if ( this.lastLayer.layerDimension === 1 ) {
if ( this.lastLayer.layerType === "Input1d" ) {
this.aggregationWidth = 3 * this.unitLength;
this.aggregationHeight = 3 * this.unitLength;
} else {
this.aggregationWidth = this.lastLayer.aggregationWidth;
this.aggregationHeight = this.lastLayer.aggregationHeight;
}
} else {
this.aggregationWidth = this.lastLayer.actualWidth;
this.aggregationHeight = this.lastLayer.actualHeight;
}
},
/**
* loadModelConfig() load model's configuration into Dense object,
* If one specific attribute has been set before, model's configuration will not be loaded into it.
*
* Based on the passed in modelConfig parameter
*
* @param { JSON } modelConfig, default and user's configuration for model
*/
loadModelConfig: function( modelConfig ) {
// Call super class "Layer"'s method to load common model configuration, check out "Layer.js" file for more information.
this.loadBasicModelConfig( modelConfig );
if ( this.color === undefined ) {
this.color = modelConfig.color.dense;
}
},
/**
* getRelativeElements() get relative element in last layer for relative lines based on given hovered element.
*
* Use bridge design patten:
* 1. "getRelativeElements" send request to previous layer for relative elements;
* 2. Previous layer's "provideRelativeElements" receives request, return relative elements.
*
* @param { THREE.Object } selectedElement, hovered element detected by THREE's Raycaster.
* @return { THREE.Object[] } relativeElements
*/
getRelativeElements: function( selectedElement ) {
let relativeElements = [];
if ( selectedElement.elementType === "aggregationElement" || selectedElement.elementType === "featureLine" ) {
// "all" means get all "displayed" elements from last layer.
let request = {
all: true
};
relativeElements = this.lastLayer.provideRelativeElements( request ).elementList;
}
return relativeElements;
},
/**
* ============
*
* Functions above override base class NativeLayer1d's abstract method.
*
* ============
*/
/**
* loadLayerConfig() Load user's configuration into Dense.
* The configuration load in this function sometimes has not been loaded in loadBasicLayerConfig.
*
* @param { JSON } layerConfig, user's configuration for Dense.
*/
loadLayerConfig: function( layerConfig ) {
if ( layerConfig !== undefined ) {
if ( layerConfig.shape !== undefined ) {
// Load user's predefined shape.
this.isShapePredefined = true;
this.width = layerConfig.shape[ 0 ];
this.outputShape = [ this.width ];
} else {
// "units" configuration is required.
if ( layerConfig.units !== undefined ) {
this.width = layerConfig.units;
// Dense layer's outputShape has one dimension, that's why Dense layer inherits from abstract layer "NativeLayer1d".
this.outputShape = [ layerConfig.units ];
if ( this.paging ) {
this.totalSegments = Math.ceil( this.width / this.segmentLength );
}
} else {
console.error( "The \"unit\" property is required for dense layer." );
}
}
}
}
} );
export { Dense };