Skip to content

Commit

Permalink
General improvements:
Browse files Browse the repository at this point in the history
- Fixed examples by removing asynchronous keyword argument.
- Improved in logging.
- Fixed neuralnetwork.py and rt.py module for cases where batch elements
  are asymmetric.
  • Loading branch information
muammar committed Nov 21, 2019
1 parent 2196266 commit a49c7d6
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/krr_potentials/cu_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ def main():
if __name__ == "__main__":
logger(filename="cu_inference.log")
cluster = LocalCluster(n_workers=8, threads_per_worker=2)
client = Client(cluster, asyncronous=True)
client = Client(cluster)
main()
2 changes: 1 addition & 1 deletion examples/krr_potentials/cu_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ def train():
if __name__ == "__main__":
logger(filename="cu_training.log")
cluster = LocalCluster()
client = Client(cluster, asyncronous=True)
client = Client(cluster)
train()
2 changes: 1 addition & 1 deletion examples/nn_potentials/cu_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ def main():
format="%(filename)s:%(lineno)s %(levelname)s:%(message)s",
)
cluster = LocalCluster(n_workers=8, threads_per_worker=2)
client = Client(cluster, asyncronous=True)
client = Client(cluster)
main()
2 changes: 1 addition & 1 deletion examples/nn_potentials/cu_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ def train():
if __name__ == "__main__":
logger(filename="cu_training.log")
cluster = LocalCluster()
client = Client(cluster, asyncronous=True)
client = Client(cluster)
train()
4 changes: 3 additions & 1 deletion ml4chem/features/autoencoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ def calculate(self, images, purpose="training", data=None, svm=False):
else:
if encoder.name() == "VAE":
purpose = "inference"
latent_space = encoder.get_latent_space(feature_space, svm=svm, purpose=purpose)
latent_space = encoder.get_latent_space(
feature_space, svm=svm, purpose=purpose
)

return latent_space

Expand Down
6 changes: 3 additions & 3 deletions ml4chem/features/cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def calculate(self, images=None, purpose="training", data=None, svm=False):
"""

logger.info(" ")
logger.info("Fingerprinting")
logger.info("==============")
logger.info("Featurization")
logger.info("=============")

if os.path.isfile(self.filename) and self.overwrite is False:
logger.warning("Loading features from {}.".format(self.filename))
Expand Down Expand Up @@ -247,7 +247,7 @@ def calculate(self, images=None, purpose="training", data=None, svm=False):
h, m, s = convert_elapsed_time(fp_time)

logger.info(
"Fingerprinting finished in {} hours {} minutes {:.2f} "
"Featurization finished in {} hours {} minutes {:.2f} "
"seconds.\n".format(h, m, s)
)

Expand Down
8 changes: 4 additions & 4 deletions ml4chem/features/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def calculate(self, images=None, purpose="training", data=None, svm=False):
"""

logger.info(" ")
logger.info("Fingerprinting")
logger.info("==============")
logger.info("Featurization")
logger.info("=============")

# FIXME the block below should become a function.
if os.path.isfile(self.filename) and self.overwrite is False:
Expand Down Expand Up @@ -392,7 +392,7 @@ def calculate(self, images=None, purpose="training", data=None, svm=False):
h, m, s = convert_elapsed_time(fp_time)

logger.info(
"Fingerprinting finished in {} hours {} minutes {:.2f}"
"Featurization finished in {} hours {} minutes {:.2f}"
" seconds.".format(h, m, s)
)

Expand Down Expand Up @@ -443,7 +443,7 @@ def calculate(self, images=None, purpose="training", data=None, svm=False):
h, m, s = convert_elapsed_time(fp_time)

logger.info(
"Fingerprinting finished in {} hours {} minutes {:.2f}"
"Featurization finished in {} hours {} minutes {:.2f}"
" seconds.".format(h, m, s)
)

Expand Down
14 changes: 9 additions & 5 deletions ml4chem/models/autoencoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def prepare_model(

values = [h, mu, logvar]
encoder = torch.nn.ModuleDict(list(map(list, zip(keys, values))))
else:
else:

encoder = torch.nn.Sequential(*encoder)

Expand Down Expand Up @@ -599,7 +599,7 @@ def reparameterize(self, mu, logvar, purpose=None):
A sample from the distribution.
"""
if purpose is None:
raise("You need to provide a purpose")
raise ("You need to provide a purpose")

elif purpose == "training":
std = torch.exp(0.5 * logvar)
Expand Down Expand Up @@ -700,7 +700,7 @@ def get_latent_space(self, X, svm=False, purpose=None):
"""

if purpose is None:
raise("You need to provide a purpose")
raise ("You need to provide a purpose")

# FIXME parallelize me
if purpose == "preprocessing":
Expand All @@ -716,7 +716,9 @@ def get_latent_space(self, X, svm=False, purpose=None):
mu_latent, logvar_latent = self.encode(x)
else:
mu_latent, logvar_latent = self.encode(x, symbol=symbol)
latent_vector = self.reparameterize(mu_latent, logvar_latent, purpose="latent")
latent_vector = self.reparameterize(
mu_latent, logvar_latent, purpose="latent"
)
_symbols.append(symbol)

if svm:
Expand Down Expand Up @@ -745,7 +747,9 @@ def get_latent_space(self, X, svm=False, purpose=None):
mu_latent, logvar_latent = self.encode(x)
else:
mu_latent, logvar_latent = self.encode(x, symbol=symbol)
latent_vector = self.reparameterize(mu_latent, logvar_latent, purpose=purpose)
latent_vector = self.reparameterize(
mu_latent, logvar_latent, purpose=purpose
)

if svm:
_latent_vector = latent_vector.detach().numpy()
Expand Down
11 changes: 6 additions & 5 deletions ml4chem/models/neuralnetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,12 @@ def __init__(
logging.info("Batch size: {} elements per batch.".format(batch_size))
logger.info(" ")

atoms_per_image = torch.tensor(
atoms_per_image, requires_grad=False, dtype=torch.float
)
atoms_per_image = [
torch.tensor(n_atoms, requires_grad=False, dtype=torch.float)
for n_atoms in atoms_per_image
]

targets = torch.tensor(targets, requires_grad=False)
targets = [torch.tensor(t, requires_grad=False) for t in targets]

if device == "cuda":
logger.info("Moving data to CUDA...")
Expand Down Expand Up @@ -381,7 +382,7 @@ def trainer(self):
client = dask.distributed.get_client()

rmse = client.submit(compute_rmse, *(outputs_, self.targets))
atoms_per_image = self.atoms_per_image.view(1, -1)
atoms_per_image = torch.cat(self.atoms_per_image)
rmse_atom = client.submit(
compute_rmse, *(outputs_, self.targets, atoms_per_image)
)
Expand Down

0 comments on commit a49c7d6

Please sign in to comment.