本文整理汇总了Python中keras.backend.cos方法的典型用法代码示例。如果您正苦于以下问题:Python backend.cos方法的具体用法?Python backend.cos怎么用?Python backend.cos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.cos方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: euler2rot_mat
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def euler2rot_mat(euler_angles):
"""
Convert euler to rotation matrix, using the XYZ convention R = Rx * Ry * Rz, left-handed positive sign
(from Openface)
:param euler_angles: euler angles
:return: rotation matrix
"""
s1 = np.sin(euler_angles[0])
s2 = np.sin(euler_angles[1])
s3 = np.sin(euler_angles[2])
c1 = np.cos(euler_angles[0])
c2 = np.cos(euler_angles[1])
c3 = np.cos(euler_angles[2])
rot_mat = np.empty((3,3), dtype=np.float32)
rot_mat[0, 0] = c2 * c3
rot_mat[0, 1] = -c2 * s3
rot_mat[0, 2] = s2
rot_mat[1, 0] = c1 * s3 + c3 * s1 * s2
rot_mat[1, 1] = c1 * c3 - s1 * s2 * s3
rot_mat[1, 2] = -c2 * s1
rot_mat[2, 0] = s1 * s3 - c1 * c3 * s2
rot_mat[2, 1] = c3 * s1 + c1 * s2 * s3
rot_mat[2, 2] = c1 * c2
return np.linalg.inv(rot_mat)
示例2: positional_signal
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def positional_signal(hidden_size: int, length: int,
min_timescale: float = 1.0, max_timescale: float = 1e4):
"""
Helper function, constructing basic positional encoding.
The code is partially based on implementation from Tensor2Tensor library
https://github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/layers/common_attention.py
"""
if hidden_size % 2 != 0:
raise ValueError(
f"The hidden dimension of the model must be divisible by 2."
f"Currently it is {hidden_size}")
position = K.arange(0, length, dtype=K.floatx())
num_timescales = hidden_size // 2
log_timescale_increment = K.constant(
(np.log(float(max_timescale) / float(min_timescale)) /
(num_timescales - 1)),
dtype=K.floatx())
inv_timescales = (
min_timescale *
K.exp(K.arange(num_timescales, dtype=K.floatx()) *
-log_timescale_increment))
scaled_time = K.expand_dims(position, 1) * K.expand_dims(inv_timescales, 0)
signal = K.concatenate([K.sin(scaled_time), K.cos(scaled_time)], axis=1)
return K.expand_dims(signal, axis=0)
示例3: test_saving_custom_activation_function
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def test_saving_custom_activation_function():
x = Input(shape=(3,))
output = Dense(3, activation=K.cos)(x)
model = Model(x, output)
model.compile(loss=losses.MSE,
optimizer=optimizers.RMSprop(lr=0.0001),
metrics=[metrics.categorical_accuracy])
x = np.random.random((1, 3))
y = np.random.random((1, 3))
model.train_on_batch(x, y)
out = model.predict(x)
_, fname = tempfile.mkstemp('.h5')
save_model(model, fname)
model = load_model(fname, custom_objects={'cos': K.cos})
os.remove(fname)
out2 = model.predict(x)
assert_allclose(out, out2, atol=1e-05)
示例4: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def call(self, x):
if (self.size is None) or (self.mode == 'sum'):
self.size = int(x.shape[-1])
batch_size, seq_len = K.shape(x)[0], K.shape(x)[1]
position_j = 1. / K.pow(10000.,
2 * K.arange(self.size / 2, dtype='float32'
) / self.size)
position_j = K.expand_dims(position_j, 0)
# K.arange不支持变长,只好用这种方法生成
position_i = K.cumsum(K.ones_like(x[:, :, 0]), 1) - 1
position_i = K.expand_dims(position_i, 2)
position_ij = K.dot(position_i, position_j)
position_ij = K.concatenate(
[K.cos(position_ij), K.sin(position_ij)], 2)
if self.mode == 'sum':
return position_ij + x
elif self.mode == 'concat':
return K.concatenate([position_ij, x], 2)
示例5: _rotation_y
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def _rotation_y(theta):
r1 = K.cos(theta[:,0:1])
r2 = K.sin(theta[:,0:1])
zero = K.zeros_like(r1)
one = K.ones_like(r1)
first = K.reshape(K.concatenate([r1,zero,r2,zero],axis=1),(-1,1,4))
second = K.reshape(K.concatenate([zero,one,zero,zero],axis=1),(-1,1,4))
third = K.reshape(K.concatenate([-r2,zero,r1,zero],axis=1),(-1,1,4))
fourth = K.reshape(K.concatenate([zero,zero,zero,one],axis=1),(-1,1,4))
rotation_y = K.concatenate([first,second,third,fourth],axis=1)
rotation_y = T.reshape(rotation_y,[-1,4,4])
return rotation_y
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:14,代码来源:transform_rnn.py
示例6: _rotation_x
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def _rotation_x(theta):
r1 = K.cos(theta[:,1:2])
r2 = K.sin(theta[:,1:2])
zero = K.zeros_like(r1)
one = K.ones_like(r1)
first = K.reshape(K.concatenate([one,zero,zero,zero],axis=1),(-1,1,4))
second = K.reshape(K.concatenate([zero,r1,-r2,zero],axis=1),(-1,1,4))
third = K.reshape(K.concatenate([zero,r2,r1,zero],axis=1),(-1,1,4))
fourth = K.reshape(K.concatenate([zero,zero,zero,one],axis=1),(-1,1,4))
rotation_x = K.concatenate([first,second,third,fourth],axis=1)
rotation_x = T.reshape(rotation_x,[-1,4,4])
return rotation_x
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:14,代码来源:transform_rnn.py
示例7: _rotation_z
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def _rotation_z(theta):
r1 = K.cos(theta[:,2:3])
r2 = K.sin(theta[:,2:3])
zero = K.zeros_like(r1)
one = K.ones_like(r1)
first = K.reshape(K.concatenate([r1,-r2,zero,zero],axis=1),(-1,1,4))
second = K.reshape(K.concatenate([r2,r1,zero,zero],axis=1),(-1,1,4))
third = K.reshape(K.concatenate([zero,zero,one,zero],axis=1),(-1,1,4))
fourth = K.reshape(K.concatenate([zero,zero,zero,one],axis=1),(-1,1,4))
rotation_z = K.concatenate([first,second,third,fourth],axis=1)
rotation_z = T.reshape(rotation_z,[-1,4,4])
return rotation_z
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:14,代码来源:transform_rnn.py
示例8: angles2vector
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def angles2vector(angles):
"""
Convert 2D angle (yaw and pitch) to 3D unit vector
:param angles: list of 2D angles
:return: computed 3D vectors
"""
x = (-1.0) * K.sin(angles[:, 0]) * K.cos(angles[:, 1])
y = (-1.0) * K.sin(angles[:, 1])
z = (-1.0) * K.cos(angles[:, 0]) * K.cos(angles[:, 1])
vec = K.transpose(K.concatenate([[x], [y], [z]], axis=0))
return vec
示例9: numpy_angles2vector
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def numpy_angles2vector(angles):
"""
Numpy version of angles2vector. Convert 2D angle (yaw and pitch) to 3D unit vector
:param angles: list of 2D angles
:return: computed 3D vectors
"""
x = (-1.0)*np.sin(angles[:, 0]) * np.cos(angles[:, 1])
y = (-1.0)*np.sin(angles[:, 1])
z = (-1.0)*np.cos(angles[:, 0]) * np.cos(angles[:, 1])
vec = np.transpose(np.concatenate([[x], [y], [z]], axis=0))
return vec
示例10: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def call(self, inputs, mask=None):
input_shape = K.shape(inputs)
if self.mode == self.MODE_ADD:
batch_size, seq_len, output_dim = input_shape[0], input_shape[1], input_shape[2]
pos_input = K.tile(K.expand_dims(K.arange(seq_len), axis=0), [batch_size, 1])
elif self.mode == self.MODE_CONCAT:
batch_size, seq_len, output_dim = input_shape[0], input_shape[1], self.output_dim
pos_input = K.tile(K.expand_dims(K.arange(seq_len), axis=0), [batch_size, 1])
else:
output_dim = self.output_dim
pos_input = inputs
if K.dtype(pos_input) != K.floatx():
pos_input = K.cast(pos_input, K.floatx())
evens = K.arange(output_dim // 2) * 2
odds = K.arange(output_dim // 2) * 2 + 1
even_embd = K.sin(
K.dot(
K.expand_dims(pos_input, -1),
K.expand_dims(1.0 / K.pow(
10000.0,
K.cast(evens, K.floatx()) / K.cast(output_dim, K.floatx())
), 0)
)
)
odd_embd = K.cos(
K.dot(
K.expand_dims(pos_input, -1),
K.expand_dims(1.0 / K.pow(
10000.0, K.cast((odds - 1), K.floatx()) / K.cast(output_dim, K.floatx())
), 0)
)
)
embd = K.stack([even_embd, odd_embd], axis=-1)
output = K.reshape(embd, [-1, K.shape(inputs)[1], output_dim])
if self.mode == self.MODE_CONCAT:
output = K.concatenate([inputs, output], axis=-1)
if self.mode == self.MODE_ADD:
output += inputs
return output
示例11: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def call(self, Z):
m = self.epsilon * (K.sin(Z) - K.cos(Z))
A = K.maximum(m, Z)
return A
示例12: parse_example_proto
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def parse_example_proto(examples_serialized, have_image_id=False):
feature_map = {
'image/encoded': tf.FixedLenFeature([], dtype=tf.string,
default_value=''),
'image/filename': tf.FixedLenFeature([], dtype=tf.string,
default_value=''),
'image/height': tf.FixedLenFeature([], dtype=tf.int64),
'image/width': tf.FixedLenFeature([], dtype=tf.int64)
}
# TODO(ahundt) remove boolean once we are set up with k-fold cross validation of images and objects
if have_image_id:
feature_map['object/id'] = tf.FixedLenFeature([], dtype=tf.int64)
for i in range(4):
y_key = 'bbox/y' + str(i)
x_key = 'bbox/x' + str(i)
feature_map[y_key] = tf.VarLenFeature(dtype=tf.float32)
feature_map[x_key] = tf.VarLenFeature(dtype=tf.float32)
feature_map['bbox/cy'] = tf.VarLenFeature(dtype=tf.float32)
feature_map['bbox/cx'] = tf.VarLenFeature(dtype=tf.float32)
feature_map['bbox/tan'] = tf.VarLenFeature(dtype=tf.float32)
feature_map['bbox/theta'] = tf.VarLenFeature(dtype=tf.float32)
feature_map['bbox/sin_theta'] = tf.VarLenFeature(dtype=tf.float32)
feature_map['bbox/cos_theta'] = tf.VarLenFeature(dtype=tf.float32)
feature_map['bbox/width'] = tf.VarLenFeature(dtype=tf.float32)
feature_map['bbox/height'] = tf.VarLenFeature(dtype=tf.float32)
feature_map['bbox/grasp_success'] = tf.VarLenFeature(dtype=tf.int64)
# feature_map['bbox/sin_2_theta'] = tf.sin(feature_map['bbox/theta'] * 2.0)
# feature_map['bbox/cos_2_theta'] = tf.cos(feature_map['bbox/theta'] * 2.0)
features = tf.parse_single_example(examples_serialized, feature_map)
return features
示例13: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def __init__(self, loss_function, lats, data_format='channels_last', weighting='cosine'):
"""
Initialize a weighted loss.
:param loss_function: method: Keras loss function to apply after the weighting
:param lats: ndarray: 1-dimensional array of latitude coordinates
:param data_format: Keras data_format ('channels_first' or 'channels_last')
:param weighting: str: type of weighting to apply. Options are:
cosine: weight by the cosine of the latitude (default)
midlatitude: weight by the cosine of the latitude but also apply a 25% reduction to the equator and boost
to the mid-latitudes
"""
self.loss_function = loss_function
self.lats = lats
self.data_format = K.normalize_data_format(data_format)
if weighting not in ['cosine', 'midlatitude']:
raise ValueError("'weighting' must be one of 'cosine' or 'midlatitude'")
self.weighting = weighting
lat_tensor = K.zeros(lats.shape)
print(lats)
lat_tensor.assign(K.cast_to_floatx(lats[:]))
self.weights = K.cos(lat_tensor * np.pi / 180.)
if self.weighting == 'midlatitude':
self.weights = self.weights - 0.25 * K.sin(lat_tensor * 2 * np.pi / 180.)
self.is_init = False
self.__name__ = 'latitude_weighted_loss'
示例14: latitude_weighted_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def latitude_weighted_loss(loss_function=mean_squared_error, lats=None, output_shape=(), axis=-2, weighting='cosine'):
"""
Create a loss function that weights inputs by a function of latitude before calculating the loss.
:param loss_function: method: Keras loss function to apply after the weighting
:param lats: ndarray: 1-dimensional array of latitude coordinates
:param output_shape: tuple: shape of expected model output
:param axis: int: latitude axis in model output shape
:param weighting: str: type of weighting to apply. Options are:
cosine: weight by the cosine of the latitude (default)
midlatitude: weight by the cosine of the latitude but also apply a 25% reduction to the equator and boost
to the mid-latitudes
:return: callable loss function
"""
if weighting not in ['cosine', 'midlatitude']:
raise ValueError("'weighting' must be one of 'cosine' or 'midlatitude'")
if lats is not None:
lat_tensor = K.zeros(lats.shape)
lat_tensor.assign(K.cast_to_floatx(lats[:]))
weights = K.cos(lat_tensor * np.pi / 180.)
if weighting == 'midlatitude':
weights = weights + 0.5 * K.pow(K.sin(lat_tensor * 2 * np.pi / 180.), 2.)
weight_shape = output_shape[axis:]
for d in weight_shape[1:]:
weights = K.expand_dims(weights, axis=-1)
weights = K.repeat_elements(weights, d, axis=-1)
else:
weights = K.ones(output_shape)
def lat_loss(y_true, y_pred):
return loss_function(y_true * weights, y_pred * weights)
return lat_loss
示例15: seasonality_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import cos [as 别名]
def seasonality_model(thetas, backcast_length, forecast_length, is_forecast):
p = thetas.get_shape().as_list()[-1]
p1, p2 = (p // 2, p // 2) if p % 2 == 0 else (p // 2, p // 2 + 1)
t = linear_space(backcast_length, forecast_length, fwd_looking=is_forecast)
s1 = K.stack([K.cos(2 * np.pi * i * t) for i in range(p1)], axis=0)
s2 = K.stack([K.sin(2 * np.pi * i * t) for i in range(p2)], axis=0)
if p == 1:
s = s2
else:
s = K.concatenate([s1, s2], axis=0)
s = K.cast(s, np.float32)
return K.dot(thetas, s)