ed.copy

Aliases:

  • ed.copy
  • ed.util.copy
copy(
    org_instance,
    dict_swap=None,
    scope='copied',
    replace_itself=False,
    copy_q=False
)

Defined in edward/util/random_variables.py.

Build a new node in the TensorFlow graph from org_instance, where any of its ancestors existing in dict_swap are replaced with dict_swap's corresponding value.

Copying is done recursively. Any Operation whose output is required to copy org_instance is also copied (if it isn't already copied within the new scope).

tf.Variables, tf.placeholders, and nodes of type Queue are always reused and not copied. In addition, tf.Operations with operation-level seeds are copied with a new operation-level seed.

Args:

  • org_instance: RandomVariable, tf.Operation, tf.Tensor, or tf.Variable. Node to add in graph with replaced ancestors.
  • dict_swap: dict, optional. Random variables, variables, tensors, or operations to swap with. Its keys are what org_instance may depend on, and its values are the corresponding object (not necessarily of the same class instance, but must have the same type, e.g., float32) that is used in exchange.
  • scope: str, optional. A scope for the new node(s). This is used to avoid name conflicts with the original node(s).
  • replace_itself: bool, optional Whether to replace org_instance itself if it exists in dict_swap. (This is used for the recursion.)
  • copy_q: bool, optional> Whether to copy the replaced tensors too (if not already copied within the new scope). Otherwise will reuse them.

Returns:

RandomVariable, tf.Variable, tf.Tensor, or tf.Operation. The copied node.

Raises:

TypeError. If org_instance is not one of the above types.

Examples

x = tf.constant(2.0)
y = tf.constant(3.0)
z = x * y

qx = tf.constant(4.0)
# The TensorFlow graph is currently
# `x` -> `z` <- y`, `qx`

# This adds a subgraph with newly copied nodes,
# `qx` -> `copied/z` <- `copied/y`
z_new = ed.copy(z, {x: qx})

sess = tf.Session()
sess.run(z)
6.0
sess.run(z_new)
12.0