Пример #1
0
def train():
    # Load the images with ASE
    images = Trajectory("cu_training.traj")

    # Arguments for fingerprinting the images
    normalized = True

    # Arguments for building the model
    n = 10
    activation = "relu"

    # Arguments for training the potential
    convergence = {"energy": 5e-3}
    epochs = 100
    lr = 1.0e-2
    weight_decay = 0.0
    regularization = 0.0

    calc = Potentials(
        features=Gaussian(cutoff=6.5,
                          normalized=normalized,
                          save_preprocessor="model.scaler"),
        model=NeuralNetwork(hiddenlayers=(n, n), activation=activation),
        label="cu_training",
    )

    optimizer = ("adam", {"lr": lr, "weight_decay": weight_decay})
    calc.train(
        training_set=images,
        epochs=epochs,
        regularization=regularization,
        convergence=convergence,
        optimizer=optimizer,
    )
Пример #2
0
def train():
    # Load the images with ASE
    images = Trajectory("cu_training.traj")

    # Arguments for fingerprinting the images
    normalized = True

    calc = Potentials(
        features=Gaussian(cutoff=6.5,
                          normalized=normalized,
                          save_preprocessor="cu_training.scaler"),
        # model=GaussianProcess(batch_size=batch_size),
        model=GaussianProcess(),
        label="cu_training",
    )

    calc.train(training_set=images)
Пример #3
0
    def checkpoint_save(self,
                        epoch,
                        model,
                        label=None,
                        checkpoint=None,
                        path=""):
        """Checkpoint saver

        A method that saves the checkpoint of a model during training.

        Parameters
        ----------
        epoch : int
            Epoch number.
        model : object
            A DeepLearning object.
        label : str, optional
            String with checkpoint label, by default None.
        checkpoint : int, optional
            Set checkpoints. If set to 100, at each 100 epoch the model will be
            saved. Use -1 to save each epoch. Default is None.
        path : str, optional
            Path to save the checkpoint, by default "".
        """

        if label is None:
            label = f"checkpoint-{epoch}"
        else:
            label = f"{label}-checkpoint-{epoch}"

        if checkpoint is None:
            pass
        elif checkpoint == -1:
            Potentials.save(model=model, label=label, path=path)
        elif epoch % checkpoint == 0:
            Potentials.save(model=model, label=label, path=path)
Пример #4
0
def main():
    """docstring for main"""

    # Load the images with ASE
    images = Trajectory("cu_training.traj")

    calc = Potentials.load(
        model="cu_training.ml4c",
        params="cu_training.params",
        preprocessor="model.scaler",
    )

    for atoms in images:
        energy = calc.get_potential_energy(atoms)
        print("ML4Chem predicted energy = {}".format(energy))
        print("              DFT energy = {}".format(
            atoms.get_potential_energy()))
Пример #5
0
def main():
    """docstring for main"""

    # Load the images with ASE
    images = Trajectory("cu_training.traj")

    calc = Potentials.load(
        model="cu_training.ml4c",
        params="cu_training.params",
        preprocessor="cu_training.scaler",
    )

    # Passage of fingerprint database with reference space
    calc.reference_space = "features.db"

    for atoms in images:
        energy = calc.get_potential_energy(atoms)
        print("ML4Chem predicted energy = {}".format(energy))
        print("              DFT energy = {}".format(
            atoms.get_potential_energy()))
Пример #6
0
def train():
    # Load the images with ASE
    images = Trajectory("training.traj")

    # Arguments for fingerprinting the images
    normalized = True

    # Arguments for building the model
    n = 10
    activation = "relu"

    # Arguments for training the potential
    convergence = {"energy": 5e-3}
    epochs = 100
    lr = 1.0e-2
    weight_decay = 0.0
    regularization = 0.0

    rcr = 5.1
    eta_r = [19.70000]
    rs = [
        8.0000000e01,
        1.0687500e00,
        1.3375000e00,
        1.6062500e00,
        1.8750000e00,
        2.1437500e00,
        2.4125000e00,
        2.6812500e00,
        2.9500000e00,
        3.2187500e00,
        3.4875000e00,
        3.7562500e00,
        4.0250000e00,
        4.2937500e00,
        4.5625000e00,
        4.8312500e00,
    ]

    rca = 3.5
    eta_a = [12.50000]
    rs_a = [
        8.0000000e-01,
        1.1375000e00,
        1.4750000e00,
        1.8125000e00,
        2.1500000e00,
        2.4875000e00,
        2.8250000e00,
        3.1625000e00,
    ]
    zetas = [14.10000]
    thetas = [3.9269908e-01, 1.1780972e00, 1.9634954e00, 2.7488936e00]

    custom = {
        "G2": {"etas": eta_r, "Rs": rs},
        "G4": {"etas": eta_a, "zetas": zetas, "Rs_a": rs_a, "thetas": thetas},
    }

    cutoff = {"radial": rcr, "angular": rca}

    calc = Potentials(
        features=AEV(cutoff=cutoff, normalized=normalized, custom=custom,),
        model=NeuralNetwork(hiddenlayers=(n, n), activation=activation),
        label="cu_training",
    )

    optimizer = ("adam", {"lr": lr, "weight_decay": weight_decay})
    calc.train(
        training_set=images,
        epochs=epochs,
        regularization=regularization,
        convergence=convergence,
        optimizer=optimizer,
    )
Пример #7
0
def hybrid():
    # Load the images with ASE, and prepare data handler
    images = Trajectory("cu_training.traj")
    purpose = "training"

    latent_dimension = 32
    data_handler = Data(images, purpose=purpose)
    data_handler.get_unique_element_symbols(images, purpose=purpose)
    training_set, energy_targets = data_handler.get_data(purpose=purpose)

    # Preprocessor setup
    preprocessor = ("MinMaxScaler", {"feature_range": (-1, 1)})
    """
    Preparing the input
    """
    features = Cartesian(preprocessor=preprocessor,
                         save_preprocessor="cartesian.scaler")
    _inputs = features.calculate(training_set, data=data_handler)
    """
    Building AutoEncoder Model1
    """
    # Arguments for building the model
    hiddenlayers = {
        "encoder": (144, 72, latent_dimension),
        "decoder": (latent_dimension, 72, 144),
    }
    # hiddenlayers = {"encoder": (2, 2, 2), "decoder": (2, 2, 2)}
    activation = "tanh"
    autoencoder = AutoEncoder(hiddenlayers=hiddenlayers, activation=activation)
    autoencoder.prepare_model(3, 3, data=data_handler)
    """
    Building the ml potential model
    """

    # Arguments for building the model
    n = 40
    activation = "tanh"

    nn = NeuralNetwork(hiddenlayers=(n, n), activation=activation)
    nn.prepare_model(latent_dimension, data=data_handler)

    models = [autoencoder, nn]
    losses = [MSELoss, AtomicMSELoss]
    # losses = [EncoderMapLoss, AtomicMSELoss]

    merged = ModelMerger(models)
    # Arguments for training the potential
    convergence = {"rmse": [1.5e-1, 1.0e-1]}
    lr = 1e-4
    weight_decay = 1e-5
    regularization = None

    # Optimizer
    optimizer = ("adam", {
        "lr": lr,
        "weight_decay": weight_decay,
        "amsgrad": True
    })
    lr_scheduler = None

    inputs = [_inputs, autoencoder.get_latent_space]
    targets = [_inputs, energy_targets]
    batch_size = 2

    merged.train(
        inputs=inputs,
        targets=targets,
        data=data_handler,
        regularization=regularization,
        convergence=convergence,
        optimizer=optimizer,
        device="cpu",
        batch_size=batch_size,
        lr_scheduler=lr_scheduler,
        lossfxn=losses,
        independent_loss=True,
    )

    for index, model in enumerate(merged.models):
        label = "{}_{}".format(index, model.name())
        Potentials.save(model, label=label)

    dump_ls = merged.models[0].get_latent_space(inputs[0])
    dump(dump_ls, filename="checkme.latent")
Пример #8
0
def autoencode():
    # Load the images with ASE
    images = Trajectory("cu_training.traj")
    purpose = "training"

    # Arguments for fingerprinting the images
    normalized = True
    """
    Data Structure Preparation
    """
    data_handler = Data(images, purpose=purpose)
    training_set, energy_targets = data_handler.get_data(purpose=purpose)
    """
    Let's create the targets of the model
    """
    features = Gaussian(cutoff=6.5,
                        normalized=normalized,
                        save_preprocessor="cu_training.scaler")

    targets = features.calculate(training_set,
                                 data=data_handler,
                                 purpose=purpose,
                                 svm=False)
    output_dimension = len(list(targets.values())[0][0][1])
    """
    Building AutoEncoder
    """
    # Arguments for building the model
    hiddenlayers = {"encoder": (20, 10, 4), "decoder": (4, 10, 20)}
    activation = "tanh"
    autoencoder = AutoEncoder(hiddenlayers=hiddenlayers, activation=activation)

    data_handler.get_unique_element_symbols(images, purpose=purpose)
    autoencoder.prepare_model(output_dimension,
                              output_dimension,
                              data=data_handler)
    # Arguments for training the potential
    convergence = {"rmse": 5e-2}
    epochs = 2000
    lr = 1e-3
    weight_decay = 0
    regularization = None

    optimizer = ("adam", {
        "lr": lr,
        "weight_decay": weight_decay,
        "amsgrad": True
    })

    inputs = targets
    train(
        inputs,
        targets,
        model=autoencoder,
        data=data_handler,
        optimizer=optimizer,
        regularization=regularization,
        epochs=epochs,
        convergence=convergence,
        lossfxn=None,
        device="cpu",
    )

    latent_space = autoencoder.get_latent_space(targets, svm=True)

    dump(latent_space, filename="cu_training.latent")

    Potentials.save(autoencoder)

    return latent_space, energy_targets, data_handler