You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. import os
  5. import sys
  6. PROJ_DIR = os.path.dirname(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..')))
  7. sys.path.insert(0, PROJ_DIR)
  8. from drug.models import GCN
  9. from drug.datasets import DDInteractionDataset
  10. from model.utils import get_FP_by_negative_index
  11. class Connector(nn.Module):
  12. def __init__(self, gpu_id=None):
  13. super(Connector, self).__init__()
  14. self.ddiDataset = DDInteractionDataset(gpu_id = gpu_id)
  15. self.gcn = GCN(self.ddiDataset.num_features, self.ddiDataset.num_features // 2)
  16. #Cell line features
  17. # np.load('cell_feat.npy')
  18. def forward(self, drug1_idx, drug2_idx, cell_feat):
  19. x = self.ddiDataset.get().x
  20. edge_index = self.ddiDataset.get().edge_index
  21. x = self.gcn(x, edge_index)
  22. drug1_idx = torch.flatten(drug1_idx)
  23. drug2_idx = torch.flatten(drug2_idx)
  24. drug1_feat = x[drug1_idx]
  25. drug2_feat = x[drug2_idx]
  26. for i, x in enumerate(drug1_idx):
  27. if x < 0:
  28. drug1_feat[i] = get_FP_by_negative_index(x)
  29. for i, x in enumerate(drug2_idx):
  30. if x < 0:
  31. drug2_feat[i] = get_FP_by_negative_index(x)
  32. feat = torch.cat([drug1_feat, drug2_feat, cell_feat], 1)
  33. return feat
  34. class MLP(nn.Module):
  35. def __init__(self, input_size: int, hidden_size: int, gpu_id=None):
  36. super(MLP, self).__init__()
  37. self.layers = nn.Sequential(
  38. nn.Linear(input_size, hidden_size),
  39. nn.ReLU(),
  40. nn.BatchNorm1d(hidden_size),
  41. nn.Linear(hidden_size, hidden_size // 2),
  42. nn.ReLU(),
  43. nn.BatchNorm1d(hidden_size // 2),
  44. nn.Linear(hidden_size // 2, 1)
  45. )
  46. self.connector = Connector(gpu_id)
  47. def forward(self, drug1_idx, drug2_idx, cell_feat): # prev input: self, drug1_feat: torch.Tensor, drug2_feat: torch.Tensor, cell_feat: torch.Tensor
  48. feat = self.connector(drug1_idx, drug2_idx, cell_feat)
  49. out = self.layers(feat)
  50. return out
  51. # other PRODeepSyn models have been deleted for now