MCPcopy
hub / github.com/tensorflow/tfjs / addWeight

Function addWeight

tfjs-layers/src/engine/topology.ts:1269–1305  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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.

Callers

nothing calls this directly

Calls 5

readMethod · 0.95
getInitializerFunction · 0.90
pushMethod · 0.45
applyMethod · 0.45
disposeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…