Source code for ml4chem.backends.operations

import numpy as np

[docs]class BackendOperations(object): """A class for centralizing backend operations This class will be growing systematically. This is probably not the best solution but can be worked out later. Parameters ---------- backend : object A backend object: numpy, tensorflow, or pytorch. """ def __init__(self, backend): self.backend = backend = self.backend.__name__
[docs] def dot(self, a, b): """Dot product""" if == "torch": if isinstance(a, np.ndarray): a = self.backend.Tensor(a).float() if isinstance(b, np.ndarray): b = self.backend.Tensor(b).float() return self.backend.matmul(a, b) else: return, b)
[docs] def logspace(self, a, b, num): """Logspace""" if == "torch": return self.backend.logspace(start=float(a), end=float(b), steps=num) else: return self.backend.logspace(a, b, num)
[docs] def log10(self, a): """Log base 10""" if == "torch": a = self.backend.Tensor([a]) return self.backend.log10(a)
[docs] def norm(self, a): """Norm between two vectors""" if == "torch": return self.backend.norm(a).float() else: return self.backend.linalg.norm(a)
[docs] def exp(self, a): """Exponential of a number""" return self.backend.exp(a)
[docs] def from_numpy(self, a): """Convert from numpy to right data type""" a = np.array(a) # This is the safest way return self.backend.from_numpy(a).float()
[docs] def to_numpy(self, a): """Convert from numpy to right data type""" if == "torch": return a.numpy()
[docs] def divide(self, a, b): """Divide two vectors/tensors""" if == "torch": return self.backend.div(a, b)
[docs] def sum(self, a): """Sum a list of values""" if == "torch": return self.backend.sum(a)