def testInputLayer(self): with tf.Graph().as_default(): text_a = tf.SparseTensor( values=["hello world", "pair-programming", "hello world"], indices=[[0, 0], [0, 1], [1, 0]], dense_shape=[2, 2]) text_b = tf.SparseTensor( values=["hello world", "oov token"], indices=[[0, 0], [0, 1]], dense_shape=[2, 3]) features = { "text_a": text_a, "text_b": text_b, } feature_columns = [ hub.sparse_text_embedding_column( "text_a", self.spec, combiner="mean", default_value="__UNKNOWN__", trainable=False), hub.sparse_text_embedding_column( "text_b", self.spec, combiner="mean", default_value="__UNKNOWN__", trainable=False), ] input_layer = tf_v1.feature_column.input_layer(features, feature_columns) with tf_v1.train.MonitoredSession() as sess: output = sess.run(input_layer) self.assertAllEqual( output, [[3, 3.5, 4, 4.5, 0.5, 1, 1.5, 2], [1, 2, 3, 4, 0, 0, 0, 0]])
def testTrainableEmbeddingColumn(self): feature_columns = [ hub.sparse_text_embedding_column( "text", self.spec, combiner="mean", default_value=None, trainable=True), ] with tf.Graph().as_default(): text = tf.SparseTensor( values=["hello world", "pair-programming"], indices=[[0, 0], [1, 0]], dense_shape=[2, 2]) target = [[1, 1, 1, 1], [4, 3, 2, 1]] input_layer = tf_v1.feature_column.input_layer({"text": text}, feature_columns) loss = tf_v1.losses.mean_squared_error(input_layer, target) optimizer = tf_v1.train.GradientDescentOptimizer(learning_rate=0.97) train_op = optimizer.minimize(loss) with tf_v1.train.MonitoredSession() as sess: self.assertAllEqual(sess.run(input_layer), [[1, 2, 3, 4], [5, 5, 5, 5]]) for _ in range(10): sess.run(train_op) self.assertAllClose(sess.run(input_layer), target, atol=0.5)
def testEmptySparseTensorBatch(self): feature_columns = [ hub.sparse_text_embedding_column("text", self.spec, combiner="mean", default_value="default", trainable=True), ] with tf.Graph().as_default(): text = tf.SparseTensor(values=tf_v1.constant([], dtype=tf_v1.string, shape=[0]), indices=tf_v1.constant([], dtype=tf_v1.int64, shape=[0, 2]), dense_shape=[3, 0]) input_layer = tf_v1.feature_column.input_layer({"text": text}, feature_columns) with tf_v1.train.MonitoredSession() as sess: embeddings = sess.run(input_layer) self.assertAllEqual(embeddings, [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
def testParents(self): text_column = hub.sparse_text_embedding_column("text", self.spec, "sum", "", trainable=False) self.assertEqual(["text"], text_column.parents)
def testVariableShape(self): text_column = hub.sparse_text_embedding_column("text", self.spec, combiner="mean", default_value=None, trainable=False) self.assertEqual(text_column._variable_shape, [4])
def testMakeParseExampleSpec(self): text_column = hub.sparse_text_embedding_column( "text", self.spec, combiner="mean", default_value=None, trainable=False) parsing_spec = tf.compat.v1.feature_column.make_parse_example_spec( [text_column]) self.assertEqual( parsing_spec, {"text": tf.compat.v1.VarLenFeature(tf.string)})