I have this sequential model:
this.model = tf.sequential()
this.model.add(tf.layers.dense({units : 16, useBias : true, inputDim : 7})) // input
this.model.add(tf.layers.dense({units : 16, useBias : true, activation: 'sigmoid'})) // hidden
this.model.add(tf.layers.dense({units : 3, useBias : true, activation: 'sigmoid'})) // hidden 2
I checked API for tensorflow.js, but there's nothing about getting weights(kernels) of neural network. So, how can I get weights and then change them, to apply new weights?(for unsupervised learning)
It seems like there is probably a simpler and cleaner way to do what you want, but regardless:
Calling this.model.getWeights()
will give you an array of Variables that correspond to layer weights and biases. Calling data()
on any of these array elements will return a promise that you can resolve to get the weights.
I haven't tried manually setting the weights, but there is a this.model.setWeights()
method.
Goodluck.
Here is a simple way to print off all the weights:
for (let i = 0; i < model.getWeights().length; i++) {
console.log(model.getWeights()[i].dataSync());
}
来源:https://stackoverflow.com/questions/50091466/getting-weights-from-tensorflow-js-neural-network