Home | History | Annotate | Download | only in ops
      1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/core/framework/op.h"
     17 #include "tensorflow/core/framework/shape_inference.h"
     18 
     19 namespace tensorflow {
     20 REGISTER_OP("SparseFeatureCross")
     21     .Input("indices: N * int64")
     22     .Input("values: sparse_types")
     23     .Input("shapes: N * int64")
     24     .Input("dense: dense_types")
     25     .Output("output_indices: int64")
     26     .Output("output_values: out_type")
     27     .Output("output_shape: int64")
     28     .Attr("N: int >= 0")
     29     .Attr("hashed_output: bool")
     30     .Attr("num_buckets: int >= 0")
     31     .Attr("sparse_types: list({int64, string}) >= 0")
     32     .Attr("dense_types: list({int64, string}) >= 0")
     33     .Attr("out_type: {int64, string}")
     34     .Attr("internal_type: {int64, string}")
     35     .SetShapeFn([](shape_inference::InferenceContext* c) {
     36       c->set_output(0, c->Matrix(c->UnknownDim(), 2));
     37       c->set_output(1, c->Vector(c->UnknownDim()));
     38       c->set_output(2, c->Vector(2));
     39       return Status::OK();
     40     })
     41     .Doc(R"doc(
     42 Generates sparse cross form a list of sparse tensors.
     43 
     44 The op takes two lists, one of 2D `SparseTensor` and one of 2D `Tensor`, each
     45 representing features of one feature column. It outputs a 2D `SparseTensor` with
     46 the batchwise crosses of these features.
     47 
     48 For example, if the inputs are
     49 
     50     inputs[0]: SparseTensor with shape = [2, 2]
     51     [0, 0]: "a"
     52     [1, 0]: "b"
     53     [1, 1]: "c"
     54 
     55     inputs[1]: SparseTensor with shape = [2, 1]
     56     [0, 0]: "d"
     57     [1, 0]: "e"
     58 
     59     inputs[2]: Tensor [["f"], ["g"]]
     60 
     61 then the output will be
     62 
     63     shape = [2, 2]
     64     [0, 0]: "a_X_d_X_f"
     65     [1, 0]: "b_X_e_X_g"
     66     [1, 1]: "c_X_e_X_g"
     67 
     68 if hashed_output=true then the output will be
     69 
     70     shape = [2, 2]
     71     [0, 0]: HashCombine(
     72                 Fingerprint64("f"), HashCombine(
     73                     Fingerprint64("d"), Fingerprint64("a")))
     74     [1, 0]: HashCombine(
     75                 Fingerprint64("g"), HashCombine(
     76                     Fingerprint64("e"), Fingerprint64("b")))
     77     [1, 1]: HashCombine(
     78                 Fingerprint64("g"), HashCombine(
     79                     Fingerprint64("e"), Fingerprint64("c")))
     80 
     81 indices: 2-D.  Indices of each input `SparseTensor`.
     82 values: 1-D.   values of each `SparseTensor`.
     83 shapes: 1-D.   Shapes of each `SparseTensor`.
     84 dense: 2-D.    Columns represented by dense `Tensor`.
     85 output_indices: 2-D.  Indices of the concatenated `SparseTensor`.
     86 output_values: 1-D.  Non-empty values of the concatenated or hashed
     87   `SparseTensor`.
     88 output_shape: 1-D.  Shape of the concatenated `SparseTensor`.
     89 )doc");
     90 
     91 REGISTER_OP("SparseFeatureCrossV2")
     92     .Input("indices: N * int64")
     93     .Input("values: sparse_types")
     94     .Input("shapes: N * int64")
     95     .Input("dense: dense_types")
     96     .Output("output_indices: int64")
     97     .Output("output_values: out_type")
     98     .Output("output_shape: int64")
     99     .Attr("N: int >= 0")
    100     .Attr("hashed_output: bool")
    101     .Attr("num_buckets: int >= 0")
    102     .Attr("hash_key: int")
    103     .Attr("sparse_types: list({int64, string}) >= 0")
    104     .Attr("dense_types: list({int64, string}) >= 0")
    105     .Attr("out_type: {int64, string}")
    106     .Attr("internal_type: {int64, string}")
    107     .SetShapeFn([](shape_inference::InferenceContext* c) {
    108       c->set_output(0, c->Matrix(c->UnknownDim(), 2));
    109       c->set_output(1, c->Vector(c->UnknownDim()));
    110       c->set_output(2, c->Vector(2));
    111       return Status::OK();
    112     })
    113     .Doc(R"doc(
    114 Generates sparse cross form a list of sparse tensors.
    115 
    116 The op takes two lists, one of 2D `SparseTensor` and one of 2D `Tensor`, each
    117 representing features of one feature column. It outputs a 2D `SparseTensor` with
    118 the batchwise crosses of these features.
    119 
    120 For example, if the inputs are
    121 
    122     inputs[0]: SparseTensor with shape = [2, 2]
    123     [0, 0]: "a"
    124     [1, 0]: "b"
    125     [1, 1]: "c"
    126 
    127     inputs[1]: SparseTensor with shape = [2, 1]
    128     [0, 0]: "d"
    129     [1, 0]: "e"
    130 
    131     inputs[2]: Tensor [["f"], ["g"]]
    132 
    133 then the output will be
    134 
    135     shape = [2, 2]
    136     [0, 0]: "a_X_d_X_f"
    137     [1, 0]: "b_X_e_X_g"
    138     [1, 1]: "c_X_e_X_g"
    139 
    140 if hashed_output=true then the output will be
    141 
    142     shape = [2, 2]
    143     [0, 0]: FingerprintCat64(
    144                 Fingerprint64("f"), FingerprintCat64(
    145                     Fingerprint64("d"), Fingerprint64("a")))
    146     [1, 0]: FingerprintCat64(
    147                 Fingerprint64("g"), FingerprintCat64(
    148                     Fingerprint64("e"), Fingerprint64("b")))
    149     [1, 1]: FingerprintCat64(
    150                 Fingerprint64("g"), FingerprintCat64(
    151                     Fingerprint64("e"), Fingerprint64("c")))
    152 
    153 indices: 2-D.  Indices of each input `SparseTensor`.
    154 values: 1-D.   values of each `SparseTensor`.
    155 shapes: 1-D.   Shapes of each `SparseTensor`.
    156 dense: 2-D.    Columns represented by dense `Tensor`.
    157 output_indices: 2-D.  Indices of the concatenated `SparseTensor`.
    158 output_values: 1-D.  Non-empty values of the concatenated or hashed
    159   `SparseTensor`.
    160 output_shape: 1-D.  Shape of the concatenated `SparseTensor`.
    161 )doc");
    162 }  // namespace tensorflow
    163