|
16 | 16 | # # _De novo_ Reinforcement Learning with the Reinvent prior |
17 | 17 | # |
18 | 18 | # This is a short demo to |
19 | | -# - Set up a reinforcment learning run |
20 | | -# - Carry out a reinforcment learning run |
| 19 | +# - Set up a reinforcement learning run |
| 20 | +# - Carry out a reinforcement learning run |
21 | 21 | # - Visualize the results with TensorBoard |
22 | 22 | # - Extract the raw data from TensorBoard and how to work with it |
23 | 23 |
|
|
37 | 37 |
|
38 | 38 | # ## Set up the REINVENT run |
39 | 39 | # |
40 | | -# A work directory is defined and created anew (all previous data deleted if the directory already exsists). The prior file is taken from the REINVENT repository and the agent is initially the same as the prior. A TOML configuration is written out to file in the work directory. |
| 40 | +# A work directory is defined and created anew (all previous data deleted if the directory already exists). The prior file is taken from the REINVENT repository and the agent is initially the same as the prior. A TOML configuration is written out to file in the work directory. |
41 | 41 |
|
42 | 42 | wd = "/tmp/R4_notebooks_output" |
43 | 43 |
|
44 | 44 | # ### Delete existing working directory (!) |
45 | 45 | # |
46 | | -# Skip this step if you have already carried out the RL run but wish to analyis the results further. |
| 46 | +# Skip this step if you have already carried out the RL run but wish to analyse the results further. |
47 | 47 |
|
48 | 48 | shutil.rmtree(wd, ignore_errors=True) |
49 | 49 | os.mkdir(wd) |
|
66 | 66 |
|
67 | 67 | # ### Parameters |
68 | 68 | # |
69 | | -# Here we specify the model files, the prefix for the output CSV summary file and the batch size for sampling and stochastic gradient descent (SGD). The batch size is often given in 2^N but there is in now way required. Typically batch sizes are betwen 50 and 150. Batch size effects on SGD and so also the learning rate. Some experimentation may be required to adjust this but keep in mind that, say, raising the total score as fast as possible is not necessarily the best choice as this may hamper exploration. |
| 69 | +# Here we specify the model files, the prefix for the output CSV summary file and the batch size for sampling and stochastic gradient descent (SGD). The batch size is often given in 2^N but there is in now way required. Typically batch sizes are between 50 and 150. Batch size effects on SGD and so also the learning rate. Some experimentation may be required to adjust this but keep in mind that, say, raising the total score as fast as possible is not necessarily the best choice as this may hamper exploration. |
70 | 70 |
|
71 | 71 | # + |
72 | 72 | prior_filename = os.path.join(reinvent.__path__[0], "..", "priors", "reinvent.prior") |
|
101 | 101 | # |
102 | 102 | # The stage will terminate when a maximum number of 300 steps is reached. Termination could occur earlier when the maximum score of 1.0 is exceeded but this is very unlikely to occur. A checkpoint file is written out which can be used as the agent in a subsequent stage. |
103 | 103 | # |
104 | | -# The scoring function is a weighted product of all the scoring components: QED and number of sterecentres. The latter is used here to avoid stereocentres as they are not support by the Reinvent prior. Zero stereocentres aids in downstream 3D task to avoid having to carry out stereocentre enumeration. Custom alerts is a filter which filters out (scores as zero) all generated compounds which match one of the SMARTS patterns. Number of sterecentres uses a transformation function to ensure the component score is between 0 and 1. |
| 104 | +# The scoring function is a weighted product of all the scoring components: QED and number of sterecentres. The latter is used here to avoid stereocentres as they are not supported by the Reinvent prior. Zero stereocentres aids in downstream 3D task to avoid having to carry out stereocentre enumeration. Custom alerts is a filter which filters out (scores as zero) all generated compounds which match one of the SMARTS patterns. Number of sterecentres uses a transformation function to ensure the component score is between 0 and 1. |
105 | 105 |
|
106 | 106 | stages = """ |
107 | 107 | [[stage]] |
|
185 | 185 | # |
186 | 186 | # TensorBoard needs to be started after REINVENT has finished. Scatter plots of all scoring components are shown (raw and transformed) in the _SCALARS_ tab as well as loss and fractions of valid and duplicate (per batch) SMILES. The _IMAGES_ tab shows the first sampled molecules for each recorded RL step labelled with the total score for the molecule. |
187 | 187 | # |
188 | | -# The loss likehoods (negative log likelihoods, NLL) for the agent is expected to move away from the prior NLL and also have smaller NLLs than the prior. This shows that the agent is increasingly producing molecules different from the prior, closer to the signal from the scoring function as requested. For sample efficiency it is desirable to observed few duplicates and a high number of valid molecules. Note that results are stochastic and will not be different in every run of RL, even with exactly the same configuration. |
| 188 | +# The loss likelihoods (negative log likelihoods, NLL) for the agent is expected to move away from the prior NLL and also have smaller NLLs than the prior. This shows that the agent is increasingly producing molecules different from the prior, closer to the signal from the scoring function as requested. For sample efficiency it is desirable to observed few duplicates and a high number of valid molecules. Note that results are stochastic and will not be different in every run of RL, even with exactly the same configuration. |
189 | 189 |
|
190 | 190 | # %tensorboard --bind_all --logdir $wd/tb_stage1_0 |
191 | 191 |
|
192 | 192 | # ## Extract data from TensorBoard |
193 | 193 | # |
194 | | -# TensorBoard data can be directy extracted as shown in this section. |
| 194 | +# TensorBoard data can be directly extracted as shown in this section. |
195 | 195 |
|
196 | 196 | # ### Load the TB data |
197 | 197 |
|
|
222 | 222 |
|
223 | 223 | # ### Sample efficiency |
224 | 224 | # |
225 | | -# Count the number of total invalid and duplcate SMILES and compare to the total number of generated SMILES. |
| 225 | +# Count the number of total invalid and duplicate SMILES and compare to the total number of generated SMILES. |
226 | 226 |
|
227 | 227 | # + |
228 | 228 | total_smilies = len(df) |
|
239 | 239 | print( |
240 | 240 | f"Total number of SMILES generated: {total_smilies}\n" |
241 | 241 | f"Total number of invalid SMILES generated: {total_invalid_smilies}\n" |
242 | | - f"Total number of batcg duplicate SMILES generated: {total_batch_duplicate_smilies}\n" |
| 242 | + f"Total number of batch duplicate SMILES generated: {total_batch_duplicate_smilies}\n" |
243 | 243 | f"Total number of duplicate SMILES generated: {total_duplicate_smilies}" |
244 | 244 | ) |
245 | 245 | # - |
|
0 commit comments