MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / capture

Method capture

tensorflow/python/framework/func_graph.py:550–606  ·  view source on GitHub ↗

Captures `tensor` if it's external to this graph. If `tensor` is from a different graph, returns a placeholder for it. `tensor` and the placeholder will appear in self.captures, and the placeholder will appear in self.inputs. Multiple calls to this method with the same `tensor` arg

(self, tensor, name=None)

Source from the content-addressed store, hash-verified

548 compute_device)
549
550 def capture(self, tensor, name=None):
551 """Captures `tensor` if it's external to this graph.
552
553 If `tensor` is from a different graph, returns a placeholder for it.
554 `tensor` and the placeholder will appear in self.captures, and the
555 placeholder will appear in self.inputs. Multiple calls to this method with
556 the same `tensor` argument will return the same placeholder. If `tensor` is
557 from this graph, returns `tensor`.
558
559 Args:
560 tensor: Tensor. May be from this FuncGraph or a different graph.
561 name: Optional name if a placeholder is created.
562
563 Returns:
564 Tensor from this FuncGraph.
565
566 Raises:
567 InaccessibleTensorError: if any tensors are accessed in a manner that
568 bypasses the mechanisms required for the data dependencies to be correctly
569 wired.
570 """
571 # Note: _forward_func_graph is currently only set when building the gradient
572 # graph graph of a defun call. If the backwards graph tries to capture
573 # tensors those will be captured first in the forward graph. This
574 # makes sure that any tensor needed by a custom_gradient is correctly
575 # captured.
576
577 if (getattr(tensor, "graph", None) is not self and
578 hasattr(self, "_forward_func_graph") and
579 isinstance(self._forward_func_graph, FuncGraph)):
580 tensor = self._forward_func_graph.capture(tensor)
581 if isinstance(tensor, ops.EagerTensor):
582 if name is None:
583 name = str(ops.uid())
584
585 # Small EagerTensors are captured with Const ops
586 if (tensor.dtype in dtypes.TF_VALUE_DTYPES and
587 np.prod(tensor.shape) <= _EAGER_CONST_THRESHOLD):
588 return self.capture_eager_tensor(tensor, name)
589
590 # Large EagerTensors and resources are captured with Placeholder ops
591 return self._capture_helper(tensor, name)
592 if tensor.graph is not self:
593 if name is None:
594 name = tensor.op.name
595 inner_graph = tensor.graph
596 while inner_graph is not None and isinstance(inner_graph, FuncGraph):
597 if inner_graph is self:
598 raise errors.InaccessibleTensorError(
599 "The tensor '%s' cannot be accessed here: it is defined"
600 " in another function or code block. Use return values,"
601 " explicit Python locals or TensorFlow collections to access"
602 " it. Defined in: %s; accessed from: %s.\n"
603 % (tensor, tensor.graph, self))
604 inner_graph = inner_graph.outer_graph
605 return self._capture_helper(tensor, name)
606 return tensor
607

Callers 4

_capture_by_valueMethod · 0.95
create_opMethod · 0.95
func_graph_from_py_funcFunction · 0.95
_get_defun_inputsFunction · 0.45

Calls 3

capture_eager_tensorMethod · 0.95
_capture_helperMethod · 0.95
uidMethod · 0.80

Tested by

no test coverage detected