コード例 #1
0
    def forward(self, seq_dignosis_codes, seq_time_step, batch_labels, options, maxlen):
        seq_time_step = np.array(list(rnn_tools.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 = rnn_tools.pad_matrix_mine(seq_dignosis_codes, batch_labels, options)
        if options['use_gpu']:
            diagnosis_codes = torch.Tensor(diagnosis_codes).cuda()
            mask_mult = torch.Tensor(mask).unsqueeze(2).cuda()
            mask_final = torch.Tensor(mask_final).unsqueeze(2).cuda()
        else:
            diagnosis_codes = torch.Tensor(diagnosis_codes)
            mask_mult = torch.Tensor(mask).unsqueeze(2)
            mask_final = torch.Tensor(mask_final).unsqueeze(2)
        features, self_weight = self.feature_encoder(diagnosis_codes, mask_mult, seq_time_step, lengths)

        final_statues = features * mask_final
        final_statues = final_statues.sum(0, keepdim=True)
        quiryes = self.relu(self.quiry_layer(final_statues))
        prior_weight = self.prior_encoder(seq_dignosis_codes, maxlen, quiryes, options, mask_mult)
        attention_weight = torch.softmax(self.quiry_weight_layer(final_statues), 2)

        total_weight = torch.cat((prior_weight, self_weight), 2)
        total_weight = torch.sum(total_weight * attention_weight * mask_mult, 2, keepdim=True)
        total_weight = total_weight / (torch.sum(total_weight, 0, keepdim=True) + 1e-5)
        weighted_features = features * total_weight
        averaged_features = torch.sum(weighted_features, 0)
        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
コード例 #2
0
    def forward(self, seq_dignosis_codes, seq_time_step, batch_labels, options, maxlen):
        diagnosis_codes, labels, mask, mask_final = rnn_tools.pad_matrix_mine(seq_dignosis_codes, batch_labels, options)
        if options['use_gpu']:
            diagnosis_codes = torch.Tensor(diagnosis_codes).cuda()
            mask_mult = torch.Tensor(mask).unsqueeze(2).cuda()
            mask_final = torch.Tensor(mask_final).unsqueeze(2).cuda()
        else:
            diagnosis_codes = torch.Tensor(diagnosis_codes)
            mask_mult = torch.Tensor(mask).unsqueeze(2)
            mask_final = torch.Tensor(mask_final).unsqueeze(2)
        features, self_weight = self.feature_encoder(diagnosis_codes, mask_mult)

        final_statues = features * mask_final
        final_statues = final_statues.sum(0, keepdim=True)
        quiryes = self.relu(self.quiry_layer(final_statues))
        prior_weight = self.prior_encoder(seq_dignosis_codes, maxlen, quiryes, options, mask_mult)
        time_weight = self.time_encoder(seq_time_step, quiryes, options, mask_mult)

        total_weight = prior_weight * time_weight * self_weight * mask_mult
        total_weight = total_weight / (torch.sum(total_weight, 0, keepdim=True) + 1e-8)
        weighted_features = features * total_weight
        averaged_features = torch.sum(weighted_features, 0)
        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
コード例 #3
0
def load_gruself_input(input_jsonl_path, max_seq_len, max_num_pervisit):
    with open("./data/hfdata/hf_code2idx_new.pickle", "rb") as fin:
        code2id = pickle.load(fin)
    n_diagnosis_codes = len(code2id)
    diagnosis_codes = []
    labels = []
    qids = []
    time_step = []
    with open(input_jsonl_path, "r", encoding="utf-8") as fin:
        for line in fin:
            input_json = json.loads(line)
            qids.append(input_json['id'])
            record_icd = input_json["medical_records"]["record_icd"]
            time_dis = input_json["medical_records"]["time_distance"]
            label = input_json["heart_diseases"]["hf_label"]
            for i in range(len(record_icd)):
                for j in range(len(record_icd[i])):
                    record_icd[i][j] = code2id[record_icd[i][j]]
            diagnosis_codes.append(record_icd)
            time_step.append(time_dis)
            labels.append(label)
    for ind in range(len(diagnosis_codes)):
        if len(diagnosis_codes[ind]) > 50:
            diagnosis_codes[ind] = diagnosis_codes[ind][-50:]
            time_step[ind] = time_step[ind][-50:]
    diagnosis_codes, mask, mask_final = rnn_tools.pad_matrix_mine(
        diagnosis_codes, n_diagnosis_codes, max_num_pervisit)
    diagnosis_codes = torch.LongTensor(diagnosis_codes).permute(
        1, 0, 2).contiguous().unsqueeze(1)
    mask_mult = torch.FloatTensor(mask).permute(1, 0).contiguous()
    mask_mult = mask_mult.unsqueeze(1)
    labels = torch.tensor(labels, dtype=torch.long)
    return qids, labels, diagnosis_codes, mask_mult
コード例 #4
0
 def forward(self, seq_dignosis_codes, batch_labels, options, maxlen):
     batch_size = seq_dignosis_codes.shape[0]
     t_diagnosis_codes, t_labels, t_mask = rnn_tools.pad_matrix_mine(seq_dignosis_codes, batch_labels, options)
     if options['use_gpu']:
         x = torch.Tensor(t_diagnosis_codes).cuda()
     else:
         x = torch.Tensor(t_diagnosis_codes)
     x = self.pre_embed(x)
     x = self.relu(x)
     features = torch.tanh(self.embedding_layer(x))
     weights = self.weight_layer(features)
     return features, t_labels, t_mask, torch.softmax(weights, 0)