def testWeightedSparseColumn(self):
ids = tf.contrib.layers.sparse_column_with_keys(
"ids", ["marlo", "omar", "stringer"])
ids_tensor = tf.SparseTensor(values=["stringer", "stringer", "marlo"],
indices=[[0, 0], [1, 0], [1, 1]],
shape=[2, 2])
weighted_ids = tf.contrib.layers.weighted_sparse_column(ids, "weights")
weights_tensor = tf.SparseTensor(values=[10.0, 20.0, 30.0],
indices=[[0, 0], [1, 0], [1, 1]],
shape=[2, 2])
features = {"ids": ids_tensor,
"weights": weights_tensor}
output = feature_column_ops._Transformer(features).transform(weighted_ids)
with self.test_session():
tf.initialize_all_tables().run()
self.assertAllEqual(output[0].shape.eval(), ids_tensor.shape.eval())
self.assertAllEqual(output[0].indices.eval(), ids_tensor.indices.eval())
self.assertAllEqual(output[0].values.eval(), [2, 2, 0])
self.assertAllEqual(output[1].shape.eval(), weights_tensor.shape.eval())
self.assertAllEqual(output[1].indices.eval(),
weights_tensor.indices.eval())
self.assertEqual(output[1].values.dtype, tf.float32)
self.assertAllEqual(output[1].values.eval(), weights_tensor.values.eval())
评论列表
文章目录