Find index of string_to_hash_bucket in linear weight index Tensorflow
<p>I am working with tensor flow on a binary classification model using a the LinearClassifier class. One of the features I'm basing the classification on is a column called hat:</p>

<pre><code>hat = tf.contrib.layers.sparse_column_with_hash_bucket("hat", hash_bucket_size=1000)
</code></pre>

<p>After I have initialized the model and done a fit with in a tf.Session():</p>

<pre><code>with tf.Session() as sess:    
    m = tf.contrib.learn.LinearClassifier(feature_columns=hat...)
    m.fit(...)
</code></pre>

<p>I would like to inspect the weights for each of the categories of hat after I have trained the model. </p>

<p>The hat tags are just given by different strings. After training the model I would like to find the weight associated with each hat label. However to compare the weights with the particular hat I need to know which hash bucket the hat label has been thrown into. One of my hat labels is "tb". I can find what this is indexed to using the function:</p>

<pre><code>tf.string_to_hash_bucket(tf.cast("tb",tf.string), 1000)
</code></pre>

<p>I can then loop over the weights returned here:</p>

<pre><code>for i,n in enumerate(m.linear_weights_["linear/hat_weights"]):
    print i, n
</code></pre>

<p>which gives me:</p>

<pre><code>linear/hat_weights
   0 [-0.147]
   ...
</code></pre>

<p>My problem is that none of the indices with significant (abs(x)>0.0005) weights correspond with any of the hash bucket ids I get from string_to_hash_bucket on all the hat labels in the dataset. </p>

<p>So finally my question:</p>

<p>Am I right in thinking that string_to_hash_bucket id should correspond to the index of the corresponding array return by m.linear_weights_["linear/hat_weights"]?</p>

<p>If not how can I obtain the correct id? Is there an easier way to inspect the weights of the feature column tensors,both sparse and real valued (which aren't even contained in .linear_weights_), in the linear model? </p>

<p>many thanks!</p>

<p>Another way to inspect the weights which is black-box (and hence guaranteed to work) is to evaluate your entire model on an example which just has a single hat feature turned on. Then you can see the weight assigned by the model to each feature.</p>

python tensorflow