def _init_main(self): self._local_score() final_score = self.global_p_e_ctx[:, :, 0] if self.args.loss_type == 'softmax': self.l_final_score = final_score final_score_softmax = tf.nn.softmax(final_score) final_score_softmax = tf.multiply(tf.cast(self.mask, tf.float32), final_score_softmax) self.final_score = final_score_softmax / tf.reduce_sum( final_score_softmax, -1, keepdims=True) self.link_loss = tf.reduce_mean(-tf.reduce_sum( self.linking_tag * tf.log(self.final_score + 1e-8), axis=1)) else: self.l_final_score = final_score self.final_score = final_score self.neg_mask = tf.subtract( tf.cast(tf.cast(self.ment_cand_id, tf.bool), tf.float32), tf.cast(self.linking_tag, tf.float32)) self.link_loss = margin_loss(labels=self.linking_tag, logits=self.final_score, neg_mask=self.neg_mask, margin_param=self.args.margin_param) correct_prediction = tf.equal(tf.argmax(self.linking_tag, 1), tf.argmax(self.final_score, 1)) self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar("accuracy", self.accuracy) self.right_ment_num = tf.reduce_sum( tf.cast(correct_prediction, tf.float32))
def fit(self): """ Training a model on the training set. """ print("\nTraining started.\n") self.model.train() optimizer = torch.optim.Adam(self.model.parameters(), lr=self.args.learning_rate, weight_decay=self.args.weight_decay) for epoch in tqdm(range(self.args.epochs), desc = "Epochs: ", leave = True): random.shuffle(self.train_graph_paths) self.create_batches() losses = 0 self.steps = trange(len(self.batches), desc="Loss") for step in self.steps: accumulated_losses = 0 optimizer.zero_grad() batch = self.batches[step] for path in batch: data = self.create_input_data(path) prediction, reconstruction_loss = self.model(data) loss = margin_loss(prediction, data["target"], self.args.lambd)+self.args.theta*reconstruction_loss accumulated_losses = accumulated_losses + loss accumulated_losses = accumulated_losses/len(batch) accumulated_losses.backward() optimizer.step() losses = losses + accumulated_losses.item() average_loss = losses/(step + 1) self.steps.set_description("CapsGNN (Loss=%g)" % round(average_loss,4))
def _init_main(self): self._local_score() self._global_GCN_score() self.global_p_e_ctx = tf.layers.dense(self.gcn_h, 1, activation=None) final_score = self.global_p_e_ctx[:, :, 0] self.final_score = tf.multiply(self.mask, final_score) self.l_final_score = tf.multiply(self.mask, final_score) self.neg_mask = tf.subtract( tf.cast(tf.cast(self.ment_cand_id, tf.bool), tf.float32), tf.cast(self.linking_tag, tf.float32)) if self.args.loss_type == 'margin_sum': self.link_loss = margin_loss_sum( labels=self.linking_tag, logits=self.final_score, neg_mask=self.neg_mask, margin_param=self.args.margin_param) else: self.link_loss = margin_loss(labels=self.linking_tag, logits=self.final_score, neg_mask=self.neg_mask, margin_param=self.args.margin_param) correct_prediction = tf.equal(tf.argmax(self.linking_tag, 1), tf.argmax(self.final_score, 1)) self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar("accuracy", self.accuracy) self.right_ment_num = tf.reduce_sum( tf.cast(correct_prediction, tf.float32))