Tfjs: Manually setting weights for model

Created on 23 Jun 2018  路  9Comments  路  Source: tensorflow/tfjs

TensorFlow.js version

0.11.6

Browser version

Describe the problem or feature request

I'm trying to manually set weights for a model(genetic algorithm) but cannot find the required functionality, I have tried model.setWeights() but it doesn't work for me. I don't want to save the entire model and load those weights each time as that would be computationally expensive.

Error

Uncaught TypeError: Cannot read property 'length' of undefined
at [email protected]:1
at e.tidy ([email protected]:1)
at t.setWeights ([email protected]:1)
at t.setWeights ([email protected]:1)

Any help towards using model.setWeights() or an alternative function will be appreciated! Thanks!

layers

Most helpful comment

All 9 comments

@ArthDh

You can access a model's layer by using model.layers. You can set a layer's weights with layer.setWeights(). Therefore you can use code like the following to set the weights of a single layer:
model.layers[2].setWeights(...). You still can't set individual weights. But at least this helps you narrow down to a smaller set of weights. Does this help with your use case?

@caisq
Thanks a ton! That worked!

It appears that Model now has both instance methods setWeights and getWeights.

Is this the equivalent of getting each layer's weights individually and setting them individually?

Yes, for model.setWeights() weights Should be a list of Tensors with shapes and types matching that of model.getWeights().

Ah ok thanks for your help. Perhaps this should be added to the documentation

I'm getting the error Input Tensor ID not referenced when running predict on a model that has had its weights set.

const tf = require("@tensorflow/tfjs");
require("@tensorflow/tfjs-node");

const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [1] }));

// Prepare the model for training: Specify the loss and the optimizer.
model.compile({ loss: "meanSquaredError", optimizer: "sgd" });

// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

// Train the model using the data.
model.fit(xs, ys, { epochs: 10 }).then(() => {
  // Use the model to do inference on a data point the model hasn't seen before:
  // Open the browser devtools to see the output
  //   model.setWeights(model.getWeights());
  for (let i = 0; i < model.layers.length; i++) {
    model.layers[i].setWeights(model.layers[i].getWeights());
  }
  model.predict(tf.tensor2d([5], [1, 1])).print();
});
Error: Input Tensor ID not referenced
    at NodeJSKernelBackend.executeSingleOutput (/Users/kyle/git-projects/hashback/tfjs-playground/node_modules/@tensorflow/tfjs-node/dist/nodejs_kernel_backend.js:96:43)
    at NodeJSKernelBackend.matMul (/Users/kyle/git-projects/hashback/tfjs-playground/node_modules/@tensorflow/tfjs-node/dist/nodejs_kernel_backend.js:150:21)
    at environment_1.ENV.engine.runKernel.$a (/Users/kyle/git-projects/hashback/tfjs-playground/node_modules/@tensorflow/tfjs-core/dist/ops/matmul.js:46:83)
    at /Users/kyle/git-projects/hashback/tfjs-playground/node_modules/@tensorflow/tfjs-core/dist/engine.js:115:26
    at Engine.scopedRun (/Users/kyle/git-projects/hashback/tfjs-playground/node_modules/@tensorflow/tfjs-core/dist/engine.js:95:23)
    at Engine.runKernel (/Users/kyle/git-projects/hashback/tfjs-playground/node_modules/@tensorflow/tfjs-core/dist/engine.js:113:14)
    at matMul_ (/Users/kyle/git-projects/hashback/tfjs-playground/node_modules/@tensorflow/tfjs-core/dist/ops/matmul.js:46:37)
    at Object.matMul (/Users/kyle/git-projects/hashback/tfjs-playground/node_modules/@tensorflow/tfjs-core/dist/ops/operation.js:23:29)
    at Object.dot (/Users/kyle/git-projects/hashback/tfjs-playground/node_modules/@tensorflow/tfjs-layers/dist/backend/tfjs_backend.js:215:24)
    at /Users/kyle/git-projects/hashback/tfjs-playground/node_modules/@tensorflow/tfjs-layers/dist/layers/core.js:142:28

Same error is thrown when using

model.setWeights(model.getWeights())

before calling predict

Verified that this is reproducible in web:
tfjs: "0.12.4"
tfjs-converter: "0.5.5"
tfjs-core: "0.12.8"
tfjs-layers: "0.7.2"

This only happens when setting the weights after fitting a model. Setting the weights before fitting the model works fine.

Was this page helpful?
0 / 5 - 0 ratings