python类cos()的实例源码

relative_trafo.py 文件源码 项目:hand3d 作者: lmb-freiburg 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _get_rot_mat_y_hom(angle):
    """ Returns a 3D rotation matrix in homogeneous coords.  """
    one_vec = tf.ones_like(angle)
    zero_vec = one_vec*0.0
    trafo_matrix = _stitch_mat_from_vecs([tf.cos(angle), zero_vec, tf.sin(angle), zero_vec,
                                          zero_vec, one_vec, zero_vec, zero_vec,
                                          -tf.sin(angle), zero_vec, tf.cos(angle), zero_vec,
                                          zero_vec, zero_vec, zero_vec, one_vec])
    return trafo_matrix
relative_trafo.py 文件源码 项目:hand3d 作者: lmb-freiburg 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _get_rot_mat_z_hom(angle):
    """ Returns a 3D rotation matrix in homogeneous coords. """
    one_vec = tf.ones_like(angle)
    zero_vec = one_vec*0.0
    trafo_matrix = _stitch_mat_from_vecs([tf.cos(angle), -tf.sin(angle), zero_vec, zero_vec,
                                          tf.sin(angle), tf.cos(angle), zero_vec, zero_vec,
                                          zero_vec, zero_vec, one_vec, zero_vec,
                                          zero_vec, zero_vec, zero_vec, one_vec])
    return trafo_matrix
canonical_trafo.py 文件源码 项目:hand3d 作者: lmb-freiburg 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _get_rot_mat_y(angle):
    """ Returns a 3D rotation matrix. """
    one_vec = tf.ones_like(angle)
    zero_vec = one_vec*0.0
    trafo_matrix = _stitch_mat_from_vecs([tf.cos(angle), zero_vec, -tf.sin(angle),
                                          zero_vec, one_vec, zero_vec,
                                          tf.sin(angle), zero_vec, tf.cos(angle)])
    return trafo_matrix
canonical_trafo.py 文件源码 项目:hand3d 作者: lmb-freiburg 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _get_rot_mat_z(angle):
    """ Returns a 3D rotation matrix. """
    one_vec = tf.ones_like(angle)
    zero_vec = one_vec*0.0
    trafo_matrix = _stitch_mat_from_vecs([tf.cos(angle), tf.sin(angle), zero_vec,
                                          -tf.sin(angle), tf.cos(angle), zero_vec,
                                          zero_vec, zero_vec, one_vec])
    return trafo_matrix
buildRotations.py 文件源码 项目:dizzy_layer 作者: Pastromhaug 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def buildRotations(n, rand_or_identity,num_rots=None):
    print("num_rots: %d" %num_rots)
    num_rots = num_rots or (n-1)
    n_prime = int(n*(n-1)//2*num_rots/(n-1))
    outputs = []

    with vs.variable_scope("Build_Rotations"):

        (indices, values_idxs) = rotationPreprocess(n, num_rots)
        if rand_or_identity:
            print("Initialization: Random")
            thetas = vs.get_variable(initializer=tf.random_uniform([n_prime, 1], 0, 2*math.pi),
                    name="Thetas_RandInit", dtype=tf.float32)
        else:
            print("Initialization: Identity")
            thetas = vs.get_variable(initializer=tf.zeros([n_prime, 1]),
                    name="Thetas_OnesInit", dtype=tf.float32)
        cos = tf.cos(thetas)
        sin = tf.sin(thetas)
        nsin = tf.neg(sin)

        thetas_concat = tf.concat(0, [cos,sin,nsin])

        gathered_values = tf.squeeze(tf.gather(thetas_concat, values_idxs))
        shape = tf.constant([n, n], dtype=tf.int64)

        splt_values = tf.split(0, num_rots, gathered_values)
        splt_indices = tf.split(0, num_rots, indices)

        shape = tf.constant([n,n], dtype=tf.int64)
        for i in range(num_rots):
            curr_indices = splt_indices[i]
            curr_values = splt_values[i]
            sparse_rot = tf.SparseTensor(indices=curr_indices, values=curr_values, shape=shape)
            outputs.append(sparse_rot)
    print("buildRotations output length: %d" % len(outputs))
    return outputs
rotationTransform.py 文件源码 项目:dizzy_layer 作者: Pastromhaug 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def rotationTransform(X, n, scope, num_rots=None):
    num_rots = num_rots or (n-1)
    n_prime = int(n*(n-1)//2*num_rots/(n-1))
    outputs = []

    with vs.variable_scope(scope or "RotationTransform"):

        for i, (name, x) in enumerate(X):
            (indices, values_idxs) = rotationPreprocess(n, num_rots)
            thetas = vs.get_variable(initializer=tf.random_uniform([n_prime, 1], 0, 2*math.pi),
                    name="Thetas"+str(i)+name, dtype=tf.float32)

            cos = tf.cos(thetas)
            sin = tf.sin(thetas)
            nsin = tf.neg(sin)

            thetas_concat = tf.concat(0, [cos,sin,nsin])

            gathered_values = tf.squeeze(tf.gather(thetas_concat, values_idxs))
            shape = tf.constant([n, n], dtype=tf.int64)

            splt_values = tf.split(0, num_rots, gathered_values)
            splt_indices = tf.split(0, num_rots, indices)

            shape = tf.constant([n,n], dtype=tf.int64)
            for i in range(num_rots):
                curr_indices = splt_indices[i]
                curr_values = splt_values[i]
                sparse_rot = tf.SparseTensor(indices=curr_indices, values=curr_values, shape=shape)
                x = tf.sparse_tensor_dense_matmul(sparse_rot, x)
            outputs.append(x)
    return outputs
spherical.py 文件源码 项目:monodepth360 作者: srijanparmeshwar 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def lat_long_to_xyz(S, T):
    x = tf.cos(T) * tf.sin(S)
    y = tf.sin(T)
    z = tf.cos(T) * tf.cos(S)
    return x, y, z
spherical.py 文件源码 项目:monodepth360 作者: srijanparmeshwar 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def backproject(S, T, depth):
    # Convert to Cartesian for modified depth input.
    # depth = sqrt(x^2 + z^2).
    x = depth * tf.sin(S)
    y = depth * tf.tan(T)
    z = depth * tf.cos(S)
    return x, y, z
attention.py 文件源码 项目:THUMT 作者: thumt 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def add_timing_signal(x, min_timescale=1.0, max_timescale=1.0e4, name=None):
    """
    This function adds a bunch of sinusoids of different frequencies to a
    Tensor. See paper: Attention is all you need

    :param x: A tensor with shape [batch, length, channels]
    :param min_timescale: A floating point number
    :param max_timescale: A floating point number
    :param name: An optional string

    :returns: a Tensor the same shape as x.
    """

    with tf.name_scope(name, default_name="add_timing_signal", values=[x]):
        length = tf.shape(x)[1]
        channels = tf.shape(x)[2]
        position = tf.to_float(tf.range(length))
        num_timescales = channels // 2

        log_timescale_increment = (
            math.log(float(max_timescale) / float(min_timescale)) /
            (tf.to_float(num_timescales) - 1)
        )
        inv_timescales = min_timescale * tf.exp(
            tf.to_float(tf.range(num_timescales)) * -log_timescale_increment
        )

        scaled_time = (tf.expand_dims(position, 1) *
                       tf.expand_dims(inv_timescales, 0))
        signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=1)
        signal = tf.pad(signal, [[0, 0], [0, tf.mod(channels, 2)]])
        signal = tf.reshape(signal, [1, length, channels])

        return x + signal
tensorflow_backend.py 文件源码 项目:keras 作者: NVIDIA 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def cos(x):
    """Computes cos of x element-wise.

    # Arguments
        x: input tensor.

    # Returns
        A tensor.
    """
    return tf.cos(x)
tensorflow_backend.py 文件源码 项目:keras_superpixel_pooling 作者: parag2489 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def cos(x):
    """Computes cos of x element-wise.

    # Arguments
        x: Tensor or variable.

    # Returns
        A tensor.
    """
    return tf.cos(x)
common_layers.py 文件源码 项目:tensor2tensor 作者: tensorflow 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def add_timing_signal(x, min_timescale=1, max_timescale=1e4, num_timescales=16):
  """Adds a bunch of sinusoids of different frequencies to a Tensor.

  This allows attention to learn to use absolute and relative positions.
  The timing signal should be added to some precursor of both the source
  and the target of the attention.

  The use of relative position is possible because sin(x+y) and cos(x+y) can be
  experessed in terms of y, sin(x) and cos(x).

  In particular, we use a geometric sequence of timescales starting with
  min_timescale and ending with max_timescale.  For each timescale, we
  generate the two sinusoidal signals sin(timestep/timescale) and
  cos(timestep/timescale).  All of these sinusoids are concatenated in
  the depth dimension, padded with zeros to be the same depth as the input,
  and added into input.

  Args:
    x: a Tensor with shape [?, length, ?, depth]
    min_timescale: a float
    max_timescale: a float
    num_timescales: an int <= depth/2

  Returns:
    a Tensor the same shape as x.
  """
  length = shape_list(x)[1]
  depth = shape_list(x)[3]
  signal = get_timing_signal(length, min_timescale, max_timescale,
                             num_timescales)
  padded_signal = tf.pad(signal, [[0, 0], [0, depth - 2 * num_timescales]])
  return x + tf.reshape(padded_signal, [1, length, 1, depth])
common_attention.py 文件源码 项目:tensor2tensor 作者: tensorflow 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
  """Adds a bunch of sinusoids of different frequencies to a Tensor.

  Each channel of the input Tensor is incremented by a sinusoid of a different
  frequency and phase.

  This allows attention to learn to use absolute and relative positions.
  Timing signals should be added to some precursors of both the query and the
  memory inputs to attention.

  The use of relative position is possible because sin(x+y) and cos(x+y) can be
  experessed in terms of y, sin(x) and cos(x).

  In particular, we use a geometric sequence of timescales starting with
  min_timescale and ending with max_timescale.  The number of different
  timescales is equal to channels / 2. For each timescale, we
  generate the two sinusoidal signals sin(timestep/timescale) and
  cos(timestep/timescale).  All of these sinusoids are concatenated in
  the channels dimension.

  Args:
    x: a Tensor with shape [batch, length, channels]
    min_timescale: a float
    max_timescale: a float

  Returns:
    a Tensor the same shape as x.
  """
  length = common_layers.shape_list(x)[1]
  channels = common_layers.shape_list(x)[2]
  signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
  return x + signal
common_attention.py 文件源码 项目:tensor2tensor 作者: tensorflow 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def add_timing_signal_1d_given_position(x,
                                        position,
                                        min_timescale=1.0,
                                        max_timescale=1.0e4):
  """Adds sinusoids of diff frequencies to a Tensor, with timing position given.

  Args:
    x: a Tensor with shape [batch, length, channels]
    position: a Tensor with shape [batch, length]
    min_timescale: a float
    max_timescale: a float

  Returns:
    a Tensor the same shape as x.
  """
  channels = common_layers.shape_list(x)[2]
  num_timescales = channels // 2
  log_timescale_increment = (
      math.log(float(max_timescale) / float(min_timescale)) /
      (tf.to_float(num_timescales) - 1))
  inv_timescales = min_timescale * tf.exp(
      tf.to_float(tf.range(num_timescales)) * -log_timescale_increment)
  scaled_time = (
      tf.expand_dims(tf.to_float(position), 2) * tf.expand_dims(
          tf.expand_dims(inv_timescales, 0), 0))
  signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=2)
  signal = tf.pad(signal, [[0, 0], [0, 0], [0, tf.mod(channels, 2)]])
  return x + signal
optimize.py 文件源码 项目:tensor2tensor 作者: tensorflow 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
def learning_rate_decay(hparams, num_worker_replicas=1, num_train_steps=1):
  """Inverse-decay learning rate until warmup_steps, then decay."""
  warmup_steps = tf.to_float(
      hparams.learning_rate_warmup_steps * num_worker_replicas)
  step = tf.to_float(tf.train.get_or_create_global_step())
  if hparams.learning_rate_decay_scheme == "noam":
    return 5000.0 * hparams.hidden_size**-0.5 * tf.minimum(
        (step + 1) * warmup_steps**-1.5, (step + 1)**-0.5)
  elif hparams.learning_rate_decay_scheme == "exp100k":
    return 0.94**(step // 100000)
  elif hparams.learning_rate_decay_scheme == "cosine":
    cycle_steps = hparams.learning_rate_cosine_cycle_steps
    return 0.5 * (1 + tf.cos(np.pi * (step % cycle_steps) / cycle_steps))
  elif hparams.learning_rate_decay_scheme == "cyclelinear10x":
    # Cycle the rate linearly by 10x every warmup_steps, up and down.
    cycle_steps = hparams.learning_rate_warmup_steps
    cycle_position = step % (2 * cycle_steps)
    cycle_position = tf.to_float(  # Normalize to the interval [-1, 1].
        cycle_position - cycle_steps) / float(cycle_steps)
    cycle_position = 1.0 - tf.abs(cycle_position)  # 0 to 1 and back to 0.
    return (cycle_position + 0.1) * 3.0  # 10x difference each cycle (0.3-3).

  inv_base = tf.exp(tf.log(0.01) / warmup_steps)
  inv_decay = inv_base**(warmup_steps - step)
  if hparams.learning_rate_decay_scheme == "sqrt":
    decay = _sqrt_decay(step - warmup_steps)
  elif hparams.learning_rate_decay_scheme == "exp10k":
    decay = _exp_decay_after(step - warmup_steps, 0.9995,
                             num_train_steps - warmup_steps - 10000)
  elif hparams.learning_rate_decay_scheme == "exp50k":
    decay = _exp_decay_after(step - warmup_steps, 0.99995,
                             num_train_steps - warmup_steps - 50000)
  elif hparams.learning_rate_decay_scheme == "exp500k":
    decay = _exp_decay_after(step - warmup_steps, 0.9999955,
                             num_train_steps - warmup_steps - 500000)
  elif hparams.learning_rate_decay_scheme == "none":
    decay = tf.constant(1.0)
  else:
    raise ValueError("Unrecognized learning rate decay scheme: %s" %
                     hparams.learning_rate_decay_scheme)
  return tf.where(step < warmup_steps, inv_decay, decay)
tensorflow_backend.py 文件源码 项目:InnerOuterRNN 作者: Chemoinformatics 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def cos(x):
    '''Computes cos of x element-wise.
    '''
    return tf.cos(x)
ops.py 文件源码 项目:tfdeploy 作者: riga 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def test_Cos(self):
        t = tf.cos(self.random(4, 3))
        self.check(t)
effects.py 文件源码 项目:py-noisemaker 作者: aayars 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def ripple(tensor, shape, freq, displacement=1.0, kink=1.0, reference=None, spline_order=3):
    """
    Apply displacement from pixel radian values.

    :param Tensor tensor: An image tensor.
    :param list[int] shape:
    :param list[int] freq: Displacement frequency
    :param float displacement:
    :param float kink:
    :param Tensor reference: An optional displacement map.
    :param int spline_order: Ortho offset spline point count. 0=Constant, 1=Linear, 2=Cosine, 3=Bicubic
    :return: Tensor
    """

    height, width, channels = shape

    x0_index = row_index(shape)
    y0_index = column_index(shape)

    value_shape = [shape[0], shape[1], 1]

    if reference is None:
        reference = resample(tf.random_uniform([freq[0], freq[1], 1]), value_shape, spline_order=spline_order)
        # reference = derivative(reference, [shape[0], shape[1], 1], with_normalize=False)

    # Twist index, borrowed from worms. TODO merge me.
    index = value_map(reference, shape) * 360.0 * math.radians(1) * kink

    reference_x = (tf.cos(index) * displacement * width) % width
    reference_y = (tf.sin(index) * displacement * height) % height

    # Bilinear interpolation of midpoints, borrowed from refract(). TODO merge me
    x0_offsets = (tf.cast(reference_x, tf.int32) + x0_index) % width
    x1_offsets = (x0_offsets + 1) % width
    y0_offsets = (tf.cast(reference_y, tf.int32) + y0_index) % height
    y1_offsets = (y0_offsets + 1) % height

    x0_y0 = tf.gather_nd(tensor, tf.stack([y0_offsets, x0_offsets], 2))
    x1_y0 = tf.gather_nd(tensor, tf.stack([y0_offsets, x1_offsets], 2))
    x0_y1 = tf.gather_nd(tensor, tf.stack([y1_offsets, x0_offsets], 2))
    x1_y1 = tf.gather_nd(tensor, tf.stack([y1_offsets, x1_offsets], 2))

    x_fract = tf.reshape(reference_x - tf.floor(reference_x), [height, width, 1])
    y_fract = tf.reshape(reference_y - tf.floor(reference_y), [height, width, 1])

    x_y0 = blend(x0_y0, x1_y0, x_fract)
    x_y1 = blend(x0_y1, x1_y1, x_fract)

    return blend(x_y0, x_y1, y_fract)
spherical.py 文件源码 项目:monodepth360 作者: srijanparmeshwar 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def lat_long_to_rectilinear_uv(K, S, T):
    # Convert to Cartesian.
    x = tf.cos(T) * tf.sin(S)
    y = tf.sin(T)
    z = tf.cos(T) * tf.cos(S)

    argmax = tf.argmax(tf.abs([x, y, z]), axis = 0)

    # Check which face the ray lies on.
    front_check = tf.logical_and(
        tf.equal(argmax, 2),
        tf.greater_equal(z, 0.0)
    )
    back_check = tf.logical_and(
        tf.equal(argmax, 2),
        tf.less(z, 0.0)
    )
    left_check = tf.logical_and(
        tf.equal(argmax, 0),
        tf.less(x, 0.0)
    )
    right_check = tf.logical_and(
        tf.equal(argmax, 0),
        tf.greater_equal(x, 0.0)
    )
    up_check = tf.logical_and(
        tf.equal(argmax, 1),
        tf.less(y, 0.0)
    )
    down_check = tf.logical_and(
        tf.equal(argmax, 1),
        tf.greater_equal(y, 0.0)
    )

    def project_u(x, y, z, offset):
        return offset + 0.5 + (K[2] + K[0] * x / z) / 2.0

    def project_v(x, y, z):
        return 0.5 + (K[3] + K[1] * y / z) / 2.0

    # Calculate UV coordinates.
    u = tf.where(front_check, project_u(x, y, z, 0.0), tf.zeros_like(x))
    u = tf.where(back_check, project_u(x, -y, z, 1.0), u)
    u = tf.where(left_check, project_u(z, y, -x, 2.0), u)
    u = tf.where(right_check, project_u(-z, y, x, 3.0), u)
    u = tf.where(up_check, project_u(x, z, -y, 4.0), u)
    u = tf.where(down_check, project_u(x, -z, y, 5.0), u)
    u = u / 6.0

    v = tf.where(front_check, project_v(x, y, z), tf.zeros_like(y))
    v = tf.where(back_check, project_v(x, -y, z), v)
    v = tf.where(left_check, project_v(z, y, -x), v)
    v = tf.where(right_check, project_v(-z, y, x), v)
    v = tf.where(up_check, project_v(x, z, -y), v)
    v = tf.where(down_check, project_v(x, -z, y), v)

    return u, v
spherical.py 文件源码 项目:monodepth360 作者: srijanparmeshwar 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def lat_long_to_cube_uv(S, T):
    # Convert to Cartesian.
    x = tf.cos(T) * tf.sin(S)
    y = tf.sin(T)
    z = tf.cos(T) * tf.cos(S)

    argmax = tf.argmax(tf.abs([x, y, z]), axis = 0)
    max = tf.reduce_max(tf.abs([x, y, z]), axis = 0)

    # Check which face the ray lies on.
    front_check = tf.logical_and(
        tf.equal(argmax, 2),
        tf.greater_equal(z, 0.0)
    )
    back_check = tf.logical_and(
        tf.equal(argmax, 2),
        tf.less(z, 0.0)
    )
    left_check = tf.logical_and(
        tf.equal(argmax, 0),
        tf.less(x, 0.0)
    )
    right_check = tf.logical_and(
        tf.equal(argmax, 0),
        tf.greater_equal(x, 0.0)
    )
    up_check = tf.logical_and(
        tf.equal(argmax, 1),
        tf.less(y, 0.0)
    )
    down_check = tf.logical_and(
        tf.equal(argmax, 1),
        tf.greater_equal(y, 0.0)
    )

    # Normalize coordinates.
    x = x / max
    y = y / max
    z = z / max

    # Calculate UV coordinates.
    u = tf.where(front_check, 0.5 + x / 2.0, tf.zeros_like(x))
    u = tf.where(back_check, 1.0 + (0.5 - x / 2.0), u)
    u = tf.where(left_check, 2.0 + (0.5 + z / 2.0), u)
    u = tf.where(right_check, 3.0 + (0.5 - z / 2.0), u)
    u = tf.where(up_check, 4.0 + (0.5 + x / 2.0), u)
    u = tf.where(down_check, 5.0 + (0.5 + x / 2.0), u)
    u = u / 6.0

    v = tf.where(front_check, (1.0 + y) / 2.0, tf.zeros_like(y))
    v = tf.where(back_check, (1.0 + y) / 2.0, v)
    v = tf.where(left_check, (1.0 + y) / 2.0, v)
    v = tf.where(right_check, (1.0 + y) / 2.0, v)
    v = tf.where(up_check, (1.0 + z) / 2.0, v)
    v = tf.where(down_check, (1.0 - z) / 2.0, v)

    return u, v


问题


面经


文章

微信
公众号

扫码关注公众号