ljw20180420 commited on
Commit
e8e13ee
·
verified ·
1 Parent(s): 09948e1

Upload folder using huggingface_hub

Browse files
unet/README.md ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ tags:
4
+ - generated_from_trainer
5
+ datasets:
6
+ - crispr_data
7
+ model-index:
8
+ - name: SX_spcas9_CRISPR_diffuser
9
+ results: []
10
+ ---
11
+
12
+ <!-- This model card has been generated automatically according to the information the Trainer had access to. You
13
+ should probably proofread and complete it, then remove this comment. -->
14
+
15
+ # SX_spcas9_CRISPR_diffuser
16
+
17
+ This model is a fine-tuned version of [](https://huggingface.co/) on the crispr_data dataset.
18
+ It achieves the following results on the evaluation set:
19
+ - Loss: 1.3662
20
+ - Random Idx: 747
21
+ - X1t: 106
22
+ - X2t: 103
23
+ - T: 9.4824
24
+
25
+ ## Model description
26
+
27
+ More information needed
28
+
29
+ ## Intended uses & limitations
30
+
31
+ More information needed
32
+
33
+ ## Training and evaluation data
34
+
35
+ More information needed
36
+
37
+ ## Training procedure
38
+
39
+ ### Training hyperparameters
40
+
41
+ The following hyperparameters were used during training:
42
+ - learning_rate: 0.001
43
+ - train_batch_size: 100
44
+ - eval_batch_size: 100
45
+ - seed: 63036
46
+ - optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
47
+ - lr_scheduler_type: linear
48
+ - lr_scheduler_warmup_ratio: 0.05
49
+ - num_epochs: 30.0
50
+
51
+ ### Training results
52
+
53
+ | Training Loss | Epoch | Step | Validation Loss | Random Idx | X1t | X2t | T |
54
+ |:-------------:|:-----:|:----:|:---------------:|:----------:|:---:|:---:|:-------:|
55
+ | 4.6246 | 1.0 | 322 | 4.2610 | 1344 | 93 | 33 | 0.1660 |
56
+ | 3.9751 | 2.0 | 644 | 4.1661 | 382 | 16 | 127 | 9.9586 |
57
+ | 3.7797 | 3.0 | 966 | 3.9104 | 675 | 91 | 30 | 16.9665 |
58
+ | 2.699 | 4.0 | 1288 | 2.1533 | 936 | 92 | 7 | 10.3704 |
59
+ | 2.1013 | 5.0 | 1610 | 2.0350 | 1351 | 114 | 3 | 5.2067 |
60
+ | 1.966 | 6.0 | 1932 | 1.9681 | 1241 | 90 | 59 | 10.7392 |
61
+ | 1.7409 | 7.0 | 2254 | 1.5903 | 1319 | 110 | 92 | 8.0265 |
62
+ | 1.551 | 8.0 | 2576 | 1.4818 | 1662 | 117 | 40 | 8.8758 |
63
+ | 1.5029 | 9.0 | 2898 | 1.4946 | 1114 | 86 | 29 | 9.4191 |
64
+ | 1.4752 | 10.0 | 3220 | 1.4272 | 815 | 14 | 35 | 3.0533 |
65
+ | 1.4458 | 11.0 | 3542 | 1.4510 | 1637 | 4 | 125 | 19.4909 |
66
+ | 1.4349 | 12.0 | 3864 | 1.4302 | 598 | 48 | 55 | 15.5983 |
67
+ | 1.4437 | 13.0 | 4186 | 1.4039 | 894 | 112 | 29 | 1.6510 |
68
+ | 1.4471 | 14.0 | 4508 | 1.4509 | 968 | 100 | 27 | 1.4723 |
69
+ | 1.4249 | 15.0 | 4830 | 1.4069 | 403 | 109 | 22 | 4.4688 |
70
+ | 1.4231 | 16.0 | 5152 | 1.4061 | 67 | 100 | 27 | 0.0717 |
71
+ | 1.4186 | 17.0 | 5474 | 1.4226 | 1145 | 91 | 40 | 1.5211 |
72
+ | 1.421 | 18.0 | 5796 | 1.4216 | 1127 | 44 | 45 | 8.0851 |
73
+ | 1.4065 | 19.0 | 6118 | 1.3847 | 617 | 66 | 17 | 9.6751 |
74
+ | 1.4055 | 20.0 | 6440 | 1.3967 | 1473 | 90 | 29 | 1.2779 |
75
+ | 1.414 | 21.0 | 6762 | 1.4085 | 372 | 26 | 46 | 6.6225 |
76
+ | 1.4056 | 22.0 | 7084 | 1.3779 | 1149 | 69 | 28 | 0.2331 |
77
+ | 1.4068 | 23.0 | 7406 | 1.4006 | 1317 | 91 | 54 | 2.8194 |
78
+ | 1.4033 | 24.0 | 7728 | 1.3939 | 229 | 122 | 66 | 8.7889 |
79
+ | 1.4044 | 25.0 | 8050 | 1.3742 | 1097 | 35 | 45 | 12.9269 |
80
+ | 1.3917 | 26.0 | 8372 | 1.3801 | 90 | 124 | 38 | 4.4619 |
81
+ | 1.3936 | 27.0 | 8694 | 1.3972 | 1065 | 81 | 25 | 11.5671 |
82
+ | 1.3916 | 28.0 | 9016 | 1.4157 | 1756 | 10 | 21 | 11.6326 |
83
+ | 1.3897 | 29.0 | 9338 | 1.3791 | 138 | 127 | 20 | 15.4719 |
84
+ | 1.3874 | 30.0 | 9660 | 1.3662 | 747 | 106 | 103 | 9.4824 |
85
+
86
+
87
+ ### Framework versions
88
+
89
+ - Transformers 4.44.2
90
+ - Pytorch 2.4.0+cu124
91
+ - Datasets 2.21.0
92
+ - Tokenizers 0.19.1
unet/config.json CHANGED
@@ -4,7 +4,6 @@
4
  0.0,
5
  1.0
6
  ],
7
- "_name_or_path": "/home/ljw/sdc1/CRISPR_results/CRISPR_diffuser/SX_spcas9_CRISPR_diffuser",
8
  "architectures": [
9
  "CRISPRDiffuserModel"
10
  ],
 
4
  0.0,
5
  1.0
6
  ],
 
7
  "architectures": [
8
  "CRISPRDiffuserModel"
9
  ],
unet/model.py ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers.models.embeddings import get_timestep_embedding
2
+ from transformers import PretrainedConfig, PreTrainedModel
3
+ import torch.nn as nn
4
+ import torch
5
+ import torch.nn.functional as F
6
+ from typing import Dict, Union, Any, List
7
+
8
+ class CRISPRDiffuserConfig(PretrainedConfig):
9
+ model_type = "CRISPR_diffuser"
10
+ label_names = ["observation"]
11
+ main_input_name = "x1t_x2t_t"
12
+
13
+ def __init__(
14
+ self,
15
+ count_normalize: float = 1000.,
16
+ channels: List = [11, 32, 64, 96, 64, 32, 1],
17
+ MCMC_corrector_factor: List = [1., 0., 0.001],
18
+ ref1len: int = 127,
19
+ ref2len: int = 127,
20
+ seed: int = 63036, # random seed for intialization
21
+ **kwargs,
22
+ ):
23
+ self.count_normalize = count_normalize
24
+ self.channels = channels
25
+ self.MCMC_corrector_factor = MCMC_corrector_factor
26
+ self.ref1len = ref1len
27
+ self.ref2len = ref2len
28
+ self.seed = seed
29
+ super().__init__(**kwargs)
30
+
31
+ class CRISPRDiffuserModel(PreTrainedModel):
32
+ config_class = CRISPRDiffuserConfig
33
+
34
+ def __init__(self, config):
35
+ super().__init__(config)
36
+ # In more recent versions of PyTorch, you no longer need to explicitly register_parameter, it's enough to set a member of your nn.Module with nn.Parameter to "notify" pytorch that this variable should be treated as a trainable parameter (https://stackoverflow.com/questions/59234238/how-to-add-parameters-in-module-class-in-pytorch-custom-model).
37
+ self.main_input_name = config.main_input_name
38
+ # record loss inside model to stop training in callbacks
39
+ self.loss = None
40
+ self.generator = torch.Generator().manual_seed(config.seed)
41
+ self.register_buffer("stationary_sampler1_probs", F.normalize(torch.ones(config.ref1len + 1), p=1.0, dim=0))
42
+ self.register_buffer("stationary_sampler2_probs", F.normalize(torch.ones(config.ref2len + 1), p=1.0, dim=0))
43
+ # time
44
+ self.time_emb = nn.Sequential(
45
+ nn.Linear(in_features=self.config.channels[1], out_features=4 * self.config.channels[1]),
46
+ nn.SiLU(),
47
+ nn.Linear(in_features=4 * self.config.channels[1], out_features=4 * self.config.channels[1])
48
+ )
49
+ # down blocks
50
+ self.down_time_embs = nn.ModuleList([])
51
+ self.down_first_convs = nn.ModuleList([])
52
+ self.down_second_convs = nn.ModuleList([])
53
+ self.down_samples = nn.ModuleList([])
54
+ for i in range((len(self.config.channels) - 1) // 2 - 1):
55
+ self.down_first_convs.append(nn.Sequential(
56
+ nn.Conv2d(in_channels=self.config.channels[i], out_channels=self.config.channels[i + 1], kernel_size=3, padding=1),
57
+ nn.BatchNorm2d(num_features=self.config.channels[i + 1]),
58
+ nn.SiLU(inplace=True)
59
+ ))
60
+ self.down_second_convs.append(nn.Sequential(
61
+ nn.Conv2d(in_channels=self.config.channels[i + 1], out_channels=self.config.channels[i + 1], kernel_size=3, padding=1),
62
+ nn.BatchNorm2d(num_features=self.config.channels[i + 1]),
63
+ nn.SiLU(inplace=True),
64
+ ))
65
+ self.down_time_embs.append(nn.Sequential(
66
+ nn.Linear(in_features=4 * self.config.channels[1], out_features=self.config.channels[i + 1]),
67
+ nn.SiLU()
68
+ ))
69
+ self.down_samples.append(
70
+ nn.MaxPool2d(kernel_size=2) # nn.AvgPool2d(kernel_size=2), nn.Conv2d(channels[i + 1], channels[i + 1], kernel_size=2, stride=2)
71
+ )
72
+ # mid block
73
+ i = (len(self.config.channels) - 1) // 2 - 1
74
+ self.mid_first_conv = nn.Sequential(
75
+ nn.Conv2d(in_channels=self.config.channels[i], out_channels=self.config.channels[i + 1], kernel_size=3, padding=1),
76
+ nn.BatchNorm2d(num_features=self.config.channels[i + 1]),
77
+ nn.SiLU(inplace=True)
78
+ )
79
+ self.mid_second_conv = nn.Sequential(
80
+ nn.Conv2d(in_channels=self.config.channels[i + 1], out_channels=self.config.channels[i + 1], kernel_size=3, padding=1),
81
+ nn.BatchNorm2d(num_features=self.config.channels[i + 1]),
82
+ nn.SiLU(inplace=True),
83
+ )
84
+ self.mid_time_emb = nn.Sequential(
85
+ nn.Linear(in_features=4 * self.config.channels[1], out_features=self.config.channels[i + 1]),
86
+ nn.SiLU()
87
+ )
88
+ # up blocks
89
+ self.up_samples = nn.ModuleList([])
90
+ self.up_time_embs = nn.ModuleList([])
91
+ self.up_first_convs = nn.ModuleList([])
92
+ self.up_second_convs = nn.ModuleList([])
93
+ for i in range((len(self.config.channels) - 1) // 2, len(self.config.channels) - 2):
94
+ self.up_samples.append(
95
+ nn.ConvTranspose2d(in_channels=self.config.channels[i], out_channels=self.config.channels[i + 1], kernel_size=2, stride=2)
96
+ )
97
+ self.up_time_embs.append(nn.Sequential(
98
+ nn.Linear(in_features=4 * self.config.channels[1], out_features=self.config.channels[i + 1]),
99
+ nn.SiLU()
100
+ ))
101
+ self.up_first_convs.append(nn.Sequential(
102
+ nn.Conv2d(in_channels=self.config.channels[i + 1]+self.config.channels[len(self.config.channels) - i - 2], out_channels=self.config.channels[i + 1], kernel_size=3, padding=1),
103
+ nn.BatchNorm2d(num_features=self.config.channels[i + 1]),
104
+ nn.SiLU(inplace=True)
105
+ ))
106
+ self.up_second_convs.append(nn.Sequential(
107
+ nn.Conv2d(in_channels=self.config.channels[i + 1], out_channels=self.config.channels[i + 1], kernel_size=3, padding=1),
108
+ nn.BatchNorm2d(num_features=self.config.channels[i + 1]),
109
+ nn.SiLU(inplace=True)
110
+ ))
111
+ self.out_cov = nn.Conv2d(in_channels=self.config.channels[-2], out_channels=self.config.channels[-1], kernel_size=1)
112
+ self.initialize_weights()
113
+
114
+ def initialize_weights(self):
115
+ for m in self.modules():
116
+ if isinstance(m, nn.Linear):
117
+ nn.init.normal_(m.weight, mean=0, std=1, generator=self.generator)
118
+ if m.bias is not None:
119
+ nn.init.constant_(m.bias, 0)
120
+ if isinstance(m, nn.Conv2d):
121
+ nn.init.normal_(m.weight, mean=0, std=1, generator=self.generator)
122
+ if m.bias is not None:
123
+ nn.init.constant_(m.bias, 0)
124
+ if isinstance(m, nn.ConvTranspose2d):
125
+ nn.init.normal_(m.weight, mean=0, std=1, generator=self.generator)
126
+ if m.bias is not None:
127
+ nn.init.constant_(m.bias, 0)
128
+
129
+ def forward(self, x1t_x2t_t: dict, condition: torch.Tensor, observation: torch.Tensor | None = None):
130
+ x1t, x2t, t = x1t_x2t_t.values()
131
+ batch_size = condition.shape[0]
132
+ x = torch.cat((
133
+ (
134
+ F.one_hot(x1t, num_classes=len(self.stationary_sampler1_probs)).view(batch_size, 1, -1) *
135
+ F.one_hot(x2t, num_classes=len(self.stationary_sampler2_probs)).view(batch_size, -1, 1)
136
+ )[:, None, :, :],
137
+ condition
138
+ ), dim = 1)
139
+ t_emb = get_timestep_embedding(t, embedding_dim=self.config.channels[1], flip_sin_to_cos=True, downscale_freq_shift=0)
140
+ t_emb = self.time_emb(t_emb)
141
+ down_xs = []
142
+ for i in range(len(self.down_first_convs)):
143
+ down_xs.append(
144
+ self.down_second_convs[i](self.down_first_convs[i](x) + self.down_time_embs[i](t_emb)[:, :, None, None])
145
+ )
146
+ x = self.down_samples[i](down_xs[-1])
147
+ x = self.mid_second_conv(self.mid_first_conv(x) + self.mid_time_emb(t_emb)[:, :, None, None])
148
+ for i in range(len(self.up_first_convs)):
149
+ x = self.up_second_convs[i](self.up_first_convs[i](torch.cat((down_xs.pop(), self.up_samples[i](x)), dim=1)) + self.up_time_embs[i](t_emb)[:, :, None, None])
150
+ p_theta_0_logit = self.out_cov(x)
151
+ if observation is not None:
152
+ self.loss = self.continuous_time_loss_function(x1t, x2t, t, p_theta_0_logit, observation)
153
+ return {
154
+ "p_theta_0_logit": p_theta_0_logit,
155
+ "loss": self.loss
156
+ }
157
+ return {
158
+ "p_theta_0_logit": p_theta_0_logit
159
+ }
160
+
161
+ def continuous_time_loss_function(self, x1t: torch.Tensor, x2t: torch.Tensor, t: torch.Tensor, p_theta_0_logit: torch.Tensor, observation: torch.Tensor):
162
+ def get_q_rkm_d(stationary_sampler_probs, xt):
163
+ xt_one_hot = F.one_hot(xt, len(stationary_sampler_probs))
164
+ q_rkm_d = alpha_t[:, None] * xt_one_hot + ((1 - alpha_t) * stationary_sampler_probs[xt])[:, None]
165
+ return q_rkm_d
166
+
167
+ def get_g_theta_d(stationary_sampler_probs, xt, dim, p_theta_0):
168
+ auxilary_term = 1 + (1 / alpha_t - 1) * stationary_sampler_probs[xt]
169
+ xt_one_hot = F.one_hot(xt, len(stationary_sampler_probs))
170
+ p_theta_d_0 = p_theta_0.sum(dim=dim)
171
+ g_theta_d = (
172
+ (1 - p_theta_d_0[torch.arange(p_theta_d_0.shape[0]), xt] / auxilary_term)[:, None] * stationary_sampler_probs +
173
+ (alpha_t / (1 - alpha_t))[:, None] * p_theta_d_0
174
+ ) * (1 - xt_one_hot) / stationary_sampler_probs[xt][:, None] + xt_one_hot
175
+ return g_theta_d
176
+
177
+ alpha_t = torch.e ** (-t)
178
+ batch_size = p_theta_0_logit.shape[0]
179
+ p_theta_0 = F.softmax(
180
+ p_theta_0_logit.view(batch_size, -1),
181
+ dim = 1
182
+ ).view(batch_size, len(self.stationary_sampler2_probs), len(self.stationary_sampler1_probs))
183
+ log_p_theta_0 = F.log_softmax(
184
+ p_theta_0_logit.view(batch_size, -1),
185
+ dim = 1
186
+ ).view(batch_size, len(self.stationary_sampler2_probs), len(self.stationary_sampler1_probs))
187
+
188
+ g_theta_1_t = get_g_theta_d(self.stationary_sampler1_probs, x1t, 1, p_theta_0)
189
+ g_theta_2_t = get_g_theta_d(self.stationary_sampler2_probs, x2t, 2, p_theta_0)
190
+
191
+ q_rkm_1 = get_q_rkm_d(self.stationary_sampler1_probs, x1t)
192
+ q_rkm_2 = get_q_rkm_d(self.stationary_sampler2_probs, x2t)
193
+ q_0_give_t = F.normalize(
194
+ (observation * q_rkm_1[:, None, :] * q_rkm_2[:, :, None]).view(batch_size, -1),
195
+ p=1.0, dim=1
196
+ ).view(batch_size, len(self.stationary_sampler2_probs), len(self.stationary_sampler1_probs))
197
+
198
+ g_1_t = get_g_theta_d(self.stationary_sampler1_probs, x1t, 1, q_0_give_t)
199
+ g_2_t = get_g_theta_d(self.stationary_sampler2_probs, x2t, 2, q_0_give_t)
200
+
201
+ common_negative_ELBO = (
202
+ self.stationary_sampler1_probs[x1t] * g_theta_1_t.sum(dim = 1) +
203
+ self.stationary_sampler2_probs[x2t] * g_theta_2_t.sum(dim = 1)
204
+ )
205
+
206
+ log_g_theta_1_t = g_theta_1_t.log().clamp_min(-1000)
207
+ log_g_theta_2_t = g_theta_2_t.log().clamp_min(-1000)
208
+
209
+ forward_negative_ELBO = common_negative_ELBO + (
210
+ torch.inner(self.stationary_sampler1_probs, log_g_theta_1_t) +
211
+ torch.inner(self.stationary_sampler2_probs, log_g_theta_2_t)
212
+ )
213
+
214
+ reverse_negative_ELBO = common_negative_ELBO - (
215
+ (g_1_t * log_g_theta_1_t).sum(dim=1) +
216
+ (g_2_t * log_g_theta_2_t).sum(dim=1)
217
+ )
218
+
219
+ MCMC_corrector = - (log_p_theta_0.view(batch_size, -1) * q_0_give_t.view(batch_size, -1)).sum(dim=1)
220
+
221
+ return (
222
+ observation.sum(dim=(1, 2)) / self.config.count_normalize * (
223
+ self.config.MCMC_corrector_factor[0] * forward_negative_ELBO +
224
+ self.config.MCMC_corrector_factor[1] * reverse_negative_ELBO +
225
+ self.config.MCMC_corrector_factor[2] * MCMC_corrector
226
+ )
227
+ ).sum()
228
+
229
+ # transformers.modeling_utils.ModuleUtilsMixin.floating_point_ops cannot handle nested input_dict, override it to avoid the error
230
+ def floating_point_ops(self, input_dict: Dict[str, Union[torch.Tensor, Any]]):
231
+ return 0
unet/runs/Nov19_16-57-36_ljw-System-Product-Name/events.out.tfevents.1732006656.ljw-System-Product-Name.777346.0 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a69c9f29bea8c57255009bca1990916c856ab0b2c67386dc38f5368713b90b4b
3
+ size 449703
unet/runs/Nov19_16-57-36_ljw-System-Product-Name/events.out.tfevents.1732006657.ljw-System-Product-Name.777346.1 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:41dfcaaf440cbc8d2d59a38a12e60d5f3ba43cbed6df141a8a87632a0ca71952
3
+ size 25357
unet/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:65a20ee314bd927e377eee77152840b16a2c156fd06d1dd9fdb5cb2e615207dd
3
+ size 5304