'''
keras层
为input添加一个可训练权重 => input · weight
'''
class Add_weight(layers.Layer):
def __init__(self, name="Add_weight", **kwargs):
super().__init__(name=name, **kwargs)
def build(self, input_shape):
self.weight_to_mut = tf.Variable(tf.constant(0.1, shape=[input_shape[2],input_shape[2]]),trainable=True)
super().build(input_shape)
def call(self, x):
return K.dot(x, self.weight_to_mut)
def get_config(self):
config=super().get_config().copy()
return config
def compute_output_shape(self, input_shape):
return (input_shape[2], input_shape[2])
input_wq = Add_weight(name='add_1')(inputs)
'''
attention is all you need 论文中的 position embedding层
继承keras layer
h2 = PositionEmbedding(time_step, embedding_size)(h1)
'''
class PositionEmbedding(layers.Layer):
def __init__(self, position, d_model, name="PositionEmbedding", **kwargs):
super().__init__(name=name, **kwargs)
self.position = position
self.d_model = d_model
self.pos_encoding = self.positional_encoding(position, d_model)
def get_angles(self, position, i, d_model):
angles = 1 / tf.pow(10000, (2 * (i // 2)) /
tf.cast(d_model, tf.float32))
return position * angles
def positional_encoding(self, position, d_model):
angle_rads = self.get_angles(
position=tf.range(position, dtype=tf.float32)[:, tf.newaxis],
i=tf.range(d_model, dtype=tf.float32)[tf.newaxis, :],
d_model=d_model)
sines = tf.math.sin(angle_rads[:, 0::2])
cosines = tf.math.cos(angle_rads[:, 1::2])
pos_encoding = tf.concat([sines, cosines], axis=-1)
pos_encoding = pos_encoding[tf.newaxis, ...]
return tf.cast(pos_encoding, tf.float32)
def call(self, inputs):
return inputs + self.pos_encoding[:, :tf.shape(inputs)[1], :]
def get_config(self):
config = super().get_config().copy()
config.update({
'position': self.position,
'd_model': self.d_model
})
return config
h2 = PositionEmbedding(time_step, embedding_size,
name="PositionEmbedding")(h1)