def add_embedding(self): """Add embedding layer. that maps from vocabulary to vectors. inputs: a list of tensors each of which have a size of [batch_size, embed_size] """ self.global_step = tf.Variable(0, name='global_step', trainable=False) vocab_sz = max(self.config.vocab_dict.values()) with tf.variable_scope('embedding') as scp: self.exclude_reg_scope(scp) ## exclude scope 추가 if self.config.pre_trained: embed = utils.readEmbedding( self.config.embed_path) ## embedding 파일 조회 embed_matrix, valid_mask = utils.mkEmbedMatrix( embed, dict(self.config.vocab_dict)) ## embedding matrix 생성 embedding = tf.Variable(embed_matrix, 'Embedding') ## embedding 생성 partial_update_embedding = entry_stop_gradients( embedding, tf.expand_dims(valid_mask, 1) ) ## a tensor have the same value of target, but some entry will have no gradient during backprop embedding = tf.cond( self.on_epoch < self.config.partial_update_until_epoch, lambda: partial_update_embedding, lambda: embedding ) ## https://www.tensorflow.org/api_docs/python/tf/cond else: embedding = tf.get_variable('Embedding', [vocab_sz, self.config.embed_size], trainable=True) ## embedding 생성 return embedding
def add_embedding(self): """Add embedding layer. that maps from vocabulary to vectors. inputs: a list of tensors each of which have a size of [batch_size, embed_size] """ self.global_step = tf.Variable(0, name='global_step', trainable=False) vocab_sz = max(self.config.vocab_dict.values()) with tf.variable_scope('embedding') as scp: self.exclude_reg_scope(scp) if self.config.pre_trained: embed = utils.readEmbedding(self.config.embed_path) embed_matrix, valid_mask = utils.mkEmbedMatrix( embed, dict(self.config.vocab_dict)) embedding = tf.Variable(embed_matrix, 'Embedding') partial_update_embedding = entry_stop_gradients( embedding, tf.expand_dims(valid_mask, 1)) embedding = tf.cond( self.on_epoch < self.config.partial_update_until_epoch, lambda: partial_update_embedding, lambda: embedding) else: embedding = tf.get_variable('Embedding', [vocab_sz, self.config.embed_size], trainable=True) return embedding