How to use hierarchy_descriptions method in Lemoncheesecake

Best Python code snippet using lemoncheesecake

neural_network.py

Source:neural_network.py Github

copy

Full Screen

1# encoding: utf-82"""3@author: Kaiqi Yuan4@software: PyCharm5@file: neural_network.py6@time: 19-1-23 上午9:267@description:8"""9import torch10import torch.nn as nn11import numpy as np12from torch.autograd import Variable13from util.utility import hierarchy_cols_padding, hierarchy_rows_padding, hierarchy_path_align14class Lenet5Classifier(nn.Module):15 def __init__(self, feature_dim):16 super(Lenet5Classifier, self).__init__()17 self.device = torch.device('cuda:1')18 self.num_classes = 219 layer1_out_channels = 2020 layer2_out_channels = 2021 self.layer1 = nn.Sequential(22 nn.Conv2d(1, layer1_out_channels, kernel_size=5, stride=1, padding=2), # Conv1d, https://pytorch.org/docs/stable/nn.html#conv1d23 nn.Dropout(0.3),24 nn.BatchNorm2d(layer1_out_channels), # BatchNorm1d(channel_out), https://pytorch.org/docs/stable/nn.html#batchnorm1d25 nn.ReLU(),26 nn.MaxPool2d(kernel_size=2, stride=2)) # Maxpool1d, https://pytorch.org/docs/stable/nn.html#maxpool1d27 self.layer2 = nn.Sequential(28 nn.Conv2d(layer1_out_channels, layer2_out_channels, kernel_size=5, stride=1, padding=2),29 nn.Dropout(0.3),30 nn.BatchNorm2d(layer2_out_channels),31 nn.ReLU(),32 nn.MaxPool2d(kernel_size=2, stride=2))33 self.fc = nn.Linear(int(feature_dim/4)*1*layer2_out_channels, 2) # https://pytorch.org/docs/stable/nn.html#linear34 print("fc feature_dim: ", int(feature_dim/4)*1*layer2_out_channels)35 def forward(self, in_tensor):36 # in_tensor = x.type(torch.cuda.DoubleTensor)37 # print(in_tensor.type)38 # print("in_tensor: ", in_tensor.shape)39 # in_tensor: torch.Size([16, 1, feature_dim, 1])40 out = self.layer1(in_tensor)41 # print("layer1: ", out.shape)42 out = self.layer2(out)43 # print("layer2: ", out.shape)44 out = out.reshape((out.size(0), -1)) # [100, 7, 7, 32] -> [100, 1568])45 out = self.fc(out)46 return out47class Conv2dReduceDims(nn.Module):48 def __init__(self, H_in, W_in, fc_out_channels, device):49 super(Conv2dReduceDims, self).__init__()50 conv_out_channels = 2051 self.fc_out_channels = fc_out_channels52 self.W_in = W_in if W_in == 1 else int(W_in/2)53 self.device = device54 self.conv_layer = nn.Sequential(55 nn.Conv2d(1, conv_out_channels, kernel_size=5, stride=1, padding=2), # Conv1d, https://pytorch.org/docs/stable/nn.html#conv1d56 nn.BatchNorm2d(conv_out_channels), # BatchNorm1d(channel_out), https://pytorch.org/docs/stable/nn.html#batchnorm1d57 nn.ReLU(),58 nn.MaxPool2d(kernel_size=2, stride=2))59 self.fc = nn.Linear(int(H_in / 2) * self.W_in * conv_out_channels, fc_out_channels)60 # print("Conv2dReduceDims fc feature_dim: ", int(H_in / 2) * self.W_in * conv_out_channels)61 def forward(self, x):62 # print("input: ", x.size())63 x = x.double().to(self.device)64 out = self.conv_layer(x)65 # print("conv_layer out: ", out.size())66 out = out.reshape((out.size(0), -1))67 # print("Conv2dReduceDims: ", out.shape)68 out = self.fc(out)69 return out70 def get_out_dims(self):71 return self.fc_out_channels72class Conv2dPharmacology(nn.Module):73 def __init__(self, feature_dim):74 super(Conv2dPharmacology, self).__init__()75 self.conv_out_channels = 1076 self.feature_dim = feature_dim77 self.cnn1 = nn.Sequential(78 nn.Conv2d(1, self.conv_out_channels, kernel_size=5, stride=2, padding=2),79 nn.BatchNorm2d(self.conv_out_channels),80 nn.ReLU(),81 nn.MaxPool2d(kernel_size=10, stride=5, padding=5))82 self.cnn2 = nn.Sequential(83 nn.Conv2d(self.conv_out_channels, self.conv_out_channels, kernel_size=5, stride=2, padding=2),84 nn.BatchNorm2d(self.conv_out_channels),85 nn.ReLU(),86 nn.MaxPool2d(kernel_size=10, stride=5, padding=5))87 def forward(self, x):88 # TODO: test the size of out89 # x = x.double()90 out = self.cnn1(x)91 # print("cnn1 out:", out.size())92 out = self.cnn2(out)93 # print("cnn2 out:", out.size())94 return out.squeeze()95 def get_flatten_out_dim(self):96 cnn1_out = int(((self.feature_dim-1)/2 + 1) / 5) + 197 cnn2_out = int(((cnn1_out-1)/2 + 1)/5) + 198 return cnn2_out * self.conv_out_channels99def test_Conv2dPharmacology():100 layer1_out_channels = 10101 layer2_out_channels = 10102 conv_out_channels = 10103 inputs = torch.randn((6, 1, 5080, 1))104 # layer1 = nn.Sequential(105 # nn.Conv2d(1, conv_out_channels, kernel_size=5, stride=2, padding=2),106 # nn.BatchNorm2d(conv_out_channels),107 # nn.ReLU(),108 # nn.MaxPool2d(kernel_size=10, stride=5, padding=5))# Maxpool1d, https://pytorch.org/docs/stable/nn.html#maxpool1d109 # layer2 = nn.Sequential(110 # nn.Conv2d(conv_out_channels, conv_out_channels, kernel_size=5, stride=2, padding=2),111 # nn.BatchNorm2d(conv_out_channels),112 # nn.ReLU(),113 # nn.MaxPool2d(kernel_size=10, stride=5, padding=5))114 # outputs = layer1(inputs)115 # print("layer1 outputs: ", outputs.size())116 # outputs = layer2(outputs)117 cnn = Conv2dPharmacology(5080)118 outputs = cnn(inputs)119 print("layer2 outputs: ", outputs.size(), outputs.squeeze().size(), cnn.get_out_dim()) # torch.Size([6, 10, 52, 1]) torch.Size([6, 10, 52]) 52120class BilstmDescription(nn.Module):121 def __init__(self, input_size, device, out_layers=500):122 super(BilstmDescription, self).__init__()123 self.input_size = input_size124 self.hidden_size = 128125 self.num_layers = 2126 self.device = device127 self.batch_first = True128 self.out_layers = out_layers129 self.lstm = nn.LSTM(input_size, self.hidden_size, self.num_layers, batch_first=True, bidirectional=True)130 self.fc = nn.Linear(self.hidden_size * 2, out_layers) # 2 for bidirection131 def forward(self, x):132 x, x_len = hierarchy_cols_padding(x, self.input_size)133 # Set initial states134 h0 = Variable(torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size)).double().to(self.device) # 2 for bidirection135 c0 = Variable(torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size)).double().to(self.device)136 """sort"""137 x_sort_idx = np.argsort(-x_len)138 x_unsort_idx = torch.LongTensor(np.argsort(x_sort_idx))139 x_len = x_len[x_sort_idx]140 x = x[torch.LongTensor(x_sort_idx)]141 """pack"""142 x_emb_p = torch.nn.utils.rnn.pack_padded_sequence(x, x_len, batch_first=self.batch_first).to(self.device)143 """process using RNN"""144 out_pack, _ = self.lstm(x_emb_p, (h0, c0))145 """unpack: out"""146 out, _ = torch.nn.utils.rnn.pad_packed_sequence(out_pack, batch_first=self.batch_first) # (sequence, lengths)147 out = self.fc(out[:, -1, :])148 """unsort: out"""149 out = out[x_unsort_idx]150 # """unsort: c"""151 # ct = torch.transpose(ct, 0, 1)[152 # x_unsort_idx] # (num_layers * num_directions, batch, hidden_size) -> (batch, ...)153 # ct = torch.transpose(ct, 0, 1)154 # return out, (ht, ct)155 return out156 # def pad_description(self, x):157 # """description_len长度不一,需要进行对齐"""158 # # x.size(): (path_len, description_len, word2vec_len)159 # # max_description_len to pad160 # x_len = []161 # for x_description in x:162 # x_len.append(len(x_description))163 #164 # max_description_len = max(x_len)165 # new_x = []166 # for i in range(len(x)):167 # new_x_description = np.zeros((max_description_len, self.input_size))168 # new_x_description[0: x_len[i]] = x[i]169 # new_x.append(new_x_description)170 #171 # return torch.DoubleTensor(new_x), np.array(x_len)172 def get_out_dims(self):173 return self.out_layers174class GruHierarchy(nn.Module):175 def __init__(self, input_size, hidden_size, device):176 super(GruHierarchy, self).__init__()177 self.input_size = input_size178 self.hidden_size = hidden_size179 self.num_layers = 2180 self.device = device181 self.batch_first = True182 self.bidirectional = True183 self.directions = 2 if self.bidirectional else 1184 self.out_layers = 100185 self.GRU = nn.GRU(input_size,186 self.hidden_size,187 self.num_layers,188 batch_first=self.batch_first,189 bidirectional=self.bidirectional)190 # print("GRU param: ",191 # "\nGRU.hidden_size: ", self.GRU.hidden_size,192 # "\nGRU.bidirectional: ", self.GRU.bidirectional,193 # "\nGRU.input_size: ", self.GRU.input_size,194 # "\nGRU.bidirectional: ", self.GRU.bidirectional,195 # "\nGRU.training: ", self.GRU.training,196 # "\nGRU.batch_first: ", self.GRU.batch_first,197 # "\nGRU.bias: ", self.GRU.bias,198 # "\nGRU.num_layers: ", self.num_layers)199 # print("self.device: ", self.device, "input_size: ", self.input_size)200 def forward(self, x):201 # print("GruHierarchy")202 x, x_len = hierarchy_cols_padding(x, self.input_size)203 batch_size = x.size(0)204 # print("x: ", type(x), x.size())205 h0 = Variable(torch.randn(self.num_layers * self.directions, batch_size, self.hidden_size)).double().to(self.device) # 2 for bidirection206 # print("forward: x:", x.size(), "h0:", h0.size())207 """sort"""208 x_sort_idx = np.argsort(-x_len)209 x_unsort_idx = torch.LongTensor(np.argsort(x_sort_idx))210 x_len = x_len[x_sort_idx]211 x = x[torch.LongTensor(x_sort_idx)]212 """pack"""213 x_emb_p = torch.nn.utils.rnn.pack_padded_sequence(x, x_len, batch_first=self.batch_first).to(self.device)214 """process using RNN"""215 out_pack, _ = self.GRU(x_emb_p, h0)216 """unpack: out"""217 out, _ = torch.nn.utils.rnn.pad_packed_sequence(out_pack, batch_first=self.batch_first) # (sequence, lengths)218 # out = self.fc(out[:, -1, :])219 """unsort: out"""220 out = out[x_unsort_idx]221 return out, x_len #.permute(1, 0, 2) # (seq_len, batch, input_size) -> (batch, seq_len, input_size)222 def get_out_dims(self):223 return self.hidden_size * self.directions224def process_hierarchy_description(description_lstm, hierarchy_gru_description, hierarchy_descriptions):225 tmp_outs = []226 # print("batch_hierarchy_description: ", len(hierarchy_descriptions))227 for descriptions in hierarchy_descriptions:228 out = description_lstm(descriptions) # path_len * self.description_lstm.get_out_dims()229 # print("description lstm out: ", out.size(), type(out), torch.is_tensor(out))230 tmp_outs.append(out.cpu().detach().numpy())231 batch_gru_outs, x_len = hierarchy_gru_description(tmp_outs) # tmp_outs: [batch_size, 10, num_directions*hidder_size]232 # print("x_len: ", type(x_len), x_len)233 # print("batch_gru_outs, x_len = self.hierarchy_gru(tmp_outs) ", batch_gru_outs.size())234 batch_gru_outs = hierarchy_path_align(batch_gru_outs, x_len) # batch内对齐(每个batch内以最长的路径为准)235 # print("hierarchy_path_align(batch_gru_outs, x_len", batch_gru_outs.size())236 # 整个数据集内对齐237 batch_outs = []238 batch_outs_numpy = batch_gru_outs.cpu().detach().numpy()239 for outs in batch_outs_numpy:240 batch_outs.append(hierarchy_rows_padding(10, hierarchy_gru_description.get_out_dims(), outs))241 return torch.Tensor(batch_outs)242def process_hierarchy_deepwalk(hierarchy_gru_deepwalk, drug_deepwalks):243 batch_gru_outs, x_len = hierarchy_gru_deepwalk(drug_deepwalks)244 batch_gru_outs = hierarchy_path_align(batch_gru_outs, x_len) # batch内对齐(每个batch内以最长的路径为准)245 # 整个数据集内对齐246 batch_outs = []247 batch_outs_numpy = batch_gru_outs.cpu().detach().numpy()248 for outs in batch_outs_numpy:249 batch_outs.append(hierarchy_rows_padding(10, hierarchy_gru_deepwalk.get_out_dims(), outs))250 return torch.Tensor(batch_outs)251def pad_description_test(x):252 tt = []253 tt.append(np.arange(4).reshape((1, 4)))254 tt.append(np.arange(8).reshape((2, 4)))255 tt.append(np.arange(12).reshape((3, 4)))256 tt.append(np.arange(16).reshape((4, 4)))257 tt.append(np.arange(20).reshape((5, 4)))258 def func_test(x):259 x_len = []260 input_size = len(x[0][0])261 for x_description in x:262 x_len.append(len(x_description))263 max_description_len = max(x_len)264 new_x = []265 for i in range(len(x)):266 new_x_description = np.zeros((max_description_len, input_size))267 new_x_description[0: x_len[i]] = x[i]268 new_x.append(new_x_description)269 return new_x, x_len270 print(func_test(tt))271def dynamic_lstm_test():272 """pytorch中的动态lstm测试样例"""273 sent_1 = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7], [0, 0, 0, 0]]274 sent_2 = [[10, 11, 12, 13], [12, 13, 14, 15], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]275 sent_3 = [[30, 31, 32, 34], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]276 sent_4 = [[40, 41, 42, 44], [41, 42, 43, 44], [42, 43, 44, 45], [43, 44, 45, 46], [44, 45, 46, 47]]277 sent_5 = [[50, 51, 52, 53], [51, 52, 53, 54], [52, 53, 54, 55], [0, 0, 0, 0], [0, 0, 0, 0]]278 text = np.zeros((5, 5, 4))279 text[0] = sent_1280 text[1] = sent_2281 text[2] = sent_3282 text[3] = sent_4283 text[4] = sent_5284 text = torch.DoubleTensor(text)285 sent_len = np.array([4, 2, 1, 5, 3])286 print("text.shape: ", text.shape)287 def func_test(x, x_len):288 x_sort_idx = np.argsort(-x_len)289 x_unsort_idx = torch.LongTensor(np.argsort(x_sort_idx))290 x_len = x_len[x_sort_idx]291 x = x[torch.LongTensor(x_sort_idx)]292 """pack"""293 x_emb_p = torch.nn.utils.rnn.pack_padded_sequence(x, x_len, batch_first=True)294 rnn = nn.GRU(4, 3, 2, batch_first=True).double()295 # rnn = nn.LSTM(4, 3, 2, batch_first=True).double()296 h0 = Variable(torch.randn(2, 5, 3)).double()297 c0 = Variable(torch.randn(2, 5, 3)).double()298 out, _ = rnn(x_emb_p, h0)299 # out, _ = rnn(x_emb_p, (h0, c0))300 # unpack301 unpacked, unpacked_len = torch.nn.utils.rnn.pad_packed_sequence(out, batch_first=True)302 print("pad_packed_sequence: ", unpacked.size())303 print("unpacked[0]: ", unpacked[0].size())304 print(unpacked)305 fc = nn.Linear(3, 5).double()306 unpacked = unpacked[:, -1, :]307 print("out[:, -1, :]: ", unpacked.size())308 fc_out = fc(unpacked)309 print("fc_out: ", fc_out.size())310 print(fc_out)311 func_test(text, sent_len)312if __name__ == '__main__':...

Full Screen

Full Screen

pharmacology_context_deepwalk.py

Source:pharmacology_context_deepwalk.py Github

copy

Full Screen

1# encoding: utf-82"""3@author: Kaiqi Yuan4@software: PyCharm5@file: pharmacology_context_deepwalk.py6@time: 2019/2/13 22:287@description: 药物cnn降维至5008 将药物描述语句经过lstm, 构建context matrix9 拼接deepwalk10 拼接target_seq11 放到cnn分类器中处理12"""13import sys14sys.path.extend(['/home/kqyuan/DDISuccess', '/home/kqyuan/DDISuccess/util',15 '/home/lei/DDISuccess', '/home/lei/DDISuccess/util'])16import torch17from sklearn import metrics18from torch import nn19from torch.utils import data20from model.pharmacology_cnn import BaseModel21from util.data_loader import PharmacologyDescriptionDeepwalkDataset22from util.neural_network import Conv2dReduceDims, BilstmDescription, Lenet5Classifier, GruHierarchy, GruHierarchy_123from util.utility import hierarchy_rows_padding, hierarchy_path_align24class PharmacologyContextDeepwalkModel(BaseModel):25 def __init__(self, dataset, train_data_file, model_save_path='pharmacology_context_model.ckpt'):26 super().__init__(dataset, train_data_file, model_save_path)27 print("self.BATCH_SIZE: ", self.BATCH_SIZE)28 print("self.NUM_EPOCHS: ", self.NUM_EPOCHS)29 self.device_2 = torch.device('cuda:3')30 def train(self):31 pharmacology_feature_dim = self.train_dataset.get_pharmacologicy_feature_dim()32 target_seq_len = self.train_dataset.get_target_seq_len()33 description_wordembedding_dim = self.train_dataset.get_description_wordembedding_dim()34 deepwalk_feature_dim = self.train_dataset.get_deepwalk_feature_dim()35 print("pharmacology_feature_dim: ", pharmacology_feature_dim)36 print("target_seq_len ", target_seq_len)37 print("description_wordembedding_dim: ", description_wordembedding_dim)38 print("hierarchy_feature_dim: ", deepwalk_feature_dim)39 self.pharmacology_cnn = Conv2dReduceDims(pharmacology_feature_dim, 1, 500, self.device)40 self.pharmacology_cnn.double()41 self.pharmacology_cnn.to(self.device)42 self.description_lstm = BilstmDescription(description_wordembedding_dim, self.device)43 self.description_lstm.double()44 self.description_lstm.to(self.device)45 self.hierarchy_gru_description = GruHierarchy(self.description_lstm.get_out_dims(), 25, self.device)46 self.hierarchy_gru_description.double()47 self.hierarchy_gru_description.to(self.device)48 self.hierarchy_gru_deepwalk = GruHierarchy(deepwalk_feature_dim, 25, self.device)49 self.hierarchy_gru_deepwalk.double()50 self.hierarchy_gru_deepwalk.to(self.device)51 # self.hierarchy_gru_deepwalk.to(self.device_2)52 drug_vec_dim = self.pharmacology_cnn.get_out_dims() + 10 * self.hierarchy_gru_description.get_out_dims() + 10 * self.hierarchy_gru_deepwalk.get_out_dims()53 self.target_cnn = Conv2dReduceDims(target_seq_len, self.train_dataset.get_target_seq_width(), drug_vec_dim, self.device_2)54 self.target_cnn.double()55 self.target_cnn.to(self.device_2)56 classifier_in_channels = drug_vec_dim * 257 print("classifier feature dim: ", classifier_in_channels)58 self.classifier = Lenet5Classifier(feature_dim=classifier_in_channels).to(self.device)59 self.classifier.double()60 self.classifier.to(self.device)61 # Loss and optimizer62 criterion = nn.CrossEntropyLoss()63 # classifier.parameters(). Returns an iterator over module parameters. This is typically passed to an optimizer.64 optimizer = torch.optim.SGD(self.classifier.parameters(), lr=self.LEARNING_RATE)65 # Train the classifier66 total_step = len(self.train_loader)67 for epoch in range(self.NUM_EPOCHS):68 correct = 069 total = 070 for i, (phy_features, target_seqs, labels) in enumerate(self.train_loader):71 hierarchy_description, drug_deepwalk = self.train_dataset.feed_batch_data(i, self.BATCH_SIZE)72 phy_features = phy_features.double().to(self.device)73 phy_features = self.pharmacology_cnn(phy_features)74 this_batch_size = len(labels) # torch.Size([100, 1, 28, 28])75 drug_deepwalk = self.process_hierarchy_deepwalk(drug_deepwalk).double().to(self.device).reshape((this_batch_size, -1))76 hierarchy_description = self.process_hierarchy_description(hierarchy_description).double().to(self.device).reshape((this_batch_size, -1))77 # drug_deepwalk = drug_deepwalk.double().to(self.device).reshape((this_batch_size, -1))78 # target_seqs = target_seqs.double().to(self.device_2)79 target_seqs = self.target_cnn(target_seqs).to(self.device)80 labels = labels.to(self.device) # torch.Size([100])81 # Forward pass82 # print("phy_features.size()", phy_features.size())83 # print("target_seqs.size()", target_seqs.size())84 # print("hierarchy_description.size()", hierarchy_description.size())85 inputs = torch.cat((phy_features, target_seqs, hierarchy_description, drug_deepwalk), 1).reshape(86 (this_batch_size, 1, -1, 1))87 # print("inputs:", inputs.size())88 outputs = self.classifier(inputs)89 loss = criterion(outputs, labels)90 # Backward and optimize91 # 根据pytorch中的backward()函数的计算,当网络参量进行反馈时,梯度是被积累的而不是被替换掉;92 # 但是在每一个batch时毫无疑问并不需要将两个batch的梯度混合起来累积,因此这里就需要每个batch设置一遍zero_grad 了93 optimizer.zero_grad() # Sets gradients of all classifier parameters to zero.94 loss.backward()95 optimizer.step()96 total, correct = self.calculate_accuracy(outputs, labels, total, correct)97 if (i + 1) % 32 == 0:98 print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Accuracy: {:.2f}%'99 .format(epoch + 1, self.NUM_EPOCHS, i + 1, total_step, loss.item(), correct / total * 100))100 # # Test the classifier101 # self.classifier.eval()102 def test(self, test_data_file):103 test_dataset = self.dataset_func(test_data_file)104 test_loader = data.DataLoader(dataset=test_dataset,105 batch_size=self.BATCH_SIZE,106 shuffle=True)107 with torch.no_grad():108 correct = 0109 total = 0110 y_true = None111 y_pred = None112 pred_score = None113 for i, (phy_features, target_seqs, labels) in enumerate(test_loader):114 hierarchy_description, drug_deepwalk = test_dataset.feed_batch_data(i, self.BATCH_SIZE)115 phy_features = phy_features.double().to(self.device)116 phy_features = self.pharmacology_cnn(phy_features)117 # print("phy_features.size()", phy_features.size())118 # print("target_seqs.size()", target_seqs.size())119 # print("hierarchy_description.size()", len(hierarchy_description))120 # print("drug_deepwalk.size()", len(drug_deepwalk))121 # print("this_batch_size: ", len(labels))122 # print("labels: ",labels)123 this_batch_size = len(labels)124 hierarchy_description = self.process_hierarchy_description(hierarchy_description)125 # print("self.process_hierarchy_description(hierarchy_description): ", hierarchy_description.size())126 hierarchy_description = hierarchy_description.double().to(self.device).reshape((this_batch_size, -1))127 drug_deepwalk = self.process_hierarchy_deepwalk(drug_deepwalk).double().to(self.device).reshape((this_batch_size, -1))128 # target_seqs = target_seqs.double().to(self.device).reshape((this_batch_size, -1))129 target_seqs = self.target_cnn(target_seqs).to(self.device)130 labels = labels.to(self.device) # torch.Size([100])131 # Forward pass132 inputs = torch.cat((phy_features, target_seqs, hierarchy_description, drug_deepwalk), 1).reshape(133 (this_batch_size, 1, -1, 1))134 outputs = self.classifier(inputs)135 y_true, total, correct, y_pred, pred_score = self.eval_model(outputs, labels, total, correct, y_true,136 y_pred, pred_score)137 print("************test**************")138 accuracy = correct / total139 precision, recall, f1, _ = metrics.precision_recall_fscore_support(y_true, y_pred)140 auroc = metrics.roc_auc_score(y_true,141 pred_score) # https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_auc_score.html#sklearn.metrics.roc_auc_score142 aupr_precision, aupr_recall, _ = metrics.precision_recall_curve(y_true, pred_score, pos_label=1)143 aupr = metrics.auc(aupr_recall, aupr_precision)144 print("test dataset: ", test_data_file)145 print("auroc: ", auroc)146 print("accuracy: ", accuracy)147 print("precision: ", precision)148 print("recall: ", recall)149 print("f1: ", f1)150 print("aupr: ", aupr)151 # # Save the classifier checkpoint152 torch.save(self.classifier.state_dict(), self.model_save_path)153 def process_hierarchy_description(self, hierarchy_descriptions):154 tmp_outs = []155 # print("batch_hierarchy_description: ", len(hierarchy_descriptions))156 for descriptions in hierarchy_descriptions:157 out = self.description_lstm(descriptions) # path_len * self.description_lstm.get_out_dims()158 # print("description lstm out: ", out.size(), type(out), torch.is_tensor(out))159 tmp_outs.append(out.cpu().detach().numpy())160 batch_gru_outs, x_len = self.hierarchy_gru_description(tmp_outs) # tmp_outs: [batch_size, 10, num_directions*hidder_size]161 # print("x_len: ", type(x_len), x_len)162 # print("batch_gru_outs, x_len = self.hierarchy_gru(tmp_outs) ", batch_gru_outs.size())163 batch_gru_outs = hierarchy_path_align(batch_gru_outs, x_len) # batch内对齐(每个batch内以最长的路径为准)164 # print("hierarchy_path_align(batch_gru_outs, x_len", batch_gru_outs.size())165 # 整个数据集内对齐166 batch_outs = []167 batch_outs_numpy = batch_gru_outs.cpu().detach().numpy()168 for outs in batch_outs_numpy:169 batch_outs.append(hierarchy_rows_padding(10, self.hierarchy_gru_description.get_out_dims(), outs))170 return torch.Tensor(batch_outs)171 def process_hierarchy_deepwalk(self, drug_deepwalks):172 batch_gru_outs, x_len = self.hierarchy_gru_deepwalk(drug_deepwalks) # drug_deepwalks: [batch_size, path_len, num_directions*hidder_size]173 # print("x_len: ", type(x_len), x_len)174 batch_gru_outs = hierarchy_path_align(batch_gru_outs, x_len) # batch内对齐(每个batch内以最长的路径为准)175 # 整个数据集内对齐176 batch_outs = []177 batch_outs_numpy = batch_gru_outs.cpu().detach().numpy()178 for outs in batch_outs_numpy:179 batch_outs.append(hierarchy_rows_padding(10, self.hierarchy_gru_deepwalk.get_out_dims(), outs))180 return torch.Tensor(batch_outs)181 # # [this_batch_size, path_len, 128]182 # batch_outs = []183 # for hierarchy_deepwalk in drug_deepwalks:184 # print("hierarchy_deepwalk is numpy: ", hierarchy_deepwalk.shape)185 # hierarchy_deepwalk = hierarchy_rows_padding(hierarchy_deepwalk.shape[0], 500, hierarchy_deepwalk)186 # hierarchy_deepwalk = torch.Tensor(hierarchy_deepwalk).to(self.device_2)187 # hierarchy_deepwalk = hierarchy_deepwalk.reshape((1, hierarchy_deepwalk.size(0), hierarchy_deepwalk.size(1)))188 # print("hierarchy_deepwalk is tensor: ", hierarchy_deepwalk.size())189 # out = self.hierarchy_gru_deepwalk(hierarchy_deepwalk)190 # print("out size: ", out.size())191 # print("out type: ", type(out))192 # out = out.squeeze().cpu().detach().numpy()193 # out_dims = self.hierarchy_gru_description.get_out_dims()194 # print("before pad: ", out.shape, "out_dims: ", out_dims)195 # out_vec = hierarchy_rows_padding(10, out_dims, out)196 # out_vec = out_vec.reshape(10 * out_dims) # multiple dimension to single dimension197 # batch_outs.append(out_vec)198 #199 # # batch_outs = hierarchy_padding(10, self.hierarchy_gru.out_dims, batch_outs)200 # batch_outs = torch.Tensor(batch_outs)201 # # print("batch_out size: ", batch_outs.size())202 # return batch_outs203if __name__ == '__main__':204 # model = PharmacologyContextDeepwalkModel(PharmacologyDescriptionDeepwalkDataset, "../Data/DTI/e_train.pickle", "e_pharmacology_context_deepwalk_model.ckpt")205 # model.train()206 # model.classifier.eval()207 # model.test("../Data/DTI/e_test.pickle")208 # model = PharmacologyContextDeepwalkModel(PharmacologyDescriptionDeepwalkDataset, "../Data/DTI/ic_train.pickle", "ic_pharmacology_context_deepwalk_model.ckpt")209 # model.train()210 # model.classifier.eval()211 # model.test("../Data/DTI/ic_test.pickle")212 #213 model = PharmacologyContextDeepwalkModel(PharmacologyDescriptionDeepwalkDataset, "../Data/DTI/gpcr_train.pickle", "gpcr_pharmacology_context_deepwalk_model.ckpt")214 model.train()215 model.classifier.eval()...

Full Screen

Full Screen

pharmacology_context.py

Source:pharmacology_context.py Github

copy

Full Screen

1# encoding: utf-82"""3@author: Kaiqi Yuan4@software: PyCharm5@file: pharmacology_context.py6@time: 19-1-24 下午8:507@description: 药物cnn降维至5008 将药物描述语句经过lstm, 构建context matrix9 拼接target_seq10 放到cnn分类器中处理11"""12import sys13sys.path.extend(['/home/kqyuan/DDISuccess', '/home/kqyuan/DDISuccess/util',14 '/home/lei/DDISuccess', '/home/lei/DDISuccess/util'])15import torch16from sklearn import metrics17from torch import nn18from torch.utils import data19from model.pharmacology_cnn import BaseModel20from util.data_loader import PharmacologyDescriptionDataset21from util.neural_network import Conv2dReduceDims, BilstmDescription, Lenet5Classifier, GruHierarchy22from util.utility import hierarchy_rows_padding, hierarchy_path_align23class PharmacologyContextModel(BaseModel):24 def __init__(self, dataset, train_data_file, model_save_path='pharmacology_context_model.ckpt'):25 super().__init__(dataset, train_data_file, model_save_path)26 # self.NUM_EPOCHS = 127 print("self.NUM_EPOCHS: ", self.NUM_EPOCHS)28 def train(self):29 pharmacology_feature_dim = self.train_dataset.get_pharmacologicy_feature_dim()30 target_seq_len = self.train_dataset.get_target_seq_len()31 description_wordembedding_dim = self.train_dataset.get_description_wordembedding_dim()32 print("pharmacology_feature_dim: ", pharmacology_feature_dim)33 print("target_seq_len ", target_seq_len)34 print("description_wordembedding_dim: ", description_wordembedding_dim)35 self.pharmacology_cnn = Conv2dReduceDims(pharmacology_feature_dim)36 self.pharmacology_cnn.double()37 self.pharmacology_cnn.to(self.device)38 self.description_lstm = BilstmDescription(description_wordembedding_dim, self.device)39 self.description_lstm.double()40 self.description_lstm.to(self.device)41 self.hierarchy_gru = GruHierarchy(self.description_lstm.get_out_dims(), self.device)42 self.hierarchy_gru.double()43 self.hierarchy_gru.to(self.device)44 classifier_in_channels = 500 + target_seq_len + 10*self.hierarchy_gru.get_out_dims() # the output of cnn for reduce dims is self.description_lstm.get_out_dims()45 print("classifier_in_channels: ", classifier_in_channels)46 self.classifier = Lenet5Classifier(feature_dim=classifier_in_channels).to(self.device)47 self.classifier.double()48 self.classifier.to(self.device)49 # Loss and optimizer50 criterion = nn.CrossEntropyLoss()51 # classifier.parameters(). Returns an iterator over module parameters. This is typically passed to an optimizer.52 optimizer = torch.optim.SGD(self.classifier.parameters(), lr=self.LEARNING_RATE)53 # Train the classifier54 total_step = len(self.train_loader)55 for epoch in range(self.NUM_EPOCHS):56 correct = 057 total = 058 for i, (phy_features, target_seqs, labels) in enumerate(self.train_loader):59 phy_features = phy_features.double().to(self.device)60 phy_features = self.pharmacology_cnn(phy_features)61 this_batch_size = len(labels)62 hierarchy_description = self.train_dataset.feed_batch_data(i, self.BATCH_SIZE)63 hierarchy_description = self.process_hierarchy_description(hierarchy_description).double().to(self.device).reshape((this_batch_size, -1)) # torch.Size([100, 1, 28, 28])64 target_seqs = target_seqs.double().to(self.device).reshape((this_batch_size, -1)) # torch.Size([100, 1, 28, 28])65 labels = labels.to(self.device) # torch.Size([100])66 # Forward pass67 # print("phy_features.size()", phy_features.size())68 # print("target_seqs.size()", target_seqs.size())69 # print("hierarchy_description.size()", hierarchy_description.size())70 inputs = torch.cat((phy_features, target_seqs, hierarchy_description), 1).reshape((this_batch_size, 1, -1, 1))71 # print("inputs:", inputs.size())72 outputs = self.classifier(inputs)73 loss = criterion(outputs, labels)74 # Backward and optimize75 # 根据pytorch中的backward()函数的计算,当网络参量进行反馈时,梯度是被积累的而不是被替换掉;76 # 但是在每一个batch时毫无疑问并不需要将两个batch的梯度混合起来累积,因此这里就需要每个batch设置一遍zero_grad 了77 optimizer.zero_grad() # Sets gradients of all classifier parameters to zero.78 loss.backward()79 optimizer.step()80 total, correct = self.calculate_accuracy(outputs, labels, total, correct)81 if (i + 1) % 32 == 0:82 print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Accuracy: {:.2f}%'83 .format(epoch + 1, self.NUM_EPOCHS, i + 1, total_step, loss.item(), correct / total * 100))84 # # Test the classifier85 # self.classifier.eval()86 def test(self, test_data_file):87 test_dataset = self.dataset_func(test_data_file)88 test_loader = data.DataLoader(dataset=test_dataset,89 batch_size=self.BATCH_SIZE,90 shuffle=True)91 with torch.no_grad():92 correct = 093 total = 094 y_true = None95 y_pred = None96 pred_score = None97 for i, (phy_features, target_seqs, labels) in enumerate(test_loader):98 phy_features = phy_features.double().to(self.device)99 phy_features = self.pharmacology_cnn(phy_features)100 this_batch_size = len(labels)101 hierarchy_description = test_dataset.feed_batch_data(i, self.BATCH_SIZE)102 hierarchy_description = self.process_hierarchy_description(hierarchy_description).reshape((this_batch_size, -1)).double().to(self.device) # torch.Size([100, 1, 28, 28])103 target_seqs = target_seqs.double().to(self.device).reshape((this_batch_size, -1)) # torch.Size([100, 1, 28, 28])104 labels = labels.to(self.device) # torch.Size([100])105 # Forward pass106 inputs = torch.cat((phy_features, target_seqs, hierarchy_description), 1).reshape(107 (this_batch_size, 1, -1, 1))108 outputs = self.classifier(inputs)109 y_true, total, correct, y_pred, pred_score = self.eval_model(outputs, labels, total, correct, y_true,110 y_pred, pred_score)111 print("************test**************")112 accuracy = correct / total113 precision, recall, f1, _ = metrics.precision_recall_fscore_support(y_true, y_pred)114 auroc = metrics.roc_auc_score(y_true,115 pred_score) # https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_auc_score.html#sklearn.metrics.roc_auc_score116 aupr_precision, aupr_recall, _ = metrics.precision_recall_curve(y_true, pred_score, pos_label=1)117 aupr = metrics.auc(aupr_recall, aupr_precision)118 print("test dataset: ", test_data_file)119 print("auroc: ", auroc)120 print("accuracy: ", accuracy)121 print("precision: ", precision)122 print("recall: ", recall)123 print("f1: ", f1)124 print("aupr: ", aupr)125 # # Save the classifier checkpoint126 torch.save(self.classifier.state_dict(), self.model_save_path)127 def process_hierarchy_description(self, hierarchy_descriptions):128 tmp_outs = []129 for descriptions in hierarchy_descriptions:130 out = self.description_lstm(descriptions) # path_len * self.description_lstm.get_out_dims()131 # print("description lstm out: ", out.size(), type(out), torch.is_tensor(out))132 tmp_outs.append(out.cpu().detach().numpy())133 batch_gru_outs, x_len = self.hierarchy_gru(tmp_outs) # tmp_outs: [batch_size, 10, num_directions*hidder_size]134 # print("x_len: ", type(x_len), x_len)135 batch_gru_outs = hierarchy_path_align(batch_gru_outs, x_len) # batch内对齐(每个batch内以最长的路径为准)136 # 整个数据集内对齐137 batch_outs = []138 batch_outs_numpy = batch_gru_outs.cpu().detach().numpy()139 for outs in batch_outs_numpy:140 batch_outs.append(hierarchy_rows_padding(10, self.hierarchy_gru.get_out_dims(), outs))141 return torch.tensor(batch_outs)142if __name__ == '__main__':143 # model = PharmacologyContextModel(PharmacologyDescriptionDataset, "../Data/DTI/e_train.pickle", "e_pharmacology_context_model.ckpt")144 # model.train()145 # model.classifier.eval()146 # model.test("../Data/DTI/e_test.pickle")147 # model = PharmacologyContextModel(PharmacologyDescriptionDataset, "../Data/DTI/ic_train.pickle", "ic_pharmacology_context_model.ckpt")148 # model.train()149 # model.classifier.eval()150 # model.test("../Data/DTI/ic_test.pickle")151 #152 model = PharmacologyContextModel(PharmacologyDescriptionDataset, "../Data/DTI/gpcr_train.pickle",153 "gpcr_pharmacology_context_model.ckpt")154 model.train()155 model.classifier.eval()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Lemoncheesecake automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful