def forward(self, seq_dignosis_codes, seq_time_step, batch_labels, options, maxlen): seq_time_step = np.array(list(units.pad_time(seq_time_step, options))) lengths = torch.from_numpy( np.array([len(seq) for seq in seq_dignosis_codes])).cuda() diagnosis_codes, labels, mask, mask_final, mask_code = units.pad_matrix_new( seq_dignosis_codes, batch_labels, options) if options['use_gpu']: diagnosis_codes = torch.LongTensor(diagnosis_codes).cuda() mask_mult = torch.BoolTensor(1 - mask).unsqueeze(2).cuda() mask_final = torch.Tensor(mask_final).unsqueeze(2).cuda() mask_code = torch.Tensor(mask_code).unsqueeze(3).cuda() else: diagnosis_codes = torch.LongTensor(diagnosis_codes) mask_mult = torch.BoolTensor(1 - mask).unsqueeze(2) mask_final = torch.Tensor(mask_final).unsqueeze(2) mask_code = torch.Tensor(mask_code).unsqueeze(3) features = self.feature_encoder(diagnosis_codes, mask_mult, mask_code, seq_time_step, lengths) final_statues = features * mask_final final_statues = final_statues.sum(1) predictions = self.classify_layer(final_statues) labels = torch.LongTensor(labels) if options['use_gpu']: labels = labels.cuda() return predictions, labels, None
def forward(self, seq_dignosis_codes, seq_time_step, batch_labels, options, maxlen): # seq_dignosis_codes: [batch_size, length, bag_len] # seq_time_step: [batch_size, length] the day times to the final visit # batch_labels: [batch_size] 0 negative 1 positive seq_time_step = np.array(list(units.pad_time(seq_time_step, options))) lengths = torch.from_numpy( np.array([len(seq) for seq in seq_dignosis_codes])).cuda() diagnosis_codes, labels, mask, mask_final, mask_code = units.pad_matrix_new( seq_dignosis_codes, batch_labels, options) if options['use_gpu']: diagnosis_codes = torch.LongTensor(diagnosis_codes).cuda() mask_mult = torch.BoolTensor(1 - mask).unsqueeze(2).cuda() mask_final = torch.Tensor(mask_final).unsqueeze(2).cuda() mask_code = torch.Tensor(mask_code).unsqueeze(3).cuda() else: diagnosis_codes = torch.LongTensor(diagnosis_codes) mask_mult = torch.BoolTensor(1 - mask).unsqueeze(2) mask_final = torch.Tensor(mask_final).unsqueeze(2) mask_code = torch.Tensor(mask_code).unsqueeze(3) features = self.feature_encoder(diagnosis_codes, mask_mult, mask_code, seq_time_step, lengths) final_statues = features * mask_final final_statues = final_statues.sum(1, keepdim=True) quiryes = self.relu(self.quiry_layer(final_statues)) # prior_weight = self.prior_encoder(seq_dignosis_codes, maxlen, quiryes, options, mask_mult) self_weight = self.get_self_attention(features, quiryes, mask_mult) time_weight = self.time_encoder(seq_time_step, quiryes, options, mask_mult) attention_weight = torch.softmax( self.quiry_weight_layer(final_statues), 2) total_weight = torch.cat((time_weight, self_weight), 2) total_weight = torch.sum(total_weight * attention_weight, 2, keepdim=True) total_weight = total_weight / ( torch.sum(total_weight, 1, keepdim=True) + 1e-5) weighted_features = features * total_weight averaged_features = torch.sum(weighted_features, 1) averaged_features = self.dropout(averaged_features) predictions = self.classify_layer(averaged_features) labels = torch.LongTensor(labels) if options['use_gpu']: labels = labels.cuda() return predictions, labels, self_weight