Handles caching of transformations while building the model. `_FeatureColumn` specifies how to digest an input column to the network. Some feature columns require data transformations. This class caches those transformations. Some features may be used in more than one place. For example, o
| 2149 | |
| 2150 | |
| 2151 | class _LazyBuilder(object): |
| 2152 | """Handles caching of transformations while building the model. |
| 2153 | |
| 2154 | `_FeatureColumn` specifies how to digest an input column to the network. Some |
| 2155 | feature columns require data transformations. This class caches those |
| 2156 | transformations. |
| 2157 | |
| 2158 | Some features may be used in more than one place. For example, one can use a |
| 2159 | bucketized feature by itself and a cross with it. In that case we |
| 2160 | should create only one bucketization op instead of creating ops for each |
| 2161 | feature column separately. To handle re-use of transformed columns, |
| 2162 | `_LazyBuilder` caches all previously transformed columns. |
| 2163 | |
| 2164 | Example: |
| 2165 | We're trying to use the following `_FeatureColumn`s: |
| 2166 | |
| 2167 | ```python |
| 2168 | bucketized_age = fc.bucketized_column(fc.numeric_column("age"), ...) |
| 2169 | keywords = fc.categorical_column_with_hash_buckets("keywords", ...) |
| 2170 | age_X_keywords = fc.crossed_column([bucketized_age, "keywords"]) |
| 2171 | ... = linear_model(features, |
| 2172 | [bucketized_age, keywords, age_X_keywords] |
| 2173 | ``` |
| 2174 | |
| 2175 | If we transform each column independently, then we'll get duplication of |
| 2176 | bucketization (one for cross, one for bucketization itself). |
| 2177 | The `_LazyBuilder` eliminates this duplication. |
| 2178 | """ |
| 2179 | |
| 2180 | def __init__(self, features, adaptive_mask_tensors=None): |
| 2181 | """Creates a `_LazyBuilder`. |
| 2182 | |
| 2183 | Args: |
| 2184 | features: A mapping from feature column to objects that are `Tensor` or |
| 2185 | `SparseTensor`, or can be converted to same via |
| 2186 | `sparse_tensor.convert_to_tensor_or_sparse_tensor`. A `string` key |
| 2187 | signifies a base feature (not-transformed). A `_FeatureColumn` key |
| 2188 | means that this `Tensor` is the output of an existing `_FeatureColumn` |
| 2189 | which can be reused. |
| 2190 | """ |
| 2191 | self._features = features.copy() |
| 2192 | self._feature_tensors = {} |
| 2193 | self._adaptive_mask_tensors = adaptive_mask_tensors |
| 2194 | |
| 2195 | def get(self, key): |
| 2196 | """Returns a `Tensor` for the given key. |
| 2197 | |
| 2198 | A `str` key is used to access a base feature (not-transformed). When a |
| 2199 | `_FeatureColumn` is passed, the transformed feature is returned if it |
| 2200 | already exists, otherwise the given `_FeatureColumn` is asked to provide its |
| 2201 | transformed output, which is then cached. |
| 2202 | |
| 2203 | Args: |
| 2204 | key: a `str` or a `_FeatureColumn`. |
| 2205 | |
| 2206 | Returns: |
| 2207 | The transformed `Tensor` corresponding to the `key`. |
| 2208 |
no outgoing calls