0.11.6
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.
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!
@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.
This should be fixed with tfjs-layers commit #285
https://github.com/tensorflow/tfjs-layers/commit/934134f677f4464ab9287004a2f6b4b4edb9a039
Most helpful comment
This should be fixed with tfjs-layers commit #285
https://github.com/tensorflow/tfjs-layers/commit/934134f677f4464ab9287004a2f6b4b4edb9a039