Skip to main content
 首页 » 编程设计

python之如何检查 Tensorflow LinearClassifier 的特征权重

2024年08月19日3052php

我正在尝试理解 Large-scale Linear Models with TensorFlow文档。文档按如下方式激励这些模型:

Linear model can be interpreted and debugged more easily than neural nets. You can examine the weights assigned to each feature to figure out what's having the biggest impact on a prediction.

所以我运行了随附的 TensorFlow Linear Model Tutorial 中的扩展代码示例.特别是,我运行了 example code from GitHub将 model-type 标志设置为 wide。这正确运行并产生了 accuracy: 0.833733,类似于 Tensorflow 网页上的 accuracy: 0.83557522。

该示例使用 tf.estimator.LinearClassifier 来训练权重。然而,与引用的能够检查权重的动机相反,我在 LinearClassifier documentation 中找不到任何函数来实际提取经过训练的权重。 .

问题:如何访问tf.estimator.LinearClassifier 中各种特征列的训练权重?我希望能够提取 NumPy 数组中的所有权重。

注意:我来自 R 环境,其中线性回归/分类模型有一个 coefs 方法来提取学习的权重。我希望能够在相同数据集上比较 R 和 TensorFlow 中的线性模型。

请您参考如下方法:

使用 Estimator 训练模型后,您可以使用 tf.train.load_variable从检查点检索权重。您可以使用 tf.train.list_variables查找模型权重的名称。

也有计划直接在 Estimator 中添加此支持。