Fetch mapper for singleton tensors and ops.
| 285 | |
| 286 | |
| 287 | class _ElementFetchMapper(_FetchMapper): |
| 288 | """Fetch mapper for singleton tensors and ops.""" |
| 289 | |
| 290 | def __init__(self, fetches, contraction_fn): |
| 291 | """Creates an _ElementFetchMapper. |
| 292 | |
| 293 | This is the fetch mapper used for leaves in the fetch struct. Because of |
| 294 | the expansions mechanism, a leaf can actually fetch more than one tensor. |
| 295 | |
| 296 | Also note that the fetches here can be just strings (tensor or op names) or |
| 297 | any other object that the graph knows how to convert to a tensor, such as a |
| 298 | Variable. So we have to run each fetch through `as_graph_element()` to get |
| 299 | the corresponding tensor or op. |
| 300 | |
| 301 | Args: |
| 302 | fetches: List of objects, as returned by a fetch_fn defined in |
| 303 | _REGISTERED_EXPANSIONS. |
| 304 | contraction_fn: Callable as returned by a fetch_fn. |
| 305 | """ |
| 306 | self._unique_fetches = [] |
| 307 | for fetch in fetches: |
| 308 | try: |
| 309 | self._unique_fetches.append(ops.get_default_graph().as_graph_element( |
| 310 | fetch, allow_tensor=True, allow_operation=True)) |
| 311 | except TypeError as e: |
| 312 | raise TypeError('Fetch argument %r has invalid type %r, ' |
| 313 | 'must be a string or Tensor. (%s)' % |
| 314 | (fetch, type(fetch), str(e))) |
| 315 | except ValueError as e: |
| 316 | raise ValueError('Fetch argument %r cannot be interpreted as a ' |
| 317 | 'Tensor. (%s)' % (fetch, str(e))) |
| 318 | except KeyError as e: |
| 319 | raise ValueError('Fetch argument %r cannot be interpreted as a ' |
| 320 | 'Tensor. (%s)' % (fetch, str(e))) |
| 321 | self._contraction_fn = contraction_fn |
| 322 | |
| 323 | def unique_fetches(self): |
| 324 | return self._unique_fetches |
| 325 | |
| 326 | def build_results(self, values): |
| 327 | if not values: |
| 328 | # 'Operation' case |
| 329 | return None |
| 330 | else: |
| 331 | return self._contraction_fn(values) |
| 332 | |
| 333 | |
| 334 | def _uniquify_fetches(fetch_mappers): |