TensorFlowJs |如何在两个经过训练的模型之间获得模型增量

时间:2019-07-17 16:19:14

标签: javascript tensorflow tensorflow.js

我有(2)个相同的模型:

  • Model1 =在基础数据集上训练
  • Model2 =在基础数据集+附加数据集中进行训练

如何使用TensorFlowJs在Model1和Model2之间获得增量?

const Model1 = tf.sequential();
Model1.add(tf.layers.dense({units: 50, inputShape: [1], activation: 'relu', kernel_regularizer: 'l2'}));
Model1.add(tf.layers.dense({units: 1, inputShape: [1], activation: 'sigmoid'}));
Model1.compile({loss: 'binaryCrossentropy', optimizer: 'rmsprop', metrics: 'accuracy'}); 

const Model2 = tf.sequential();
Model2.add(tf.layers.dense({units: 50, inputShape: [1], activation: 'relu', kernel_regularizer: 'l2'}));
Model2.add(tf.layers.dense({units: 1, inputShape: [1], activation: 'sigmoid'}));
Model2.compile({loss: 'binaryCrossentropy', optimizer: 'rmsprop', metrics: 'accuracy'}); 

# train/fit Model1 with base dataset
# train/fit Model2 with base dataset + additional dataset
# get difference between Model1 and Model2 with TensorFlowJs here

我希望得到Model1和Model2之间的权重差

1 个答案:

答案 0 :(得分:1)

要获得模型的权重,可以对每一层使用getWeights。答案here描述了如何做到这一点。

检索两个模型的权重后,可以使用sub运算符找到两者之间的差异。

相关问题