def random_flip(images, labels, contours):
    border = tf.get_seed(0)
    seed = np.random.randint(border[1], border[0])
    images = tf.image.random_flip_left_right(images, seed)
    labels = tf.image.random_flip_left_right(labels, seed)
    contours = tf.image.random_flip_left_right(contours, seed)
    return images, labels, contours
def random_crop(images, labels, contours, shape):
    border = tf.get_seed(0)
    seed = np.random.randint(border[1], border[0])
    images = tf.random_crop(images, shape, seed)
    labels = tf.random_crop(labels, shape, seed)
    contours = tf.random_crop(contours, shape, seed)
    return images, labels, contours
Exemple #3
0
 def weaver_op(**kwds):
     seed1, seed2 = tf.get_seed(seed)
     return gen_clause_ops.random_clauses_weaver(examples=examples,
                                                 vocab=vocab,
                                                 shuffle=shuffle,
                                                 seed=seed1,
                                                 seed2=seed2,
                                                 **kwds)
Exemple #4
0
 def weaver_op(**kwds):
     seed1, seed2 = tf.get_seed(seed)
     return gen_clause_ops.fast_clause_weaver(clauses=clauses,
                                              shuffle=shuffle,
                                              seed=seed1,
                                              seed2=seed2,
                                              conjunction=and_ is not None,
                                              **kwds)
Exemple #5
0
        def _Initializer(shape, dtype=tf.float32, partition_info=None):
            """Variable initializer that loads pretrained embeddings."""
            unused_dtype = dtype
            seed1, seed2 = tf.get_seed(self._seed)
            t = gen_parser_ops.word_embedding_initializer(
                vectors=embeddings_path,
                task_context=task_context,
                embedding_init=self._embedding_init,
                seed=seed1,
                seed2=seed2)

            t.set_shape(shape)
            return t
Exemple #6
0
    def _Initializer(shape, dtype=tf.float32, partition_info=None):
      """Variable initializer that loads pretrained embeddings."""
      unused_dtype = dtype
      seed1, seed2 = tf.get_seed(self._seed)
      t = gen_parser_ops.word_embedding_initializer(
          vectors=embeddings_path,
          task_context=task_context,
          embedding_init=self._embedding_init,
          seed=seed1,
          seed2=seed2)

      t.set_shape(shape)
      return t
Exemple #7
0
def add_embeddings(channel_id, feature_spec, seed=None):
    """Adds a variable for the embedding of a given fixed feature.

  Supports pre-trained or randomly initialized embeddings.

  Args:
    channel_id: Numeric id of the fixed feature channel
    feature_spec: Feature spec protobuf of type FixedFeatureChannel
    seed: used for random initializer

  Returns:
    tf.Variable object corresponding to the embedding for that feature.

  Raises:
    RuntimeError: if more the pretrained embeddings are specified in resources
        containing more than one part.
  """
    check.Gt(
        feature_spec.embedding_dim, 0,
        'Embeddings requested for non-embedded feature: %s' % feature_spec)
    name = fixed_embeddings_name(channel_id)
    shape = [feature_spec.vocabulary_size, feature_spec.embedding_dim]
    if feature_spec.HasField('pretrained_embedding_matrix'):
        if len(feature_spec.pretrained_embedding_matrix.part) > 1:
            raise RuntimeError(
                'pretrained_embedding_matrix resource contains '
                'more than one part:\n%s',
                str(feature_spec.pretrained_embedding_matrix))
        if len(feature_spec.vocab.part) > 1:
            raise RuntimeError(
                'vocab resource contains more than one part:\n%s',
                str(feature_spec.vocab))
        seed1, seed2 = tf.get_seed(seed)
        embeddings = dragnn_ops.word_embedding_initializer(
            vectors=feature_spec.pretrained_embedding_matrix.part[0].
            file_pattern,
            vocabulary=feature_spec.vocab.part[0].file_pattern,
            seed=seed1,
            seed2=seed2)
        return tf.get_variable(name, initializer=tf.reshape(embeddings, shape))
    else:
        return tf.get_variable(name,
                               shape,
                               initializer=tf.random_normal_initializer(
                                   stddev=1.0 / feature_spec.embedding_dim**.5,
                                   seed=seed))
Exemple #8
0
  def __init__(self,
               images,
               labels,
               fake_data=False,
               one_hot=False,
               dtype=tf.float32,
               reshape=True,
               seed=None):
    """Construct a DataSet.
    one_hot arg is used only if fake_data is true.  `dtype` can be either
    `uint8` to leave the input as `[0, 255]`, or `float32` to rescale into
    `[0, 1]`.  Seed arg provides for convenient deterministic testing.
    """
    seed1, seed2 = tf.get_seed(seed)
    # If op level seed is not set, use whatever graph level seed is returned
    numpy.random.seed(seed1 if seed is None else seed2)
    dtype = tf.as_dtype(dtype).base_dtype
    if dtype not in (tf.uint8, tf.float32):
      raise TypeError(
          'Invalid image dtype %r, expected uint8 or float32' % dtype)
    if fake_data:
      self._num_examples = 10000
      self.one_hot = one_hot
    else:
      assert images.shape[0] == labels.shape[0], (
          'images.shape: %s labels.shape: %s' % (images.shape, labels.shape))
      self._num_examples = images.shape[0]

      # Convert shape from [num examples, rows, columns, depth]
      # to [num examples, rows*columns] (assuming depth == 1)
      if reshape:
        assert images.shape[3] == 1
        images = images.reshape(images.shape[0],
                                images.shape[1] * images.shape[2])
      if dtype == tf.float32:
        # Convert from [0, 255] -> [0.0, 1.0].
        images = images.astype(numpy.float32)
        images = numpy.multiply(images, 1.0 / 255.0)
    self._images = images
    self._labels = labels
    self._epochs_completed = 0
    self._index_in_epoch = 0
Exemple #9
0
def random_clauses_as_sequence(examples, vocab, seed=None, name=None):
  """Select one positive and one negative clause from a ProverClauseExamples.

  If the ProverClauseExamples doesn't have both positives and negatives,
  clauses and labels will be empty.

  Args:
    examples: 0-D `string` `Tensor`.  Serialized ProverClauseExamples.
    vocab: Path to vocabulary sstable.
    seed: A Python integer. Used to create a random seed for the distribution.
    name: A name for the operation (optional).

  Returns:
    negated_conjecture: 0-D `int32` negated encoded conjecture as `string`.
    clauses: 1-D `int32` clauses encoded as `string`.  One positive, one
      negative if possible, otherwise empty.
    labels: 1-D `bool` labels (true for positive, false for negative).
  """
  seed1, seed2 = tf.get_seed(seed)
  return gen_clause_ops.random_clauses_as_sequence(examples=examples,
                                                   vocab=vocab,
                                                   seed=seed1,
                                                   seed2=seed2,
                                                   name=name)
Exemple #10
0
from sklearn.manifold import TSNE
"""
    # 设置最小GPU使用量
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    self.sess=tf.Session(config=config)
    ==================================================
    # 自定义GPU使用量
    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.4 # 占用GPU40%的显存
    session = tf.Session(config=config)
"""
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5

tf.get_seed(1)
np.random.seed(1)


def plot_with_labels(lowDWeight, labels):
    plt.cla()
    X, Y = lowDWeight[:, 0], lowDWeight[:, 1]
    for x, y, s in zip(X, Y, labels):
        c = cm.rainbow(int(255 * s / 9))
        plt.text(x, y, s, backgroundcolor=c, fontsize=9)
    plt.xlim(X.min(), X.max())
    plt.ylim(Y.min(), Y.max())
    plt.title("The lasrt layer")
    plt.show()
    plt.pause(0.01)
Exemple #11
0
def copy(org_instance,
         dict_swap=None,
         scope="copied",
         replace_itself=False,
         copy_q=False,
         copy_parent_rvs=True):
    """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.Variable`s, `tf.placeholder`s, and nodes of type `Queue` are
  always reused and not copied. In addition, `tf.Operation`s 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.
      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.
      A scope for the new node(s). This is used to avoid name
      conflicts with the original node(s).
    replace_itself: bool.
      Whether to replace `org_instance` itself if it exists in
      `dict_swap`. (This is used for the recursion.)
    copy_q: bool.
      Whether to copy the replaced tensors too (if not already
      copied within the new scope). Otherwise will reuse them.
    copy_parent_rvs:
      Whether to copy parent random variables `org_instance` depends
      on. Otherwise will copy only the sample tensors and not the
      random variable class itself.

  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

  ```python
  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
  ```
  """
    if not isinstance(org_instance,
                      (RandomVariable, tf.Operation, tf.Tensor, tf.Variable)):
        raise TypeError("Could not copy instance: " + str(org_instance))

    if dict_swap is None:
        dict_swap = {}
    if scope[-1] != '/':
        scope += '/'

    # Swap instance if in dictionary.
    if org_instance in dict_swap and replace_itself:
        org_instance = dict_swap[org_instance]
        if not copy_q:
            return org_instance
    elif isinstance(org_instance, tf.Tensor) and replace_itself:
        # Deal with case when `org_instance` is the associated tensor
        # from the RandomVariable, e.g., `z.value()`. If
        # `dict_swap={z: qz}`, we aim to swap it with `qz.value()`.
        for key, value in six.iteritems(dict_swap):
            if isinstance(key, RandomVariable):
                if org_instance == key.value():
                    if isinstance(value, RandomVariable):
                        org_instance = value.value()
                    else:
                        org_instance = value
                    if not copy_q:
                        return org_instance
                    break

    # If instance is a tf.Variable, return it; do not copy any. Note we
    # check variables via their name. If we get variables through an
    # op's inputs, it has type tf.Tensor and not tf.Variable.
    if isinstance(org_instance, (tf.Tensor, tf.Variable)):
        for variable in tf.global_variables():
            if org_instance.name == variable.name:
                if variable in dict_swap and replace_itself:
                    # Deal with case when `org_instance` is the associated _ref
                    # tensor for a tf.Variable.
                    org_instance = dict_swap[variable]
                    if not copy_q or isinstance(org_instance, tf.Variable):
                        return org_instance
                    for variable in tf.global_variables():
                        if org_instance.name == variable.name:
                            return variable
                    break
                else:
                    return variable

    graph = tf.get_default_graph()
    new_name = scope + org_instance.name

    # If an instance of the same name exists, return it.
    if isinstance(org_instance, RandomVariable):
        for rv in random_variables():
            if new_name == rv.name:
                return rv
    elif isinstance(org_instance, (tf.Tensor, tf.Operation)):
        try:
            return graph.as_graph_element(new_name,
                                          allow_tensor=True,
                                          allow_operation=True)
        except:
            pass

    # Preserve ordering of random variables. Random variables are always
    # copied first (from parent -> child) before any deterministic
    # operations that depend on them.
    if copy_parent_rvs and \
            isinstance(org_instance, (RandomVariable, tf.Tensor, tf.Variable)):
        for v in get_parents(org_instance):
            copy(v, dict_swap, scope, True, copy_q, True)

    if isinstance(org_instance, RandomVariable):
        rv = org_instance

        # If it has copiable arguments, copy them.
        args = [
            _copy_default(arg, dict_swap, scope, True, copy_q, False)
            for arg in rv._args
        ]

        kwargs = {}
        for key, value in six.iteritems(rv._kwargs):
            if isinstance(value, list):
                kwargs[key] = [
                    _copy_default(v, dict_swap, scope, True, copy_q, False)
                    for v in value
                ]
            else:
                kwargs[key] = _copy_default(value, dict_swap, scope, True,
                                            copy_q, False)

        kwargs['name'] = new_name
        # Create new random variable with copied arguments.
        try:
            new_rv = type(rv)(*args, **kwargs)
        except ValueError:
            # Handle case where parameters are copied under absolute name
            # scope. This can cause an error when creating a new random
            # variable as tf.identity name ops are called on parameters ("op
            # with name already exists"). To avoid remove absolute name scope.
            kwargs['name'] = new_name[:-1]
            new_rv = type(rv)(*args, **kwargs)
        return new_rv
    elif isinstance(org_instance, tf.Tensor):
        tensor = org_instance

        # Do not copy tf.placeholders.
        if 'Placeholder' in tensor.op.type:
            return tensor

        # A tensor is one of the outputs of its underlying
        # op. Therefore copy the op itself.
        op = tensor.op
        new_op = copy(op, dict_swap, scope, True, copy_q, False)

        output_index = op.outputs.index(tensor)
        new_tensor = new_op.outputs[output_index]

        # Add copied tensor to collections that the original one is in.
        for name, collection in six.iteritems(tensor.graph._collections):
            if tensor in collection:
                graph.add_to_collection(name, new_tensor)

        return new_tensor
    elif isinstance(org_instance, tf.Operation):
        op = org_instance

        # Do not copy queue operations.
        if 'Queue' in op.type:
            return op

        # Copy the node def.
        # It is unique to every Operation instance. Replace the name and
        # its operation-level seed if it has one.
        node_def = deepcopy(op.node_def)
        node_def.name = new_name

        # when copying control flow contexts,
        # we need to make sure frame definitions are copied
        if 'frame_name' in node_def.attr and node_def.attr[
                'frame_name'].s != b'':
            node_def.attr['frame_name'].s = (scope.encode('utf-8') +
                                             node_def.attr['frame_name'].s)

        if 'seed2' in node_def.attr and tf.get_seed(None)[1] is not None:
            node_def.attr['seed2'].i = tf.get_seed(None)[1]

        # Copy other arguments needed for initialization.
        output_types = op._output_types[:]

        # If it has an original op, copy it.
        if op._original_op is not None:
            original_op = copy(op._original_op, dict_swap, scope, True, copy_q,
                               False)
        else:
            original_op = None

        # Copy the op def.
        # It is unique to every Operation type.
        op_def = deepcopy(op.op_def)

        new_op = tf.Operation(
            node_def,
            graph,
            [],  # inputs; will add them afterwards
            output_types,
            [],  # control inputs; will add them afterwards
            [],  # input types; will add them afterwards
            original_op,
            op_def)

        # advertise op early to break recursions
        graph._add_op(new_op)

        # If it has control inputs, copy them.
        control_inputs = []
        for x in op.control_inputs:
            elem = copy(x, dict_swap, scope, True, copy_q, False)
            if not isinstance(elem, tf.Operation):
                elem = tf.convert_to_tensor(elem)

            control_inputs.append(elem)

        new_op._add_control_inputs(control_inputs)

        # If it has inputs, copy them.
        for x in op.inputs:
            elem = copy(x, dict_swap, scope, True, copy_q, False)
            if not isinstance(elem, tf.Operation):
                elem = tf.convert_to_tensor(elem)

            new_op._add_input(elem)

        # Copy the control flow context.
        control_flow_context = _copy_context(op._get_control_flow_context(),
                                             {}, dict_swap, scope, copy_q)
        new_op._set_control_flow_context(control_flow_context)

        # Use Graph's private methods to add the op, following
        # implementation of `tf.Graph().create_op()`.
        compute_shapes = True
        compute_device = True
        op_type = new_name

        if compute_shapes:
            #set_shapes_for_outputs(new_op)
            set_shape_and_handle_data_for_outputs(new_op)
        graph._record_op_seen_by_control_dependencies(new_op)

        if compute_device:
            graph._apply_device_functions(new_op)

        if graph._colocation_stack:
            all_colocation_groups = []
            for colocation_op in graph._colocation_stack:
                all_colocation_groups.extend(colocation_op.colocation_groups())
                if colocation_op.device:
                    # Make this device match the device of the colocated op, to
                    # provide consistency between the device and the colocation
                    # property.
                    if new_op.device and new_op.device != colocation_op.device:
                        logging.warning(
                            "Tried to colocate %s with an op %s that had "
                            "a different device: %s vs %s. "
                            "Ignoring colocation property.", name,
                            colocation_op.name, new_op.device,
                            colocation_op.device)

            all_colocation_groups = sorted(set(all_colocation_groups))
            new_op.node_def.attr["_class"].CopyFrom(
                attr_value_pb2.AttrValue(
                    list=attr_value_pb2.AttrValue.ListValue(
                        s=all_colocation_groups)))

        # Sets "container" attribute if
        # (1) graph._container is not None
        # (2) "is_stateful" is set in OpDef
        # (3) "container" attribute is in OpDef
        # (4) "container" attribute is None
        if (graph._container and op_type in graph._registered_ops
                and graph._registered_ops[op_type].is_stateful
                and "container" in new_op.node_def.attr
                and not new_op.node_def.attr["container"].s):
            new_op.node_def.attr["container"].CopyFrom(
                attr_value_pb2.AttrValue(s=compat.as_bytes(graph._container)))

        return new_op
    else:
        raise TypeError("Could not copy instance: " + str(org_instance))
Exemple #12
0
def copy(org_instance, dict_swap=None, scope="copied",
         replace_itself=False, copy_q=False, copy_parent_rvs=True):
  """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.Variable`s, `tf.placeholder`s, and nodes of type `Queue` are
  always reused and not copied. In addition, `tf.Operation`s 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.
      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.
      A scope for the new node(s). This is used to avoid name
      conflicts with the original node(s).
    replace_itself: bool.
      Whether to replace `org_instance` itself if it exists in
      `dict_swap`. (This is used for the recursion.)
    copy_q: bool.
      Whether to copy the replaced tensors too (if not already
      copied within the new scope). Otherwise will reuse them.
    copy_parent_rvs:
      Whether to copy parent random variables `org_instance` depends
      on. Otherwise will copy only the sample tensors and not the
      random variable class itself.

  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

  ```python
  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
  ```
  """
  if not isinstance(org_instance,
                    (RandomVariable, tf.Operation, tf.Tensor, tf.Variable)):
    raise TypeError("Could not copy instance: " + str(org_instance))

  if dict_swap is None:
    dict_swap = {}
  if scope[-1] != '/':
    scope += '/'

  # Swap instance if in dictionary.
  if org_instance in dict_swap and replace_itself:
    org_instance = dict_swap[org_instance]
    if not copy_q:
      return org_instance
  elif isinstance(org_instance, tf.Tensor) and replace_itself:
    # Deal with case when `org_instance` is the associated tensor
    # from the RandomVariable, e.g., `z.value()`. If
    # `dict_swap={z: qz}`, we aim to swap it with `qz.value()`.
    for key, value in six.iteritems(dict_swap):
      if isinstance(key, RandomVariable):
        if org_instance == key.value():
          if isinstance(value, RandomVariable):
            org_instance = value.value()
          else:
            org_instance = value
          if not copy_q:
            return org_instance
          break

  # If instance is a tf.Variable, return it; do not copy any. Note we
  # check variables via their name. If we get variables through an
  # op's inputs, it has type tf.Tensor and not tf.Variable.
  if isinstance(org_instance, (tf.Tensor, tf.Variable)):
    for variable in tf.global_variables():
      if org_instance.name == variable.name:
        if variable in dict_swap and replace_itself:
          # Deal with case when `org_instance` is the associated _ref
          # tensor for a tf.Variable.
          org_instance = dict_swap[variable]
          if not copy_q or isinstance(org_instance, tf.Variable):
            return org_instance
          for variable in tf.global_variables():
            if org_instance.name == variable.name:
              return variable
          break
        else:
          return variable

  graph = tf.get_default_graph()
  new_name = scope + org_instance.name

  # If an instance of the same name exists, return it.
  if isinstance(org_instance, RandomVariable):
    for rv in random_variables():
      if new_name == rv.name:
        return rv
  elif isinstance(org_instance, (tf.Tensor, tf.Operation)):
    try:
      return graph.as_graph_element(new_name,
                                    allow_tensor=True,
                                    allow_operation=True)
    except:
      pass

  # Preserve ordering of random variables. Random variables are always
  # copied first (from parent -> child) before any deterministic
  # operations that depend on them.
  if copy_parent_rvs and \
          isinstance(org_instance, (RandomVariable, tf.Tensor, tf.Variable)):
    for v in get_parents(org_instance):
      copy(v, dict_swap, scope, True, copy_q, True)

  if isinstance(org_instance, RandomVariable):
    rv = org_instance

    # If it has copiable arguments, copy them.
    args = [_copy_default(arg, dict_swap, scope, True, copy_q, False)
            for arg in rv._args]

    kwargs = {}
    for key, value in six.iteritems(rv._kwargs):
      if isinstance(value, list):
        kwargs[key] = [_copy_default(v, dict_swap, scope, True, copy_q, False)
                       for v in value]
      else:
        kwargs[key] = _copy_default(
            value, dict_swap, scope, True, copy_q, False)

    kwargs['name'] = new_name
    # Create new random variable with copied arguments.
    try:
      new_rv = type(rv)(*args, **kwargs)
    except ValueError:
      # Handle case where parameters are copied under absolute name
      # scope. This can cause an error when creating a new random
      # variable as tf.identity name ops are called on parameters ("op
      # with name already exists"). To avoid remove absolute name scope.
      kwargs['name'] = new_name[:-1]
      new_rv = type(rv)(*args, **kwargs)
    return new_rv
  elif isinstance(org_instance, tf.Tensor):
    tensor = org_instance

    # Do not copy tf.placeholders.
    if 'Placeholder' in tensor.op.type:
      return tensor

    # A tensor is one of the outputs of its underlying
    # op. Therefore copy the op itself.
    op = tensor.op
    new_op = copy(op, dict_swap, scope, True, copy_q, False)

    output_index = op.outputs.index(tensor)
    new_tensor = new_op.outputs[output_index]

    # Add copied tensor to collections that the original one is in.
    for name, collection in six.iteritems(tensor.graph._collections):
      if tensor in collection:
        graph.add_to_collection(name, new_tensor)

    return new_tensor
  elif isinstance(org_instance, tf.Operation):
    op = org_instance

    # Do not copy queue operations.
    if 'Queue' in op.type:
      return op

    # Copy the node def.
    # It is unique to every Operation instance. Replace the name and
    # its operation-level seed if it has one.
    node_def = deepcopy(op.node_def)
    node_def.name = new_name

    # when copying control flow contexts,
    # we need to make sure frame definitions are copied
    if 'frame_name' in node_def.attr and node_def.attr['frame_name'].s != b'':
      node_def.attr['frame_name'].s = (scope.encode('utf-8') +
                                       node_def.attr['frame_name'].s)

    if 'seed2' in node_def.attr and tf.get_seed(None)[1] is not None:
      node_def.attr['seed2'].i = tf.get_seed(None)[1]

    # Copy other arguments needed for initialization.
    output_types = op._output_types[:]

    # If it has an original op, copy it.
    if op._original_op is not None:
      original_op = copy(op._original_op, dict_swap, scope, True, copy_q, False)
    else:
      original_op = None

    # Copy the op def.
    # It is unique to every Operation type.
    op_def = deepcopy(op.op_def)

    new_op = tf.Operation(node_def,
                          graph,
                          [],  # inputs; will add them afterwards
                          output_types,
                          [],  # control inputs; will add them afterwards
                          [],  # input types; will add them afterwards
                          original_op,
                          op_def)

    # advertise op early to break recursions
    graph._add_op(new_op)

    # If it has control inputs, copy them.
    control_inputs = []
    for x in op.control_inputs:
      elem = copy(x, dict_swap, scope, True, copy_q, False)
      if not isinstance(elem, tf.Operation):
        elem = tf.convert_to_tensor(elem)

      control_inputs.append(elem)

    new_op._add_control_inputs(control_inputs)

    # If it has inputs, copy them.
    for x in op.inputs:
      elem = copy(x, dict_swap, scope, True, copy_q, False)
      if not isinstance(elem, tf.Operation):
        elem = tf.convert_to_tensor(elem)

      new_op._add_input(elem)

    # Copy the control flow context.
    control_flow_context = _copy_context(op._get_control_flow_context(), {},
                                         dict_swap, scope, copy_q)
    new_op._set_control_flow_context(control_flow_context)

    # Use Graph's private methods to add the op, following
    # implementation of `tf.Graph().create_op()`.
    compute_shapes = True
    compute_device = True
    op_type = new_name

    if compute_shapes:
      set_shapes_for_outputs(new_op)
    graph._record_op_seen_by_control_dependencies(new_op)

    if compute_device:
      graph._apply_device_functions(new_op)

    if graph._colocation_stack:
      all_colocation_groups = []
      for colocation_op in graph._colocation_stack:
        all_colocation_groups.extend(colocation_op.colocation_groups())
        if colocation_op.device:
          # Make this device match the device of the colocated op, to
          # provide consistency between the device and the colocation
          # property.
          if new_op.device and new_op.device != colocation_op.device:
            logging.warning("Tried to colocate %s with an op %s that had "
                            "a different device: %s vs %s. "
                            "Ignoring colocation property.",
                            name, colocation_op.name, new_op.device,
                            colocation_op.device)

      all_colocation_groups = sorted(set(all_colocation_groups))
      new_op.node_def.attr["_class"].CopyFrom(attr_value_pb2.AttrValue(
          list=attr_value_pb2.AttrValue.ListValue(s=all_colocation_groups)))

    # Sets "container" attribute if
    # (1) graph._container is not None
    # (2) "is_stateful" is set in OpDef
    # (3) "container" attribute is in OpDef
    # (4) "container" attribute is None
    if (graph._container and
        op_type in graph._registered_ops and
        graph._registered_ops[op_type].is_stateful and
        "container" in new_op.node_def.attr and
            not new_op.node_def.attr["container"].s):
      new_op.node_def.attr["container"].CopyFrom(
          attr_value_pb2.AttrValue(s=compat.as_bytes(graph._container)))

    return new_op
  else:
    raise TypeError("Could not copy instance: " + str(org_instance))
Exemple #13
0
tf.divide()
tf.batch_to_space_nd()
tf.space_to_batch_nd()
tf.batch_to_space()
tf.space_to_batch()

tf.depth_to_space()
tf.space_to_depth()

tf.dtypes

tf.get_collection()
tf.get_collection_ref()
tf.get_default_session()
tf.get_local_variable
tf.get_seed()
tf.get_session_handle()
tf.get_session_tensor()
tf.get_default_graph()
tf.get_summary_op()
tf.get_variable()
tf.get_variable_scope()
tf.set_random_seed()
tf.serialize_tensor()
tf.save_v2()
tf.scalar_mul()
tf.scan()
tf.scatter_add()
tf.scatter_div()
tf.scatter_mul()
tf.scatter_nd()