def testSparseFeatureColumnWithHashedBucketSize(self):
movies = tf.contrib.layers.sparse_column_with_hash_bucket(
column_name="movies", hash_bucket_size=15)
with tf.Graph().as_default():
features = {
"movies": tf.SparseTensor(
values=["matrix", "head-on", "winter sleep"],
indices=[[0, 0], [0, 1], [1, 0]],
shape=[2, 2])
}
output, column_to_variable, _ = (
tf.contrib.layers.weighted_sum_from_feature_columns(features,
[movies],
num_outputs=1))
with self.test_session() as sess:
tf.global_variables_initializer().run()
tf.initialize_all_tables().run()
weights = column_to_variable[movies][0]
self.assertEqual(weights.get_shape(), (15, 1))
sess.run(weights.assign(weights + 0.4))
# score for first example = 0.4 (matrix) + 0.4 (head-on) = 0.8
# score for second example = 0.4 (winter sleep)
self.assertAllClose(output.eval(), [[0.8], [0.4]])
评论列表
文章目录