python类DType()的实例源码

loom.py 文件源码 项目:fold 作者: tensorflow 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def __new__(cls, dtype=None, shape=None, tag='', tensor=None):
    if tensor is not None:
      if dtype is not None:
        raise TypeError('Specify only one of tensor and dtype.')
      if shape is not None:
        raise TypeError('Specify only one of tensor and shape.')
      dtype = tensor.dtype
      shape = tensor.get_shape().as_list()
    elif not (isinstance(dtype, tf.DType) or
              isinstance(dtype, six.string_types)):
      raise TypeError('%r is not a tf.DType or string' % (dtype,))
    dtype = tf.as_dtype(dtype).base_dtype.name
    if not all(isinstance(s, numbers.Integral) and s >= 0 for s in shape):
      raise TypeError('shape must be non-negative integers: %s' % shape)
    shape = tuple(int(s) for s in shape)
    if not isinstance(tag, six.string_types):
      raise TypeError('A TypeShape tag must be a string; type of %r is %s' %
                      (tag, type(tag)))
    return _TypeShape.__new__(cls, dtype, shape, tag)
misc.py 文件源码 项目:GPflow 作者: GPflow 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def normalize_num_type(num_type):
    """
    Work out what a sensible type for the array is. if the default type
    is float32, downcast 64bit float to float32. For ints, assume int32
    """
    if isinstance(num_type, tf.DType):
        num_type = num_type.as_numpy_dtype.type

    if num_type in [np.float32, np.float64]:  # pylint: disable=E1101
        num_type = settings.float_type
    elif num_type in [np.int16, np.int32, np.int64]:
        num_type = settings.int_type
    else:
        raise ValueError('Unknown dtype "{0}" passed to normalizer.'.format(num_type))

    return num_type


# def types_array(tensor, shape=None):
#     shape = shape if shape is not None else tensor.shape.as_list()
#     return np.full(shape, tensor.dtype).tolist()
abstract_reader.py 文件源码 项目:DLTK 作者: DLTK 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __init__(self, read_fn, dtypes):
        """Constructs a Reader instance

        Args:
            read_fn: Input function returning features which is a dictionary of
                string feature name to `Tensor` or `SparseTensor`. If it
                returns a tuple, first item is extracted as features.
                Prediction continues until `input_fn` raises an end-of-input
                exception (`OutOfRangeError` or `StopIteration`).
            dtypes:  A nested structure of tf.DType objects corresponding to
                each component of an element yielded by generator.

        """
        self.dtypes = dtypes

        self.read_fn = read_fn
result_types.py 文件源码 项目:fold 作者: tensorflow 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __init__(self, shape, dtype='float32'):
    """Creates a tensor type.

    Args:
      shape: A tuple or list of non-negative integers.
      dtype: A `tf.DType`, or stringified version thereof (e.g. `'int64'`).

    Raises:
      TypeError: If `shape` is not a tuple or list of non-negative integers.
      TypeError: If `dtype` cannot be converted to a TF dtype.
    """
    if not isinstance(shape, (tuple, list)):
      raise TypeError('shape must be a tuple or list: %s' % str(shape))
    self._type_shape = loom.TypeShape(dtype, shape)
nn.py 文件源码 项目:XMUNMT 作者: XMUNLP 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def layer_norm(inputs, epsilon=1e-6, dtype=None, scope=None):
    """ Layer Normalization

    Args:
        inputs: A Tensor of shape [..., channel_size]
        epsilon: A floating number
        dtype: An optional instance of tf.DType
        scope: An optional string

    Returns:
            A Tensor with the same shape as inputs
    """
    with tf.variable_scope(scope, default_name="layer_norm", values=[inputs],
                           dtype=dtype):
        channel_size = inputs.get_shape().as_list()[-1]

        scale = tf.get_variable("scale", shape=[channel_size],
                                initializer=tf.ones_initializer())

        offset = tf.get_variable("offset", shape=[channel_size],
                                 initializer=tf.zeros_initializer())

        mean = tf.reduce_mean(inputs, axis=-1, keep_dims=True)
        variance = tf.reduce_mean(tf.square(inputs - mean), axis=-1,
                                  keep_dims=True)

        norm_inputs = (inputs - mean) * tf.rsqrt(variance + epsilon)

        return norm_inputs * scale + offset
tools.py 文件源码 项目:hart 作者: akosiorek 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __init__(self, img_size, in_memory=True, dtype=np.float32, **kwargs):

        super(ImageStore, self).__init__(**kwargs)
        self.img_size = img_size
        self.in_memory = in_memory

        if isinstance(dtype, tf.DType):
            dtype = getattr(np, dtype.name)

        self.dtype = dtype
        self.lock = Lock()
tools.py 文件源码 项目:hart 作者: akosiorek 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __init__(self, data_dict, n_timesteps, img_size, batch_size, overlap_fraction=.5,
                 sample_objects=False, num_epochs=None, shuffle=True,
                 which_seqs=None, n_threads=3, in_memory=False, depth_folder=None,
                 storage_dtype=tf.float32, mirror=False, reverse=False, bbox_scale=1., name='',
                 deplete_queues_at_length_increase=True):

        assert isinstance(storage_dtype, tf.DType)

        self.data_dict = data_dict
        self.img_size = img_size
        self.batch_size = batch_size
        self.overlap_fraction = overlap_fraction
        self.sample_objects = sample_objects
        self.n_threads = n_threads
        self.in_memory = in_memory
        self.depth_folder = depth_folder
        self.storage_dtype = storage_dtype
        self.mirror = mirror
        self.reverse = reverse
        self.bbox_scale = bbox_scale
        self.name = name
        self.deplete_queues_at_length_increase = deplete_queues_at_length_increase

        if which_seqs is not None:
            self._filter_seqs(which_seqs)

        super(KittiStore, self).__init__(self.data_dict, num_epochs, shuffle)

        self.set_length(n_timesteps)
utils.py 文件源码 项目:nengo_dl 作者: nengo 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def cast_dtype(dtype, target):
    """Changes float dtypes to the target dtype, leaves others unchanged.

    Used to map all float values to a target precision.  Also casts numpy
    dtypes to TensorFlow dtypes.

    Parameters
    ----------
    dtype : ``tf.DType`` or :class:`~numpy:numpy.dtype`
        Input dtype to be converted
    target : ``tf.DType``
        Floating point dtype to which all floating types should be converted

    Returns
    -------
    ``tf.DType``
        Input dtype, converted to ``target`` type if necessary
    """

    if not isinstance(dtype, tf.DType):
        dtype = tf.as_dtype(dtype)

    if dtype.is_floating:
        dtype = target

    return dtype
base.py 文件源码 项目:odin 作者: imito 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _check_dtype(dtype):
  if hasattr(dtype, '__call__'):
    return functionable(dtype)
  # ====== check dtype ====== #
  if dtype is None:
    dtype = K.floatX
  elif isinstance(dtype, np.dtype) or is_string(dtype):
    dtype = str(dtype)
  elif isinstance(dtype, VariableDesc):
    dtype = dtype.dtype
  elif isinstance(dtype, tf.DType):
    dtype = dtype.base_dtype.name
  return dtype
nn.py 文件源码 项目:THUMT 作者: thumt 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def layer_norm(inputs, epsilon=1e-6, dtype=None, scope=None):
    """
    Layer Normalization
    :param inputs: A Tensor of shape [..., channel_size]
    :param epsilon: A floating number
    :param dtype: An optional instance of tf.DType
    :param scope: An optional string
    :returns: A Tensor with the same shape as inputs
    """
    with tf.variable_scope(scope, default_name="layer_norm", values=[inputs],
                           dtype=dtype):
        channel_size = inputs.get_shape().as_list()[-1]

        scale = tf.get_variable("scale", shape=[channel_size],
                                initializer=tf.ones_initializer())

        offset = tf.get_variable("offset", shape=[channel_size],
                                 initializer=tf.zeros_initializer())

        mean = tf.reduce_mean(inputs, axis=-1, keep_dims=True)
        variance = tf.reduce_mean(tf.square(inputs - mean), axis=-1,
                                  keep_dims=True)

        norm_inputs = (inputs - mean) * tf.rsqrt(variance + epsilon)

        return norm_inputs * scale + offset
result_types.py 文件源码 项目:fold 作者: tensorflow 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def convert_to_type(type_like):
  """Converts `type_like` to a `Type`.

  If `type_like` is already a `Type`, it is returned. The following
  conversions are performed:

  * Python tuples become `Tuple`s; items are recursively converted.

  * A `tf.TensorShape` becomes a corresponding `TensorType` with
  `dtype=float32`. Must be fully defined.

  * Lists of `shape + [dtype]` (e.g. `[3, 4, 'int32']`) become
  `TensorType`s, with the default `dtype=float32` if omitted.

  * A `tf.Dtype` or stringified version thereof (e.g. `'int64'`)
  becomes a corresponding scalar `TensorType((), dtype)`.

  * An integer `vector_len` becomes a corresponding vector
  `TensorType((vector_len,), dtype=float32)`.

  Args:
    type_like: Described above.

  Returns:
    A `Type`.

  Raises:
    TypeError: If `type_like` cannot be converted to a `Type`.

  """
  if isinstance(type_like, ResultType):
    return type_like
  if isinstance(type_like, tf.TensorShape):
    # Check this *before* calling as_list() otherwise it throws.
    if not type_like.is_fully_defined():
      raise TypeError('shape %s is not fully defined' % type_like)
    return TensorType(type_like.as_list())
  if isinstance(type_like, tuple):
    return TupleType(convert_to_type(item) for item in type_like)
  if isinstance(type_like, list):
    if type_like and isinstance(type_like[-1], six.string_types):
      return TensorType(type_like[:-1], dtype=type_like[-1])
    else:
      return TensorType(type_like)
  if isinstance(type_like, tf.DType) or isinstance(type_like, six.string_types):
    return TensorType((), dtype=type_like)
  if isinstance(type_like, numbers.Integral):
    return TensorType((type_like,))
  raise TypeError('Cannot covert %s to a type.' % (type_like,))
nn.py 文件源码 项目:XMUNMT 作者: XMUNLP 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def linear(inputs, output_size, bias, concat=False, dtype=None, scope=None):
    """
    Linear layer

    Args:
        inputs: A Tensor or a list of Tensors with shape [batch, input_size]
        output_size: An integer specify the output size
        bias: a boolean value indicate whether to use bias term
        concat: a boolean value indicate whether to concatenate all inputs
        dtype: an instance of tf.DType, the default value is ``tf.float32''
        scope: the scope of this layer, the default value is ``linear''

    Returns:
         a Tensor with shape [batch, output_size]

    Raises:
        RuntimeError: raises ``RuntimeError'' when input sizes do not
                      compatible with each other
    """

    with tf.variable_scope(scope, default_name="linear", values=[inputs]):
        if not isinstance(inputs, (list, tuple)):
            inputs = [inputs]

        input_size = [item.get_shape()[-1].value for item in inputs]

        if len(inputs) != len(input_size):
            raise RuntimeError("inputs and input_size unmatched!")

        output_shape = tf.concat([tf.shape(inputs[0])[:-1], [output_size]],
                                 axis=0)
        # Flatten to 2D
        inputs = [tf.reshape(inp, [-1, inp.shape[-1].value]) for inp in inputs]

        results = []

        if concat:
            input_size = sum(input_size)
            inputs = tf.concat(inputs, 1)

            shape = [input_size, output_size]
            matrix = tf.get_variable("matrix", shape, dtype=dtype)
            results.append(tf.matmul(inputs, matrix))
        else:
            for i in range(len(input_size)):
                shape = [input_size[i], output_size]
                name = "matrix_%d" % i
                matrix = tf.get_variable(name, shape, dtype=dtype)
                results.append(tf.matmul(inputs[i], matrix))

        output = tf.add_n(results)

        if bias:
            shape = [output_size]
            bias = tf.get_variable("bias", shape, dtype=dtype)
            output = tf.nn.bias_add(output, bias)

        output = tf.reshape(output, output_shape)

        return output
attention.py 文件源码 项目:XMUNMT 作者: XMUNLP 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def attention(query, memories, bias, hidden_size, cache=None, reuse=None,
              dtype=None, scope=None):
    """ Standard attention layer

    Args:
        query: A tensor with shape [batch, key_size]
        memories: A tensor with shape [batch, memory_size, key_size]
        bias: A tensor with shape [batch, memory_size]
        hidden_size: An integer
        cache: A dictionary of precomputed value
        reuse: A boolean value, whether to reuse the scope
        dtype: An optional instance of tf.DType
        scope: An optional string, the scope of this layer

    Return:
        A tensor with shape [batch, value_size] and a Tensor with
        shape [batch, memory_size]
    """

    with tf.variable_scope(scope or "attention", reuse=reuse,
                           values=[query, memories, bias], dtype=dtype):
        mem_shape = tf.shape(memories)
        key_size = memories.get_shape().as_list()[-1]

        if cache is None:
            k = tf.reshape(memories, [-1, key_size])
            k = linear(k, hidden_size, False, False, scope="k_transform")

            if query is None:
                return {"key": k}
        else:
            k = cache["key"]

        q = linear(query, hidden_size, False, False, scope="q_transform")
        k = tf.reshape(k, [mem_shape[0], mem_shape[1], hidden_size])

        hidden = tf.tanh(q[:, None, :] + k)
        hidden = tf.reshape(hidden, [-1, hidden_size])

        logits = linear(hidden, 1, False, False, scope="logits")
        logits = tf.reshape(logits, [-1, mem_shape[1]])

        if bias is not None:
            logits = logits + bias

        alpha = tf.nn.softmax(logits)

        outputs = {
            "value": tf.reduce_sum(alpha[:, :, None] * memories, axis=1),
            "weight": alpha
        }

    return outputs
utils.py 文件源码 项目:nengo_dl 作者: nengo 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def align_func(output_shape, output_dtype):
    """Decorator that ensures the output of ``func`` is an
    :class:`~numpy:numpy.ndarray` with the given shape and dtype.

    Parameters
    ----------
    output_shape : tuple of int
        Desired shape for function output (must have the same size as actual
        function output)
    output_dtype : ``tf.DType`` or :class:`~numpy:numpy.dtype`
        Desired dtype of function output

    Raises
    ------
    :class:`~nengo:nengo.exceptions.SimulationError`
        If the function returns ``None`` or a non-finite value.
    """

    if isinstance(output_dtype, tf.DType):
        output_dtype = output_dtype.as_numpy_dtype

    def apply_align(func):
        def aligned_func(*args):
            output = func(*args)

            if output is None:
                raise SimulationError(
                    "Function %r returned None" %
                    function_name(func, sanitize=False))
            try:
                if not np.all(np.isfinite(output)):
                    raise SimulationError(
                        "Function %r returned invalid value %r" %
                        (function_name(func, sanitize=False), output))
            except (TypeError, ValueError):
                raise SimulationError(
                    "Function %r returned a value %r of invalid type %r" %
                    (function_name(func, sanitize=False), output,
                     type(output)))
            output = np.asarray(output, dtype=output_dtype)
            output = output.reshape(output_shape)
            return output

        return aligned_func

    return apply_align
nn.py 文件源码 项目:THUMT 作者: thumt 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def linear(inputs, output_size, bias, concat=True, dtype=None, scope=None):
    """
    Linear layer
    :param inputs: A Tensor or a list of Tensors with shape [batch, input_size]
    :param output_size: An integer specify the output size
    :param bias: a boolean value indicate whether to use bias term
    :param concat: a boolean value indicate whether to concatenate all inputs
    :param dtype: an instance of tf.DType, the default value is ``tf.float32''
    :param scope: the scope of this layer, the default value is ``linear''
    :returns: a Tensor with shape [batch, output_size]
    :raises RuntimeError: raises ``RuntimeError'' when input sizes do not
                          compatible with each other
    """

    with tf.variable_scope(scope, default_name="linear", values=[inputs]):
        if not isinstance(inputs, (list, tuple)):
            inputs = [inputs]

        input_size = [item.get_shape()[-1].value for item in inputs]

        if len(inputs) != len(input_size):
            raise RuntimeError("inputs and input_size unmatched!")

        output_shape = tf.concat([tf.shape(inputs[0])[:-1], [output_size]],
                                 axis=0)
        # Flatten to 2D
        inputs = [tf.reshape(inp, [-1, inp.shape[-1].value]) for inp in inputs]

        results = []

        if concat:
            input_size = sum(input_size)
            inputs = tf.concat(inputs, 1)

            shape = [input_size, output_size]
            matrix = tf.get_variable("matrix", shape, dtype=dtype)
            results.append(tf.matmul(inputs, matrix))
        else:
            for i in range(len(input_size)):
                shape = [input_size[i], output_size]
                name = "matrix_%d" % i
                matrix = tf.get_variable(name, shape, dtype=dtype)
                results.append(tf.matmul(inputs[i], matrix))

        output = tf.add_n(results)

        if bias:
            shape = [output_size]
            bias = tf.get_variable("bias", shape, dtype=dtype)
            output = tf.nn.bias_add(output, bias)

        output = tf.reshape(output, output_shape)

        return output
attention.py 文件源码 项目:THUMT 作者: thumt 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def attention(query, memories, bias, hidden_size, cache=None, reuse=None,
              dtype=None, scope=None):
    """ Standard attention layer

    :param query: A tensor with shape [batch, key_size]
    :param memories: A tensor with shape [batch, memory_size, key_size]
    :param bias: A tensor with shape [batch, memory_size]
    :param hidden_size: An integer
    :param cache: A dictionary of precomputed value
    :param reuse: A boolean value, whether to reuse the scope
    :param dtype: An optional instance of tf.DType
    :param scope: An optional string, the scope of this layer
    :return: A tensor with shape [batch, value_size] and
        a Tensor with shape [batch, memory_size]
    """

    with tf.variable_scope(scope or "attention", reuse=reuse,
                           values=[query, memories, bias], dtype=dtype):
        mem_shape = tf.shape(memories)
        key_size = memories.get_shape().as_list()[-1]

        if cache is None:
            k = tf.reshape(memories, [-1, key_size])
            k = linear(k, hidden_size, False, False, scope="k_transform")

            if query is None:
                return {"key": k}
        else:
            k = cache["key"]

        q = linear(query, hidden_size, False, False, scope="q_transform")
        k = tf.reshape(k, [mem_shape[0], mem_shape[1], hidden_size])

        hidden = tf.tanh(q[:, None, :] + k)
        hidden = tf.reshape(hidden, [-1, hidden_size])

        # Shape: [batch, mem_size, 1]
        logits = linear(hidden, 1, False, False, scope="logits")
        logits = tf.reshape(logits, [-1, mem_shape[1]])

        if bias is not None:
            logits = logits + bias

        alpha = tf.nn.softmax(logits)

        outputs = {
            "value": tf.reduce_sum(alpha[:, :, None] * memories, axis=1),
            "weight": alpha
        }

    return outputs
attention.py 文件源码 项目:THUMT 作者: thumt 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def additive_attention(queries, keys, values, bias, hidden_size, concat=False,
                       keep_prob=None, dtype=None, scope=None):
    """ Additive attention mechanism. This layer is implemented using a
        one layer feed forward neural network

    :param queries: A tensor with shape [batch, heads, length_q, depth_k]
    :param keys: A tensor with shape [batch, heads, length_kv, depth_k]
    :param values: A tensor with shape [batch, heads, length_kv, depth_v]
    :param bias: A tensor
    :param hidden_size: An integer
    :param concat: A boolean value. If ``concat'' is set to True, then
        the computation of attention mechanism is following $tanh(W[q, k])$.
        When ``concat'' is set to False, the computation is following
        $tanh(Wq + Vk)$
    :param keep_prob: a scalar in [0, 1]
    :param dtype: An optional instance of tf.DType
    :param scope: An optional string, the scope of this layer

    :returns: A dict with the following keys:
        weights: A tensor with shape [batch, length_q]
        outputs: A tensor with shape [batch, length_q, depth_v]
    """

    with tf.variable_scope(scope, default_name="additive_attention",
                           values=[queries, keys, values, bias], dtype=dtype):
        length_q = tf.shape(queries)[2]
        length_kv = tf.shape(keys)[2]
        q = tf.tile(tf.expand_dims(queries, 3), [1, 1, 1, length_kv, 1])
        k = tf.tile(tf.expand_dims(keys, 2), [1, 1, length_q, 1, 1])

        if concat:
            combined = tf.tanh(linear(tf.concat([q, k], axis=-1), hidden_size,
                                      True, True, name="qk_transform"))
        else:
            q = linear(queries, hidden_size, True, True, name="q_transform")
            k = linear(keys, hidden_size, True, True, name="key_transform")
            combined = tf.tanh(q + k)

        # shape: [batch, heads, length_q, length_kv]
        logits = tf.squeeze(linear(combined, 1, True, True, name="logits"),
                            axis=-1)

        if bias is not None:
            logits += bias

        weights = tf.nn.softmax(logits, name="attention_weights")

        if keep_prob or keep_prob < 1.0:
            weights = tf.nn.dropout(weights, keep_prob)

        outputs = tf.matmul(weights, values)

        return {"weights": weights, "outputs": outputs}
dataset_schema.py 文件源码 项目:transform 作者: tensorflow 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def __init__(self, dtype, min_value=None, max_value=None,
               is_categorical=None, vocabulary_file=''):
    super(IntDomain, self).__init__(dtype)
    if not self.dtype.is_integer:
      raise ValueError('IntDomain must be initialized with an integral dtype.')
    # NOTE: Because there is no uint64 or 128 bit ints, the following values
    # are always in the int64 range, which is important for the proto
    # representation.
    self._min_value = min_value if min_value is not None else self.dtype.min
    self._max_value = max_value if max_value is not None else self.dtype.max
    # Parsing a non-existing value from JSON will return None make sure it is
    # translated to False.
    self._is_categorical = (is_categorical
                            if is_categorical is not None
                            else False)
    self._vocabulary_file = vocabulary_file
dataset_schema.py 文件源码 项目:transform 作者: tensorflow 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def as_feature_spec(self, column):
    ind = self.index_fields
    if len(ind) != 1 or len(column.axes) != 1:
      raise ValueError('tf.Example parser supports only 1-d sparse features.')
    index = ind[0]

    if column.domain.dtype not in _TF_EXAMPLE_ALLOWED_TYPES:
      raise ValueError('tf.Example parser supports only types {}, so it is '
                       'invalid to generate a feature_spec with type '
                       '{}.'.format(
                           _TF_EXAMPLE_ALLOWED_TYPES,
                           repr(column.domain.dtype)))

    return tf.SparseFeature(index.name,
                            self._value_field_name,
                            column.domain.dtype,
                            column.axes[0].size,
                            index.is_sorted)
util.py 文件源码 项目:tefla 作者: litan 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def get_bbox_10crop(crop_size, im_size):
    im_center = im_size[:2] / 2.0
    h_indices = (0, (im_size[0] - crop_size[0]) / 2.0)
    w_indices = (0, (im_size[1] - crop_size[1]) / 2.0)
    bboxs = np.empty((5, 5), dtype=np.int32)
    curr = 0
    for i in h_indices:
        for j in w_indices:
            bboxs[curr, :4] = (i, j, i + crop_size[0], j + crop_size[1])
            bboxs[curr, 4] = 1
            curr += 1
    bboxs[4, :4] = np.tile(im_center, (1, 2)) + np.concatenate([-crop_size / 2.0, crop_size / 2.0])
    bboxs[4, 4] = 1
    bboxs = np.tile(bboxs, (2, 1))
    bboxs[5:, 4] = 0

    return bboxs
tensor_ops.py 文件源码 项目:hart 作者: akosiorek 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _bbox_to_mask(yy, region_size, dtype):
    # trim bounding box exeeding region_size on top and left
    neg_part = tf.nn.relu(-yy[:2])
    core = tf.ones(tf.to_int32(tf.round(yy[2:] - neg_part)), dtype=dtype)

    y1 = tf.maximum(yy[0], 0.)
    x1 = tf.maximum(yy[1], 0.)

    y2 = tf.minimum(region_size[0], yy[0] + yy[2])
    x2 = tf.minimum(region_size[1], yy[1] + yy[3])

    padding = (y1, region_size[0] - y2, x1, region_size[1] - x2)
    padding = tf.reshape(tf.stack(padding), (-1, 2))
    padding = tf.to_int32(tf.round(padding))
    mask = tf.pad(core, padding)

    # trim bounding box exeeding region_size on bottom and right
    rs = tf.to_int32(tf.round(region_size))
    mask = mask[:rs[0], :rs[1]]
    mask.set_shape((None, None))
    return mask
tensor_ops.py 文件源码 项目:hart 作者: akosiorek 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def bbox_to_mask(bbox, region_size, output_size, dtype=tf.float32):
    """Creates a binary mask of size `region_size` where rectangle given by
    `bbox` is filled with ones and the rest is zeros. Finally, the binary mask
    is resized to `output_size` with bilinear interpolation.

    :param bbox: tensor of shape (..., 4)
    :param region_size: tensor of shape (..., 2)
    :param output_size: 2-tuple of ints
    :param dtype: tf.dtype
    :return: a tensor of shape = (..., output_size)
    """
    shape = tf.concat(axis=0, values=(tf.shape(bbox)[:-1], output_size))
    bbox = tf.reshape(bbox, (-1, 4))
    region_size = tf.reshape(region_size, (-1, 2))

    def create_mask(args):
        yy, region_size = args
        return _bbox_to_mask_fixed_size(yy, region_size, output_size, dtype)

    mask = tf.map_fn(create_mask, (bbox, region_size), dtype=dtype)
    return tf.reshape(mask, shape)
util.py 文件源码 项目:tefla 作者: openAGI 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def get_bbox_10crop(crop_size, im_size):
    im_center = im_size[:2] / 2.0
    h_indices = (0, (im_size[0] - crop_size[0]) / 2.0)
    w_indices = (0, (im_size[1] - crop_size[1]) / 2.0)
    bboxs = np.empty((5, 5), dtype=np.int32)
    curr = 0
    for i in h_indices:
        for j in w_indices:
            bboxs[curr, :4] = (i, j, i + crop_size[0], j + crop_size[1])
            bboxs[curr, 4] = 1
            curr += 1
    bboxs[4, :4] = np.tile(im_center, (1, 2)) + \
        np.concatenate([-crop_size / 2.0, crop_size / 2.0])
    bboxs[4, 4] = 1
    bboxs = np.tile(bboxs, (2, 1))
    bboxs[5:, 4] = 0

    return bboxs
util.py 文件源码 项目:tefla 作者: openAGI 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def one_hot(labels, num_classes, name='one_hot'):
    """Transform numeric labels into onehot_labels.
    Args:
        labels: [batch_size] target labels.
        num_classes: total number of classes.
        scope: Optional scope for op_scope.
    Returns:
        one hot encoding of the labels.
    """
    with tf.op_scope(name):
        batch_size = labels.get_shape()[0]
        indices = tf.expand_dims(tf.range(0, batch_size), 1)
        labels = tf.cast(tf.expand_dims(labels, 1), indices.dtype)
        concated = tf.concat(1, [indices, labels])
        onehot_labels = tf.sparse_to_dense(
            concated, tf.pack([batch_size, num_classes]), 1.0, 0.0)
        onehot_labels.set_shape([batch_size, num_classes])
        return onehot_labels
tf_util.py 文件源码 项目:distributional_perspective_on_RL 作者: Kiwoo 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def create_net(self, shape):
        print "Creat Net"
        self.x = tf.placeholder(shape=[None, shape], name="x", dtype=tf.float32)
        self.y = tf.placeholder(shape=[None], name="y", dtype=tf.float32)

        out = layers.fully_connected(self.x, num_outputs=5, activation_fn=tf.nn.relu, weights_initializer=tf.contrib.layers.xavier_initializer())
        out = layers.fully_connected(out, num_outputs=3, activation_fn=tf.nn.relu, weights_initializer=tf.contrib.layers.xavier_initializer())
        self.net = layers.fully_connected(out, num_outputs=1, activation_fn=None, weights_initializer=tf.contrib.layers.xavier_initializer())
        self.net = tf.reshape(self.net, (-1, ))
        l2 = (self.net - self.y) * (self.net - self.y)
        self.train = tf.train.AdamOptimizer(1e-4).minimize(l2)
        tf.global_variables_initializer().run()
tf_util.py 文件源码 项目:distributional_perspective_on_RL 作者: Kiwoo 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __init__(self, shape, dtype=tf.float32, name=None):
        """Creates a placeholder for a batch of tensors of a given shape and dtype

        Parameters
        ----------
        shape: [int]
            shape of a single elemenet of the batch
        dtype: tf.dtype
            number representation used for tensor contents
        name: str
            name of the underlying placeholder
        """
        print "C1"
        super(BatchInput, self).__init__(tf.placeholder(dtype, [None] + list(shape), name=name))
tf_util.py 文件源码 项目:distributional_perspective_on_RL 作者: Kiwoo 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def normc_initializer(std=1.0):
    def _initializer(shape, dtype=None, partition_info=None): #pylint: disable=W0613
        out = np.random.randn(*shape).astype(np.float32)
        out *= std / np.sqrt(np.square(out).sum(axis=0, keepdims=True))
        return tf.constant(out)
    return _initializer
tf_util.py 文件源码 项目:distributional_perspective_on_RL 作者: Kiwoo 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def conv2d(x, num_filters, name, filter_size=(3, 3), stride=(1, 1), pad="SAME", dtype=tf.float32, collections=None,
           summary_tag=None):
    with tf.variable_scope(name):
        stride_shape = [1, stride[0], stride[1], 1]
        filter_shape = [filter_size[0], filter_size[1], int(x.get_shape()[3]), num_filters]

        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = intprod(filter_shape[:3])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width" /
        #   pooling size
        fan_out = intprod(filter_shape[:2]) * num_filters
        # initialize weights with random weights
        w_bound = np.sqrt(6. / (fan_in + fan_out))

        w = tf.get_variable("W", filter_shape, dtype, tf.random_uniform_initializer(-w_bound, w_bound),
                            collections=collections)
        b = tf.get_variable("b", [1, 1, 1, num_filters], initializer=tf.zeros_initializer(),
                            collections=collections)

        if summary_tag is not None:
            tf.summary.image(summary_tag,
                             tf.transpose(tf.reshape(w, [filter_size[0], filter_size[1], -1, 1]),
                                          [2, 0, 1, 3]),
                             max_images=10)

        return tf.nn.conv2d(x, w, stride_shape, pad) + b
tf_util.py 文件源码 项目:distributional_perspective_on_RL 作者: Kiwoo 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self, var_list, dtype=tf.float32):
        assigns = []
        shapes = list(map(var_shape, var_list))
        total_size = np.sum([intprod(shape) for shape in shapes])

        self.theta = theta = tf.placeholder(dtype,[total_size])
        start=0
        assigns = []
        for (shape,v) in zip(shapes,var_list):
            size = intprod(shape)
            assigns.append(tf.assign(v, tf.reshape(theta[start:start+size],shape)))
            start+=size
        self.op = tf.group(*assigns)
dataset_schema.py 文件源码 项目:transform 作者: tensorflow 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self, dtype):
    self._dtype = tf.as_dtype(dtype)


问题


面经


文章

微信
公众号

扫码关注公众号