/
Activation1d.js
201 lines (129 loc) · 4.6 KB
/
Activation1d.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
193
194
195
196
197
198
199
200
201
/**
* @author syt123450 / https://github.com/syt123450
*/
import { NativeLayer1d } from "../abstract/NativeLayer1d";
/**
* Applies an activation function to an 1D output.
*
* @param config, user's configuration for Activation1d layer
* @constructor
*/
function Activation1d( config ) {
// "Activation1d" inherits from abstract layer "NativeLayer1d".
NativeLayer1d.call( this, config );
/**
* Name of the activation function to use.
*
* @type { string }
*/
this.activation = undefined;
// Load user's Activation1d configuration.
this.loadLayerConfig( config );
this.layerType = "Activation1d";
}
Activation1d.prototype = Object.assign( Object.create( NativeLayer1d.prototype ), {
/**
* ============
*
* Functions below override base class NativeLayer1d's abstract method
*
* Activation1d overrides NativeLayer1d's function:
* assemble, loadModelConfig, getRelativeElements
*
* ============
*/
/**
* assemble() configure layer's index in model, calculate the shape and parameters based on previous layer.
*
* @param { int } layerIndex, this layer's order in model
*/
assemble: function( layerIndex, layerLevel ) {
this.layerIndex = layerIndex;
this.layerLevel = layerLevel;
this.inputShape = this.lastLayer.outputShape;
this.width = this.inputShape[ 0 ];
if ( this.paging ) {
this.totalSegments = Math.ceil( this.width / this.segmentLength );
}
// Activation1d layer's outputShape has one dimension, that's why Activation1d layer inherits from abstract layer "NativeLayer1d".
this.outputShape = [ this.width ];
// 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 Activation1d 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.activation1d;
}
},
/**
* 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 Activation1d.
* The configuration load in this function sometimes has not been loaded in loadBasicLayerConfig.
*
* @param { JSON } layerConfig, user's configuration for Activation1d.
*/
loadLayerConfig: function( layerConfig ) {
if ( layerConfig !== undefined ) {
// "activation" configuration is required.
if ( layerConfig.activation !== undefined ) {
this.activation = layerConfig.activation;
} else {
console.error( "\"activation\" property is required for activation1d layer." );
}
} else {
console.error( "Lack config for layer activation1d." );
}
}
} );
export { Activation1d };