Esempio n. 1
0
    def testSharedEmbeddingColumn(self):
        a1 = fc.sparse_column_with_keys("a1", ["marlo", "omar", "stringer"])
        a2 = fc.sparse_column_with_keys("a2", ["marlo", "omar", "stringer"])
        b = fc.shared_embedding_columns([a1, a2], dimension=4, combiner="mean")
        self.assertEqual(len(b), 2)
        self.assertEqual(b[0].shared_embedding_name, "a1_a2_shared_embedding")
        self.assertEqual(b[1].shared_embedding_name, "a1_a2_shared_embedding")

        # Create a sparse id tensor for a1.
        input_tensor_c1 = sparse_tensor_lib.SparseTensor(indices=[[0,
                                                                   0], [1, 1],
                                                                  [2, 2]],
                                                         values=[0, 1, 2],
                                                         dense_shape=[3, 3])
        # Create a sparse id tensor for a2.
        input_tensor_c2 = sparse_tensor_lib.SparseTensor(indices=[[0,
                                                                   0], [1, 1],
                                                                  [2, 2]],
                                                         values=[0, 1, 2],
                                                         dense_shape=[3, 3])
        with variable_scope.variable_scope("run_1"):
            b1 = feature_column_ops.input_from_feature_columns(
                {b[0]: input_tensor_c1}, [b[0]])
            b2 = feature_column_ops.input_from_feature_columns(
                {b[1]: input_tensor_c2}, [b[1]])
        with self.test_session() as sess:
            sess.run(variables.global_variables_initializer())
            b1_value = b1.eval()
            b2_value = b2.eval()
        for i in range(len(b1_value)):
            self.assertAllClose(b1_value[i], b2_value[i])

        # Test the case when a shared_embedding_name is explictly specified.
        d = fc.shared_embedding_columns(
            [a1, a2],
            dimension=4,
            combiner="mean",
            shared_embedding_name="my_shared_embedding")
        # a3 is a completely different sparse column with a1 and a2, but since the
        # same shared_embedding_name is passed in, a3 will have the same embedding
        # as a1 and a2
        a3 = fc.sparse_column_with_keys("a3", [42, 1, -1000],
                                        dtype=dtypes.int32)
        e = fc.shared_embedding_columns(
            [a3],
            dimension=4,
            combiner="mean",
            shared_embedding_name="my_shared_embedding")
        with variable_scope.variable_scope("run_2"):
            d1 = feature_column_ops.input_from_feature_columns(
                {d[0]: input_tensor_c1}, [d[0]])
            e1 = feature_column_ops.input_from_feature_columns(
                {e[0]: input_tensor_c1}, [e[0]])
        with self.test_session() as sess:
            sess.run(variables.global_variables_initializer())
            d1_value = d1.eval()
            e1_value = e1.eval()
        for i in range(len(d1_value)):
            self.assertAllClose(d1_value[i], e1_value[i])
  def testSharedEmbeddingColumn(self):
    a1 = fc.sparse_column_with_keys("a1", ["marlo", "omar", "stringer"])
    a2 = fc.sparse_column_with_keys("a2", ["marlo", "omar", "stringer"])
    b = fc.shared_embedding_columns([a1, a2], dimension=4, combiner="mean")
    self.assertEqual(len(b), 2)
    self.assertEqual(b[0].shared_embedding_name, "a1_a2_shared_embedding")
    self.assertEqual(b[1].shared_embedding_name, "a1_a2_shared_embedding")

    # Create a sparse id tensor for a1.
    input_tensor_c1 = sparse_tensor_lib.SparseTensor(
        indices=[[0, 0], [1, 1], [2, 2]], values=[0, 1, 2], dense_shape=[3, 3])
    # Create a sparse id tensor for a2.
    input_tensor_c2 = sparse_tensor_lib.SparseTensor(
        indices=[[0, 0], [1, 1], [2, 2]], values=[0, 1, 2], dense_shape=[3, 3])
    with variable_scope.variable_scope("run_1"):
      b1 = feature_column_ops.input_from_feature_columns({
          b[0]: input_tensor_c1
      }, [b[0]])
      b2 = feature_column_ops.input_from_feature_columns({
          b[1]: input_tensor_c2
      }, [b[1]])
    with self.test_session() as sess:
      sess.run(variables.global_variables_initializer())
      b1_value = b1.eval()
      b2_value = b2.eval()
    for i in range(len(b1_value)):
      self.assertAllClose(b1_value[i], b2_value[i])

    # Test the case when a shared_embedding_name is explictly specified.
    d = fc.shared_embedding_columns(
        [a1, a2],
        dimension=4,
        combiner="mean",
        shared_embedding_name="my_shared_embedding")
    # a3 is a completely different sparse column with a1 and a2, but since the
    # same shared_embedding_name is passed in, a3 will have the same embedding
    # as a1 and a2
    a3 = fc.sparse_column_with_keys("a3", ["cathy", "tom", "anderson"])
    e = fc.shared_embedding_columns(
        [a3],
        dimension=4,
        combiner="mean",
        shared_embedding_name="my_shared_embedding")
    with variable_scope.variable_scope("run_2"):
      d1 = feature_column_ops.input_from_feature_columns({
          d[0]: input_tensor_c1
      }, [d[0]])
      e1 = feature_column_ops.input_from_feature_columns({
          e[0]: input_tensor_c1
      }, [e[0]])
    with self.test_session() as sess:
      sess.run(variables.global_variables_initializer())
      d1_value = d1.eval()
      e1_value = e1.eval()
    for i in range(len(d1_value)):
      self.assertAllClose(d1_value[i], e1_value[i])
  def testSharedEmbeddingColumnErrors(self):
    # Tries passing in a string.
    with self.assertRaises(TypeError):
      invalid_string = "Invalid string."
      fc.shared_embedding_columns(invalid_string, dimension=2, combiner="mean")

    # Tries passing in a set of sparse columns.
    with self.assertRaises(TypeError):
      invalid_set = set([
          fc.sparse_column_with_keys("a", ["foo", "bar"]),
          fc.sparse_column_with_keys("b", ["foo", "bar"]),
      ])
      fc.shared_embedding_columns(invalid_set, dimension=2, combiner="mean")
Esempio n. 4
0
  def testSharedEmbeddingColumnErrors(self):
    # Tries passing in a string.
    with self.assertRaises(TypeError):
      invalid_string = "Invalid string."
      fc.shared_embedding_columns(invalid_string, dimension=2, combiner="mean")

    # Tries passing in a set of sparse columns.
    with self.assertRaises(TypeError):
      invalid_set = set([
          fc.sparse_column_with_keys("a", ["foo", "bar"]),
          fc.sparse_column_with_keys("b", ["foo", "bar"]),
      ])
      fc.shared_embedding_columns(invalid_set, dimension=2, combiner="mean")
 def testSharedEmbeddingColumnDeepCopy(self):
   a1 = fc.sparse_column_with_keys("a1", ["marlo", "omar", "stringer"])
   a2 = fc.sparse_column_with_keys("a2", ["marlo", "omar", "stringer"])
   columns = fc.shared_embedding_columns(
       [a1, a2], dimension=4, combiner="mean")
   columns_copy = copy.deepcopy(columns)
   self.assertEqual(
       columns_copy[0].shared_embedding_name, "a1_a2_shared_embedding")
   self.assertEqual(
       columns_copy[1].shared_embedding_name, "a1_a2_shared_embedding")
Esempio n. 6
0
 def testSharedEmbeddingColumnDeepCopy(self):
   a1 = fc.sparse_column_with_keys("a1", ["marlo", "omar", "stringer"])
   a2 = fc.sparse_column_with_keys("a2", ["marlo", "omar", "stringer"])
   columns = fc.shared_embedding_columns(
       [a1, a2], dimension=4, combiner="mean")
   columns_copy = copy.deepcopy(columns)
   self.assertEqual(
       columns_copy[0].shared_embedding_name, "a1_a2_shared_embedding")
   self.assertEqual(
       columns_copy[1].shared_embedding_name, "a1_a2_shared_embedding")
 def testSharedEmbeddingColumnDeterminism(self):
   # Tests determinism in auto-generated shared_embedding_name.
   sparse_id_columns = tuple([
       fc.sparse_column_with_keys(k, ["foo", "bar"])
       for k in ["07", "02", "00", "03", "05", "01", "09", "06", "04", "08"]
   ])
   output = fc.shared_embedding_columns(
       sparse_id_columns, dimension=2, combiner="mean")
   self.assertEqual(len(output), 10)
   for x in output:
     self.assertEqual(x.shared_embedding_name,
                      "00_01_02_plus_7_others_shared_embedding")
Esempio n. 8
0
 def testSharedEmbeddingColumnDeterminism(self):
   # Tests determinism in auto-generated shared_embedding_name.
   sparse_id_columns = tuple([
       fc.sparse_column_with_keys(k, ["foo", "bar"])
       for k in ["07", "02", "00", "03", "05", "01", "09", "06", "04", "08"]
   ])
   output = fc.shared_embedding_columns(
       sparse_id_columns, dimension=2, combiner="mean")
   self.assertEqual(len(output), 10)
   for x in output:
     self.assertEqual(x.shared_embedding_name,
                      "00_01_02_plus_7_others_shared_embedding")
Esempio n. 9
0
  def testSharedEmbeddingColumnWithWeightedSparseColumn(self):
    # Tests creation of shared embeddings containing weighted sparse columns.
    sparse_col = fc.sparse_column_with_keys("a1", ["marlo", "omar", "stringer"])
    ids = fc.sparse_column_with_keys("ids", ["marlo", "omar", "stringer"])
    weighted_sparse_col = fc.weighted_sparse_column(ids, "weights")
    self.assertEqual(weighted_sparse_col.name, "ids_weighted_by_weights")

    b = fc.shared_embedding_columns([sparse_col, weighted_sparse_col],
                                    dimension=4, combiner="mean")
    self.assertEqual(len(b), 2)
    self.assertEqual(b[0].shared_embedding_name,
                     "a1_ids_weighted_by_weights_shared_embedding")
    self.assertEqual(b[1].shared_embedding_name,
                     "a1_ids_weighted_by_weights_shared_embedding")

    # Tries reversing order to check compatibility condition.
    b = fc.shared_embedding_columns([weighted_sparse_col, sparse_col],
                                    dimension=4, combiner="mean")
    self.assertEqual(len(b), 2)
    self.assertEqual(b[0].shared_embedding_name,
                     "a1_ids_weighted_by_weights_shared_embedding")
    self.assertEqual(b[1].shared_embedding_name,
                     "a1_ids_weighted_by_weights_shared_embedding")

    # Tries adding two weighted columns to check compatibility between them.
    weighted_sparse_col_2 = fc.weighted_sparse_column(ids, "weights_2")
    b = fc.shared_embedding_columns([weighted_sparse_col,
                                     weighted_sparse_col_2],
                                    dimension=4, combiner="mean")
    self.assertEqual(len(b), 2)
    self.assertEqual(
        b[0].shared_embedding_name,
        "ids_weighted_by_weights_ids_weighted_by_weights_2_shared_embedding"
    )
    self.assertEqual(
        b[1].shared_embedding_name,
        "ids_weighted_by_weights_ids_weighted_by_weights_2_shared_embedding"
    )