* Adds a weight variable to the layer. * * @param name Name of the new weight variable. * @param shape The shape of the weight. * @param dtype The dtype of the weight. * @param initializer An initializer instance. * @param regularizer A regularizer instance. * @param trainable W
(
name: string, shape: Shape, dtype?: DataType, initializer?: Initializer,
regularizer?: Regularizer, trainable?: boolean, constraint?: Constraint,
getInitializerFunc?: Function)
| 1267 | * @doc {heading: 'Models', 'subheading': 'Classes'} |
| 1268 | */ |
| 1269 | protected addWeight( |
| 1270 | name: string, shape: Shape, dtype?: DataType, initializer?: Initializer, |
| 1271 | regularizer?: Regularizer, trainable?: boolean, constraint?: Constraint, |
| 1272 | getInitializerFunc?: Function): LayerVariable { |
| 1273 | // Reject duplicate weight names. |
| 1274 | if (this._addedWeightNames.indexOf(name) !== -1) { |
| 1275 | throw new ValueError( |
| 1276 | `Duplicate weight name ${name} for layer ${this.name}`); |
| 1277 | } |
| 1278 | this._addedWeightNames.push(name); |
| 1279 | |
| 1280 | if (dtype == null) { |
| 1281 | dtype = 'float32'; |
| 1282 | } |
| 1283 | |
| 1284 | if (this.fastWeightInitDuringBuild) { |
| 1285 | initializer = getInitializerFunc != null ? getInitializerFunc() : |
| 1286 | getInitializer('zeros'); |
| 1287 | } |
| 1288 | const initValue = initializer.apply(shape, dtype); |
| 1289 | const weight = |
| 1290 | new LayerVariable(initValue, dtype, name, trainable, constraint); |
| 1291 | initValue.dispose(); |
| 1292 | // Request backend not to dispose the weights of the model on scope() exit. |
| 1293 | if (regularizer != null) { |
| 1294 | this.addLoss(() => regularizer.apply(weight.read())); |
| 1295 | } |
| 1296 | if (trainable == null) { |
| 1297 | trainable = true; |
| 1298 | } |
| 1299 | if (trainable) { |
| 1300 | this._trainableWeights.push(weight); |
| 1301 | } else { |
| 1302 | this._nonTrainableWeights.push(weight); |
| 1303 | } |
| 1304 | return weight; |
| 1305 | } |
| 1306 | |
| 1307 | /** |
| 1308 | * Set the fast-weight-initialization flag. |
nothing calls this directly
no test coverage detected
searching dependent graphs…