Add a node invoking a registered Op to a graph. Example usage: # input1 and input2 can be Tensors or anything ops.convert_to_tensor() # will convert to a Tensor. op_def_library.apply_op("op", input1=input1, input2=input2) # Can specify a node name. op_def_libr
(self, op_type_name, name=None, **keywords)
| 309 | self.add_op(op_def) |
| 310 | |
| 311 | def apply_op(self, op_type_name, name=None, **keywords): |
| 312 | # pylint: disable=g-doc-args |
| 313 | """Add a node invoking a registered Op to a graph. |
| 314 | |
| 315 | Example usage: |
| 316 | # input1 and input2 can be Tensors or anything ops.convert_to_tensor() |
| 317 | # will convert to a Tensor. |
| 318 | op_def_library.apply_op("op", input1=input1, input2=input2) |
| 319 | # Can specify a node name. |
| 320 | op_def_library.apply_op("op", input1=input1, name="node_name") |
| 321 | # Must use keyword arguments, with the names specified in the OpDef. |
| 322 | op_def_library.apply_op("op", input_name=input, attr_name=attr) |
| 323 | |
| 324 | All attrs must either be inferred from an input or specified. |
| 325 | (If inferred, the attr must not be specified.) If an attr has a default |
| 326 | value specified in the Op's OpDef, then you may pass None as the value |
| 327 | of that attr to get the default. |
| 328 | |
| 329 | Args: |
| 330 | op_type_name: string. Must match the name field of a registered Op. |
| 331 | name: string. Optional name of the created op. |
| 332 | **keywords: input Tensor and attr arguments specified by name, |
| 333 | and optional parameters to pass when constructing the Operation. |
| 334 | |
| 335 | Returns: |
| 336 | The Tensor(s) representing the output of the operation, or the Operation |
| 337 | itself if there are no outputs. |
| 338 | |
| 339 | Raises: |
| 340 | RuntimeError: On some errors. |
| 341 | TypeError: On some errors. |
| 342 | ValueError: On some errors. |
| 343 | """ |
| 344 | output_structure, is_stateful, op = self._apply_op_helper( |
| 345 | op_type_name, name, **keywords) |
| 346 | if output_structure: |
| 347 | outputs = op.outputs |
| 348 | res = _Restructure(ops.convert_n_to_tensor(outputs), output_structure) |
| 349 | if isinstance(res, list) and not res and is_stateful: |
| 350 | return op |
| 351 | else: |
| 352 | return res |
| 353 | else: |
| 354 | return op |
| 355 | |
| 356 | def _apply_op_helper(self, op_type_name, name=None, **keywords): |
| 357 | """Implementation of apply_op that returns output_structure, op.""" |