torch-molecule 0.1.0.dev2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- torch_molecule/__init__.py +70 -0
- torch_molecule/base/__init__.py +8 -0
- torch_molecule/base/base.py +396 -0
- torch_molecule/base/encoder.py +44 -0
- torch_molecule/base/generator.py +24 -0
- torch_molecule/base/old_encoder.py +322 -0
- torch_molecule/base/old_predictor.py +904 -0
- torch_molecule/base/old_predictor2.py +436 -0
- torch_molecule/base/predictor.py +112 -0
- torch_molecule/encoder/attrmask/__init__.py +3 -0
- torch_molecule/encoder/attrmask/model.py +113 -0
- torch_molecule/encoder/attrmask/modeling_attrmask.py +304 -0
- torch_molecule/encoder/constant.py +13 -0
- torch_molecule/encoder/contextpred/__init__.py +3 -0
- torch_molecule/encoder/contextpred/model.py +207 -0
- torch_molecule/encoder/contextpred/modeling_contextpred.py +299 -0
- torch_molecule/encoder/contextpred/utils.py +206 -0
- torch_molecule/encoder/edgepred/__init__.py +3 -0
- torch_molecule/encoder/edgepred/model.py +104 -0
- torch_molecule/encoder/edgepred/modeling_edgepred.py +285 -0
- torch_molecule/encoder/graphmae/__init__.py +3 -0
- torch_molecule/encoder/graphmae/dataloader.py +206 -0
- torch_molecule/encoder/graphmae/model.py +137 -0
- torch_molecule/encoder/graphmae/modeling_graphmae.py +323 -0
- torch_molecule/encoder/infograph/__init__.py +3 -0
- torch_molecule/encoder/infograph/loss_function.py +128 -0
- torch_molecule/encoder/infograph/model.py +92 -0
- torch_molecule/encoder/infograph/modeling_infograph.py +301 -0
- torch_molecule/encoder/infograph/module.py +31 -0
- torch_molecule/encoder/moama/__init__.py +3 -0
- torch_molecule/encoder/moama/model.py +131 -0
- torch_molecule/encoder/moama/modeling_moama.py +307 -0
- torch_molecule/encoder/moama/utils.py +143 -0
- torch_molecule/encoder/pretrained/__init__.py +3 -0
- torch_molecule/encoder/pretrained/modeling_pretrained.py +274 -0
- torch_molecule/encoder/supervised/__init__.py +3 -0
- torch_molecule/encoder/supervised/model.py +97 -0
- torch_molecule/encoder/supervised/modeling_supervised.py +354 -0
- torch_molecule/generator/digress/__init__.py +3 -0
- torch_molecule/generator/digress/diffusion.py +194 -0
- torch_molecule/generator/digress/modeling_digress.py +503 -0
- torch_molecule/generator/digress/transformer.py +360 -0
- torch_molecule/generator/digress/utils.py +196 -0
- torch_molecule/generator/gdss/__init__.py +3 -0
- torch_molecule/generator/gdss/layers.py +257 -0
- torch_molecule/generator/gdss/model.py +204 -0
- torch_molecule/generator/gdss/modeling_gdss.py +474 -0
- torch_molecule/generator/gdss/sde.py +334 -0
- torch_molecule/generator/gdss/solver.py +227 -0
- torch_molecule/generator/gdss/toremove/ScoreNetworkA.py +167 -0
- torch_molecule/generator/gdss/toremove/ScoreNetworkX.py +85 -0
- torch_molecule/generator/gdss/utils.py +240 -0
- torch_molecule/generator/graph_dit/__init__.py +3 -0
- torch_molecule/generator/graph_dit/diffusion.py +188 -0
- torch_molecule/generator/graph_dit/modeling_graph_dit.py +581 -0
- torch_molecule/generator/graph_dit/transformer.py +207 -0
- torch_molecule/generator/graph_dit/utils.py +220 -0
- torch_molecule/generator/graph_ga/__init__.py +3 -0
- torch_molecule/generator/graph_ga/crossover.py +182 -0
- torch_molecule/generator/graph_ga/modeling_graph_ga.py +310 -0
- torch_molecule/generator/graph_ga/mutate.py +136 -0
- torch_molecule/generator/graph_ga/oracle.py +95 -0
- torch_molecule/generator/jtvae/__init__.py +3 -0
- torch_molecule/generator/jtvae/jtnn/chemutils.py +450 -0
- torch_molecule/generator/jtvae/jtnn/datautils.py +127 -0
- torch_molecule/generator/jtvae/jtnn/jtmpn.py +140 -0
- torch_molecule/generator/jtvae/jtnn/jtnn_dec.py +350 -0
- torch_molecule/generator/jtvae/jtnn/jtnn_enc.py +131 -0
- torch_molecule/generator/jtvae/jtnn/mol_tree.py +111 -0
- torch_molecule/generator/jtvae/jtnn/mpn.py +129 -0
- torch_molecule/generator/jtvae/jtnn/nnutils.py +66 -0
- torch_molecule/generator/jtvae/jtnn/vocab.py +31 -0
- torch_molecule/generator/jtvae/jtnn_vae.py +246 -0
- torch_molecule/generator/jtvae/modeling_jtvae.py +247 -0
- torch_molecule/generator/lstm/__init__.py +3 -0
- torch_molecule/generator/lstm/action_sampler.py +91 -0
- torch_molecule/generator/lstm/lstm.py +59 -0
- torch_molecule/generator/lstm/modeling_lstm.py +263 -0
- torch_molecule/generator/lstm/smiles_char_dict.py +123 -0
- torch_molecule/generator/lstm/utils.py +62 -0
- torch_molecule/generator/molgpt/__init__.py +3 -0
- torch_molecule/generator/molgpt/dataset.py +87 -0
- torch_molecule/generator/molgpt/gpt.py +251 -0
- torch_molecule/generator/molgpt/modeling_molgpt.py +427 -0
- torch_molecule/generator/pretrained/modeling_pretrained.py +1 -0
- torch_molecule/nn/__init__.py +16 -0
- torch_molecule/nn/attention.py +63 -0
- torch_molecule/nn/embedder.py +183 -0
- torch_molecule/nn/gnn.py +631 -0
- torch_molecule/nn/mlp.py +34 -0
- torch_molecule/predictor/bfgnn/__init__.py +3 -0
- torch_molecule/predictor/bfgnn/model.py +116 -0
- torch_molecule/predictor/bfgnn/modeling_bfgnn.py +81 -0
- torch_molecule/predictor/dir/__init__.py +3 -0
- torch_molecule/predictor/dir/model.py +235 -0
- torch_molecule/predictor/dir/modeling_dir.py +161 -0
- torch_molecule/predictor/dir/utils.py +40 -0
- torch_molecule/predictor/gnn/__init__.py +3 -0
- torch_molecule/predictor/gnn/model.py +114 -0
- torch_molecule/predictor/gnn/modeling_gnn.py +706 -0
- torch_molecule/predictor/gnn/old_modeling_gnn.py +707 -0
- torch_molecule/predictor/grea/__init__.py +3 -0
- torch_molecule/predictor/grea/model.py +190 -0
- torch_molecule/predictor/grea/modeling_grea.py +107 -0
- torch_molecule/predictor/grea/old1_features.py +229 -0
- torch_molecule/predictor/grea/old1_modeling_grea.py +493 -0
- torch_molecule/predictor/grea/utils.py +97 -0
- torch_molecule/predictor/grin/__init__.py +3 -0
- torch_molecule/predictor/grin/model.py +116 -0
- torch_molecule/predictor/grin/modeling_grin.py +91 -0
- torch_molecule/predictor/irm/__init__.py +3 -0
- torch_molecule/predictor/irm/model.py +127 -0
- torch_molecule/predictor/irm/modeling_irm.py +179 -0
- torch_molecule/predictor/lstm/__init__.py +3 -0
- torch_molecule/predictor/lstm/model.py +145 -0
- torch_molecule/predictor/lstm/modeling_lstm.py +627 -0
- torch_molecule/predictor/lstm/token_from_smiles.py +85 -0
- torch_molecule/predictor/old_modeling_graph.py +650 -0
- torch_molecule/predictor/rpgnn/__init__.py +3 -0
- torch_molecule/predictor/rpgnn/model.py +185 -0
- torch_molecule/predictor/rpgnn/modeling_rpgnn.py +76 -0
- torch_molecule/predictor/sgir/__init__.py +3 -0
- torch_molecule/predictor/sgir/modeling_sgir.py +260 -0
- torch_molecule/predictor/sgir/strategy.py +266 -0
- torch_molecule/predictor/smiles_transformer/__init__.py +3 -0
- torch_molecule/predictor/smiles_transformer/model.py +424 -0
- torch_molecule/predictor/smiles_transformer/modeling_transformer.py +252 -0
- torch_molecule/predictor/ssr/__init__.py +3 -0
- torch_molecule/predictor/ssr/model.py +283 -0
- torch_molecule/predictor/ssr/modeling_ssr.py +267 -0
- torch_molecule/predictor/ssr/utils.py +185 -0
- torch_molecule/utils/__init__.py +31 -0
- torch_molecule/utils/checker.py +158 -0
- torch_molecule/utils/checkpoint.py +294 -0
- torch_molecule/utils/format.py +140 -0
- torch_molecule/utils/generic/metrics.py +530 -0
- torch_molecule/utils/generic/pseudo_tasks.py +11 -0
- torch_molecule/utils/generic/weights.py +30 -0
- torch_molecule/utils/graph/features.py +178 -0
- torch_molecule/utils/graph/graph_from_smiles.py +134 -0
- torch_molecule/utils/graph/graph_to_smiles.py +312 -0
- torch_molecule/utils/hf.py +232 -0
- torch_molecule/utils/search.py +64 -0
- torch_molecule-0.1.0.dev2.dist-info/METADATA +243 -0
- torch_molecule-0.1.0.dev2.dist-info/RECORD +148 -0
- torch_molecule-0.1.0.dev2.dist-info/WHEEL +5 -0
- torch_molecule-0.1.0.dev2.dist-info/licenses/LICENSE +21 -0
- torch_molecule-0.1.0.dev2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
from torch_geometric.nn import global_add_pool, global_mean_pool, global_max_pool
|
|
4
|
+
|
|
5
|
+
from ...nn import GNN_node, GNN_node_Virtualnode, MLP
|
|
6
|
+
from ...utils import init_weights
|
|
7
|
+
|
|
8
|
+
class GNN(nn.Module):
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
num_task,
|
|
12
|
+
num_layer,
|
|
13
|
+
hidden_size=300,
|
|
14
|
+
gnn_type="gin-virtual",
|
|
15
|
+
drop_ratio=0.5,
|
|
16
|
+
norm_layer="batch_norm",
|
|
17
|
+
graph_pooling="max",
|
|
18
|
+
augmented_feature=['maccs', 'morgan'],
|
|
19
|
+
algorithm_aligned = None
|
|
20
|
+
):
|
|
21
|
+
super(GNN, self).__init__()
|
|
22
|
+
gnn_name = gnn_type.split("-")[0]
|
|
23
|
+
self.num_task = num_task
|
|
24
|
+
self.hidden_size = hidden_size
|
|
25
|
+
|
|
26
|
+
if "virtual" in gnn_type:
|
|
27
|
+
self.graph_encoder = GNN_node_Virtualnode(
|
|
28
|
+
num_layer,
|
|
29
|
+
hidden_size,
|
|
30
|
+
JK="last",
|
|
31
|
+
drop_ratio=drop_ratio,
|
|
32
|
+
residual=True,
|
|
33
|
+
gnn_name=gnn_name,
|
|
34
|
+
norm_layer=norm_layer,
|
|
35
|
+
algorithm_aligned = algorithm_aligned
|
|
36
|
+
)
|
|
37
|
+
else:
|
|
38
|
+
self.graph_encoder = GNN_node(
|
|
39
|
+
num_layer,
|
|
40
|
+
hidden_size,
|
|
41
|
+
JK="last",
|
|
42
|
+
drop_ratio=drop_ratio,
|
|
43
|
+
residual=True,
|
|
44
|
+
gnn_name=gnn_name,
|
|
45
|
+
norm_layer=norm_layer,
|
|
46
|
+
algorithm_aligned = algorithm_aligned
|
|
47
|
+
)
|
|
48
|
+
if graph_pooling == "sum":
|
|
49
|
+
self.pool = global_add_pool
|
|
50
|
+
elif graph_pooling == "mean":
|
|
51
|
+
self.pool = global_mean_pool
|
|
52
|
+
elif graph_pooling == "max":
|
|
53
|
+
self.pool = global_max_pool
|
|
54
|
+
else:
|
|
55
|
+
raise ValueError(f"Invalid graph pooling type {graph_pooling}.")
|
|
56
|
+
|
|
57
|
+
graph_dim = hidden_size
|
|
58
|
+
self.augmented_feature = augmented_feature
|
|
59
|
+
if augmented_feature:
|
|
60
|
+
if "morgan" in augmented_feature:
|
|
61
|
+
graph_dim += 1024
|
|
62
|
+
if "maccs" in augmented_feature:
|
|
63
|
+
graph_dim += 167
|
|
64
|
+
self.predictor = MLP(graph_dim, hidden_features=2 * hidden_size, out_features=num_task)
|
|
65
|
+
|
|
66
|
+
def initialize_parameters(self, seed=None):
|
|
67
|
+
"""
|
|
68
|
+
Randomly initialize all model parameters using the init_weights function.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
seed (int, optional): Random seed for reproducibility. Defaults to None.
|
|
72
|
+
"""
|
|
73
|
+
if seed is not None:
|
|
74
|
+
torch.manual_seed(seed)
|
|
75
|
+
|
|
76
|
+
# Initialize the main components
|
|
77
|
+
init_weights(self.graph_encoder)
|
|
78
|
+
init_weights(self.predictor)
|
|
79
|
+
|
|
80
|
+
# Reset all parameters using PyTorch Geometric's reset function
|
|
81
|
+
def reset_parameters(module):
|
|
82
|
+
if hasattr(module, 'reset_parameters'):
|
|
83
|
+
module.reset_parameters()
|
|
84
|
+
elif hasattr(module, 'weight') and hasattr(module.weight, 'data'):
|
|
85
|
+
init_weights(module)
|
|
86
|
+
|
|
87
|
+
self.apply(reset_parameters)
|
|
88
|
+
|
|
89
|
+
def _augmented_graph_features(self, batched_data, h_rep):
|
|
90
|
+
if self.augmented_feature:
|
|
91
|
+
if 'morgan' in self.augmented_feature:
|
|
92
|
+
morgan = batched_data.morgan.type_as(h_rep)
|
|
93
|
+
h_rep = torch.cat((h_rep, morgan), dim=1)
|
|
94
|
+
if 'maccs' in self.augmented_feature:
|
|
95
|
+
maccs = batched_data.maccs.type_as(h_rep)
|
|
96
|
+
h_rep = torch.cat((h_rep, maccs), dim=1)
|
|
97
|
+
return h_rep
|
|
98
|
+
|
|
99
|
+
def compute_loss(self, batched_data, criterion):
|
|
100
|
+
h_node, _ = self.graph_encoder(batched_data)
|
|
101
|
+
h_rep = self.pool(h_node, batched_data.batch)
|
|
102
|
+
h_rep = self._augmented_graph_features(batched_data, h_rep)
|
|
103
|
+
prediction = self.predictor(h_rep)
|
|
104
|
+
target = batched_data.y.to(torch.float32)
|
|
105
|
+
is_labeled = batched_data.y == batched_data.y
|
|
106
|
+
loss = criterion(prediction.to(torch.float32)[is_labeled], target[is_labeled]).mean()
|
|
107
|
+
return loss
|
|
108
|
+
|
|
109
|
+
def forward(self, batched_data):
|
|
110
|
+
h_node, _ = self.graph_encoder(batched_data)
|
|
111
|
+
h_rep = self.pool(h_node, batched_data.batch)
|
|
112
|
+
h_rep = self._augmented_graph_features(batched_data, h_rep)
|
|
113
|
+
prediction = self.predictor(h_rep)
|
|
114
|
+
return {"prediction": prediction}
|