def _build_newsencoder(self, embedding_layer): """The main function to create news encoder of LSTUR. Args: embedding_layer (object): a word embedding layer. Return: object: the news encoder of LSTUR. """ hparams = self.hparams sequences_input_title = keras.Input(shape=(hparams.title_size, ), dtype="int32") embedded_sequences_title = embedding_layer(sequences_input_title) y = layers.Dropout(hparams.dropout)(embedded_sequences_title) y = layers.Conv1D( hparams.filter_num, hparams.window_size, activation=hparams.cnn_activation, padding="same", bias_initializer=keras.initializers.Zeros(), kernel_initializer=keras.initializers.glorot_uniform( seed=self.seed), )(y) print(y) y = layers.Dropout(hparams.dropout)(y) y = layers.Masking()( OverwriteMasking()([y, ComputeMasking()(sequences_input_title)])) pred_title = AttLayer2(hparams.attention_hidden_dim, seed=self.seed)(y) print(pred_title) model = keras.Model(sequences_input_title, pred_title, name="news_encoder") return model
def _build_userencoder(self, newsencoder): """The main function to create user encoder of NAML. Args: newsencoder (object): the news encoder of NAML. Return: object: the user encoder of NAML. """ hparams = self.hparams his_input_title_body_verts = keras.Input( shape=(hparams.his_size, hparams.title_size + hparams.body_size + 2), dtype="int32", ) click_news_presents = layers.TimeDistributed(newsencoder)( his_input_title_body_verts) user_present = AttLayer2(hparams.attention_hidden_dim, seed=self.seed)(click_news_presents) model = keras.Model(his_input_title_body_verts, user_present, name="user_encoder") return model
def _build_bodyencoder(self, embedding_layer): """build body encoder of NAML news encoder. Args: embedding_layer (object): a word embedding layer. Return: object: the body encoder of NAML. """ hparams = self.hparams sequences_input_body = keras.Input(shape=(hparams.body_size, ), dtype="int32") embedded_sequences_body = embedding_layer(sequences_input_body) y = layers.Dropout(hparams.dropout)(embedded_sequences_body) y = layers.Conv1D( hparams.filter_num, hparams.window_size, activation=hparams.cnn_activation, padding="same", bias_initializer=keras.initializers.Zeros(), kernel_initializer=keras.initializers.glorot_uniform( seed=self.seed), )(y) y = layers.Dropout(hparams.dropout)(y) pred_body = AttLayer2(hparams.attention_hidden_dim, seed=self.seed)(y) pred_body = layers.Reshape((1, hparams.filter_num))(pred_body) model = keras.Model(sequences_input_body, pred_body, name="body_encoder") return model
def _build_newsencoder(self, embedding_layer): """The main function to create news encoder of NRMS. Args: embedding_layer (object): a word embedding layer. Return: object: the news encoder of NRMS. """ hparams = self.hparams sequences_input_title = keras.Input(shape=(hparams.title_size, ), dtype="int32") embedded_sequences_title = embedding_layer(sequences_input_title) y = layers.Dropout(hparams.dropout)(embedded_sequences_title) y = SelfAttention(hparams.head_num, hparams.head_dim, seed=self.seed)([y, y, y]) y = layers.Dropout(hparams.dropout)(y) pred_title = AttLayer2(hparams.attention_hidden_dim, seed=self.seed)(y) model = keras.Model(sequences_input_title, pred_title, name="news_encoder") return model
def _build_newsencoder(self, embedding_layer): """The main function to create news encoder of NAML. news encoder in composed of title encoder, body encoder, vert encoder and subvert encoder Args: embedding_layer (object): a word embedding layer. Return: object: the news encoder of NAML. """ hparams = self.hparams input_title_body_verts = keras.Input(shape=(hparams.title_size + hparams.body_size + 2, ), dtype="int32") sequences_input_title = layers.Lambda( lambda x: x[:, :hparams.title_size])(input_title_body_verts) sequences_input_body = layers.Lambda( lambda x: x[:, hparams.title_size:hparams.title_size + hparams. body_size])(input_title_body_verts) input_vert = layers.Lambda( lambda x: x[:, hparams.title_size + hparams.body_size:hparams. title_size + hparams.body_size + 1, ])( input_title_body_verts) input_subvert = layers.Lambda( lambda x: x[:, hparams.title_size + hparams.body_size + 1:])( input_title_body_verts) title_repr = self._build_titleencoder(embedding_layer)( sequences_input_title) body_repr = self._build_bodyencoder(embedding_layer)( sequences_input_body) vert_repr = self._build_vertencoder()(input_vert) subvert_repr = self._build_subvertencoder()(input_subvert) concate_repr = layers.Concatenate(axis=-2)( [title_repr, body_repr, vert_repr, subvert_repr]) news_repr = AttLayer2(hparams.attention_hidden_dim, seed=self.seed)(concate_repr) model = keras.Model(input_title_body_verts, news_repr, name="news_encoder") return model
def _build_userencoder(self, titleencoder): """The main function to create user encoder of NRMS. Args: titleencoder (object): the news encoder of NRMS. Return: object: the user encoder of NRMS. """ hparams = self.hparams his_input_title = keras.Input( shape=(hparams.his_size, hparams.title_size), dtype="int32" ) click_title_presents = layers.TimeDistributed(titleencoder)(his_input_title) y = SelfAttention(hparams.head_num, hparams.head_dim, seed=self.seed)( [click_title_presents] * 3 ) user_present = AttLayer2(hparams.attention_hidden_dim, seed=self.seed)(y) model = keras.Model(his_input_title, user_present, name="user_encoder") return model