Python 可视化tf.contrib.learn.LinearClassifier权重
我用著名的泰坦尼克号数据集处理了tensorflow的线性分类器数据 (我的问题本身在底部-这是模型本身的一些代码) 所以我有我的专题专栏:Python 可视化tf.contrib.learn.LinearClassifier权重,python,tensorflow,Python,Tensorflow,我用著名的泰坦尼克号数据集处理了tensorflow的线性分类器数据 (我的问题本身在底部-这是模型本身的一些代码) 所以我有我的专题专栏: CONTINUOUS_COLS = ['Age', 'Fare'] CATEGORICAL_COLS = ['Sex', 'Pclass', 'Title'] LABELS_COL = 'Survived' sex_col = sparse_column_with_keys('Sex', keys=['male', 'female']) title_co
CONTINUOUS_COLS = ['Age', 'Fare']
CATEGORICAL_COLS = ['Sex', 'Pclass', 'Title']
LABELS_COL = 'Survived'
sex_col = sparse_column_with_keys('Sex', keys=['male', 'female'])
title_col = sparse_column_with_hash_bucket('Title', 10)
fare_class_col = sparse_column_with_keys('Pclass', keys=['1','2','3'])
age_col = real_valued_column('Age')
fare_col = real_valued_column('Fare')
我的输入功能:
def create_input_fn(df):
continous_features = {k: tf.constant(df[k].values) for k in CONTINUOUS_COLS}
categorical_features = {k : tf.SparseTensor(
indices=[[0,i] for i in range(df[k].size)],
values=df[k].values,
dense_shape=[df[k].size, 1]
) for k in CATEGORICAL_COLS}
feature_cols = {**continous_features, **categorical_features}
labels = tf.constant(df[LABELS_COL].values)
return feature_cols, labels
我的模型是:
clf = LinearClassifier(feature_columns=[sex_col, fare_class_col, age_col, fare_col, title_col],
optimizer=tf.train.FtrlOptimizer(
learning_rate=0.5,
l1_regularization_strength=1.0,
l2_regularization_strength=1.0),
model_dir=tempfile.TemporaryDirectory().name)
现在,当我运行模型时,它是OK的,我想看看模型的权重,以便更好地可视化它们
因此,clf.weights\uu
是存在的(尽管它被列为不推荐使用的),所以我只是手动将它们取出:
for var in clf.get_variable_names():
if var.endswith('weights'):
print(f'{var} -> {clf.get_variable_value(var)}')
我得到了一些不错的结果:
linear/Pclass/weights -> [[ 0. ]
[ 0. ]
[-0.01772301]]
linear/Sex/weights -> [[-0.07285357]
[ 0. ]]
linear/Title/weights -> [[ 0. ]
[ 0. ]
[ 0. ]
[-0.03760524]
[ 0. ]
[ 0. ]
[ 0. ]
[ 0. ]
[ 0. ]
[ 0. ]]
现在我的问题是-如何拔出最初使用的键?
因此,我可以更好地匹配数字,例如与性别匹配-键最初映射为男性/女性
谢谢 对于带有键的
稀疏列:
sex_col.lookup_config.keys#('male','female')
比如:
matched = {}
weights = clf.get_variable_value('linear/Sex/weights') # np array
for index, key in enumerate(sex_col.lookup_config.keys):
matched[key] = weights[index]
当您dir(sex\u col.lookup\u config)
并在以下位置检查方法docstrings时,还有一些其他有趣的属性:
我还没有从sparse\u column\u和\u hash\u bucket
中找到映射
如果您有tf.contrib.layers.bucketized\u列
与教程中的年龄桶类似:
age\u bucket.边界