def __init__(self, hidden_units: list = None, hidden_activate=tf.keras.layers.ReLU(), use_bn: bool = False, res_unit=1, output_dim=-1, seed=2020, other_dense=None, use_ln: bool = False, use_flatten=False, **kwargs): ''' notice: dense of dnn can to replace other layer, e.g:mult head atten(autoInt), to_replace:other_dense,succ to replace. :param hidden_units:please make sure to need units list when use other dense,need to input it too. e.g need 3 hidden,but use other dense==>[[],[],[]] num is not import,shape is very import :param res_unit:res add skip num :param activate:hidden activate Dnn core: supports auto bn ''' super(DnnLayer, self).__init__(**kwargs) self.hidden_list = other_dense if not other_dense: self.hidden_list = [ HiddenLayer(hidden_units=dim, use_bn=False, other_dense=other_dense) for dim in hidden_units ] self.activate = hidden_activate self.activate = [ ResActivateLayer(use_bn=use_bn, use_ln=use_ln, hidden_activate=hidden_activate) for idx_ in range(len(self.hidden_list)) ] self.add = tf.keras.layers.Add() self.seed = 2020 self.output_dim = output_dim self.res_unit = res_unit if output_dim != -1: self.logit_layer = tf.keras.layers.Dense( units=output_dim, kernel_initializer=glorot_uniform(seed=seed), bias_initializer=glorot_uniform(seed=seed)) if use_flatten: self.flat = tf.keras.layers.Flatten() self.use_flatten = use_flatten
def build(self, input_shape): super(AttentionBaseLayer, self).build(input_shape) self.kernel_w = self.add_weight( name='single_score_w', shape=(input_shape[0][-1], self.atten_dim), initializer=glorot_uniform(seed=self.seed)) self.kernel_b = self.add_weight( name='single_score_b', shape=(self.atten_dim, ), initializer=glorot_uniform(seed=self.seed))
def __init__(self, hidden_units: int, use_bn: bool = True, seed=2020, other_dense=None): super(HiddenLayer, self).__init__() self.dense = tf.keras.layers.Dense( units=hidden_units, kernel_initializer=glorot_uniform(seed=seed), bias_initializer=glorot_uniform(seed=seed)) if other_dense: self.dense = other_dense self.bn = tf.keras.layers.BatchNormalization() self.use_bn = use_bn
def __init__(self, attention_dim=4, seed=2020, output_dim=1): super(AttentionBaseLayer, self).__init__() self.add = tf.keras.layers.Add() self.atten_dim = attention_dim self.seed = seed self.single_mlp = tf.keras.layers.Dense( 1, 'relu', use_bias=False, kernel_initializer=glorot_uniform(self.seed)) self.single_softmax = tf.keras.layers.Activation('softmax') self.output_layer = tf.keras.layers.Dense(output_dim)
def build(self, input_shape): self.kernel = [ self.add_weight(name='outer_weight_{}'.format(str(i)), shape=[input_shape[-1], 1], initializer=glorot_uniform(seed=self.seed)) for i in range(self.cross_hidden) ] self.bias = [ self.add_weight(name='outer_bias_{}'.format(str(i)), shape=[input_shape[-1], 1], initializer=tf.keras.initializers.zeros()) for i in range(self.cross_hidden) ] super(CrossLayer, self).build(input_shape)
def build(self, input_shape): if not self.is_linear: self.embed = [ tf.keras.layers.Embedding( name=info_.fea_name, input_dim=info_.word_size, output_dim=info_.cross_unit, mask_zero=info_.mask_zero, embeddings_initializer=glorot_uniform(seed=self.seed), input_length=info_.input_length, trainable=info_.is_trainable, weights=info_.pre_weight) if info_.cross_unit != 0 else [] for info_ in self.sparse_info ] else: self.embed = [ tf.keras.layers.Embedding(name=info_.fea_name, input_dim=info_.word_size, output_dim=info_.linear_unit) for info_ in self.sparse_info ] super(SparseEmbed, self).build(input_shape)
def build(self, input_shape): super(ScoreLayer, self).build(input_shape) if self.use_global: self.global_bias = self.add_weight(shape=(1, ), initializer=glorot_uniform( self.seed))