sgdml/train.py
def create_task(
        self,
        train_dataset,
        n_train,
        valid_dataset,
        n_valid,
        sig,
        lam=1e-15,
        use_sym=True,
        use_E=True,
        use_E_cstr=False,
        use_cprsn=False,
        model0=None,  # TODO: document me
    ):
        """
        Create a data structure of custom type `task`.

        These data structures serve as recipes for model creation,
        summarizing the configuration of one particular training run.
        Training and test points are sampled from the provided dataset,
        without replacement. If the same dataset if given for training
        and testing, the subsets are drawn without overlap.

        Each task also contains a choice for the hyper-parameters of the
        training process and the MD5 fingerprints of the used datasets.

        Parameters
        ----------
            train_dataset : :obj:`dict`
                Data structure of custom type :obj:`dataset` containing
                train dataset.
            n_train : int
                Number of training points to sample.
            valid_dataset : :obj:`dict`
                Data structure of custom type :obj:`dataset` containing
                validation dataset.
            n_valid : int
                Number of validation points to sample.
            sig : int
                Hyper-parameter (kernel length scale).
            lam : float, optional
                Hyper-parameter lambda (regularization strength).
            use_sym : bool, optional
                True: include symmetries (sGDML), False: GDML.
            use_E : bool, optional
                True: reconstruct force field with corresponding potential energy surface,
                False: ignore energy during training, even if energy labels are available
                       in the dataset. The trained model will still be able to predict
                       energies up to an unknown integration constant. Note, that the
                       energy predictions accuracy will be untested.
            use_E_cstr : bool, optional
                True: include energy constraints in the kernel,
                False: default (s)GDML.
            use_cprsn : bool, optional
                True: compress kernel matrix along symmetric degrees of
                freedom,
                False: train using full kernel matrix

        Returns
        -------
            dict
                Data structure of custom type :obj:`task`.

        Raises
        ------
            ValueError
                If a reconstruction of the potential energy surface is requested,
                but the energy labels are missing in the dataset.
        """

        if use_E and 'E' not in train_dataset:
            raise ValueError(
                'No energy labels found in dataset!\n'
                + 'By default, force fields are always reconstructed including the\n'
                + 'corresponding potential energy surface (this can be turned off).\n'
                + 'However, the energy labels are missing in the provided dataset.\n'
            )

        use_E_cstr = use_E and use_E_cstr

        ui.progr_toggle(is_done=False, disp_str='Hashing dataset(s)...')
        md5_train = io.dataset_md5(train_dataset)
        md5_valid = io.dataset_md5(valid_dataset)
        ui.progr_toggle(is_done=True, disp_str='Hashing dataset(s)...')

        if model0 is not None and (
            md5_train != model0['md5_train'] or md5_valid != model0['md5_valid']
        ):
            raise ValueError(
                'Provided training and/or validation dataset(s) do(es) not match the ones in the initial model.'
            )

        if model0 is None:
            ui.progr_toggle(
                is_done=False, disp_str='Sampling training and validation subsets...'
            )
            if 'E' in train_dataset:
                idxs_train = self.draw_strat_sample(train_dataset['E'], n_train)
            else:
                idxs_train = np.random.choice(
                    np.arange(train_dataset['F'].shape[0]), n_train, replace=False
                )

            excl_idxs = idxs_train if md5_train == md5_valid else None
            if 'E' in valid_dataset:
                idxs_valid = self.draw_strat_sample(
                    valid_dataset['E'], n_valid, excl_idxs
                )
            else:
                idxs_valid_all = np.setdiff1d(
                    np.arange(valid_dataset['F'].shape[0]),
                    excl_idxs,
                    assume_unique=True,
                )
                idxs_valid = np.random.choice(idxs_valid_all, n_valid, replace=False)
            ui.progr_toggle(
                is_done=True, disp_str='Sampling training and validation subsets...'
            )
        else:
            idxs_train = model0['idxs_train']
            idxs_valid = model0['idxs_valid']

            ui.progr_toggle(
                is_done=True,
                disp_str='Transfering training and validation subsets from initial model...',
            )

        R_train = train_dataset['R'][idxs_train, :, :]
        task = {
            'type': 't',
            'code_version': __version__,
            'dataset_name': train_dataset['name'].astype(str),
            'dataset_theory': train_dataset['theory'].astype(str),
            'z': train_dataset['z'],
            'R_train': R_train,
            'F_train': train_dataset['F'][idxs_train, :, :],
            'idxs_train': idxs_train,
            'md5_train': md5_train,
            'idxs_valid': idxs_valid,
            'md5_valid': md5_valid,
            'sig': sig,
            'lam': lam,
            'use_E': use_E,
            'use_E_cstr': use_E_cstr,
            'use_sym': use_sym,
            'use_cprsn': use_cprsn,
        }

        if use_E:
            task['E_train'] = train_dataset['E'][idxs_train]

        if 'lattice' in train_dataset:
            task['lattice'] = train_dataset['lattice']

        if 'r_unit' in train_dataset and 'e_unit' in train_dataset:
            task['r_unit'] = train_dataset['r_unit']
            task['e_unit'] = train_dataset['e_unit']

        if model0 is None:
            if use_sym:
                n_train = R_train.shape[0]
                R_train_sync_mat = R_train
                if n_train > 1000:
                    R_train_sync_mat = R_train[np.random.choice(n_train, 1000), :, :]
                    self.log.info(
                        'Symmetry search has been restricted to a random subset of 1000/{:d} training points for faster convergence.'.format(
                            n_train
                        )
                    )

                task['perms'] = perm.find_perms(
                    R_train_sync_mat, train_dataset['z'], self._max_processes
                )
            else:
                task['perms'] = np.arange(train_dataset['R'].shape[1])[
                    None, :
                ]  # no symmetries
        else:
            task['perms'] = model0['perms']
            self.log.info('Reusing permutations from initial model.')

        if model0 is not None:
            if 'alphas_F' in model0:
                self.log.info('Reusing alphas from initial model.')
                task['alphas0_F'] = model0['alphas_F']

        return task

        Create a data structure of custom type `task`.

        These data structures serve as recipes for model creation,
        summarizing the configuration of one particular training run.
        Training and test points are sampled from the provided dataset,
        without replacement. If the same dataset if given for training
        and testing, the subsets are drawn without overlap.

        Each task also contains a choice for the hyper-parameters of the
        training process and the MD5 fingerprints of the used datasets.

        Parameters
        ----------
            train_dataset : :obj:`dict`
                Data structure of custom type :obj:`dataset` containing
                train dataset.
            n_train : int
                Number of training points to sample.
            valid_dataset : :obj:`dict`
                Data structure of custom type :obj:`dataset` containing
                validation dataset.
            n_valid : int
                Number of validation points to sample.
            sig : int
                Hyper-parameter (kernel length scale).
            lam : float, optional
                Hyper-parameter lambda (regularization strength).
            use_sym : bool, optional
                True: include symmetries (sGDML), False: GDML.
            use_E : bool, optional
                True: reconstruct force field with corresponding potential energy surface,
                False: ignore energy during training, even if energy labels are available
                       in the dataset. The trained model will still be able to predict
                       energies up to an unknown integration constant. Note, that the
                       energy predictions accuracy will be untested.
            use_E_cstr : bool, optional
                True: include energy constraints in the kernel,
                False: default (s)GDML.
            use_cprsn : bool, optional
                True: compress kernel matrix along symmetric degrees of
                freedom,
                False: train using full kernel matrix

        Returns
        -------
            dict
                Data structure of custom type :obj:`task`.

        Raises
        ------
            ValueError
                If a reconstruction of the potential energy surface is requested,
                but the energy labels are missing in the dataset.
        
Create a data structure of custom type `task`.
These data structures serve as recipes for model creation,
summarizing the configuration of one particular training run.
Training and test points are sampled from the provided dataset,
without replacement. If the same dataset if given for training
and testing, the subsets are drawn without overlap.

Each task also contains a choice for the hyper-parameters of the
training process and the MD5 fingerprints of the used datasets.

Parameters



Returns

dict
Data structure of custom type :obj:`task`.

Raises

ValueError
If a reconstruction of the potential energy surface is requested,
but the energy labels are missing in the dataset.
Create a data structure of custom type ` task ` . These data structures serve as recipes for model creation summarizing the configuration of one particular training run . Training and test points are sampled from the provided dataset without replacement . If the same dataset if given for training and testing the subsets are drawn without overlap . Each task also contains a choice for the hyper - parameters of the training process and the MD5 fingerprints of the used datasets . Parameters Returns dict Data structure of custom type : obj : ` task ` . Raises ValueError If a reconstruction of the potential energy surface is requested but the energy labels are missing in the dataset .
def create_task(
        self,
        train_dataset,
        n_train,
        valid_dataset,
        n_valid,
        sig,
        lam=1e-15,
        use_sym=True,
        use_E=True,
        use_E_cstr=False,
        use_cprsn=False,
        model0=None,  
    ):
        if use_E and 'E' not in train_dataset:
            raise ValueError(
                'No energy labels found in dataset!\n'
                + 'By default, force fields are always reconstructed including the\n'
                + 'corresponding potential energy surface (this can be turned off).\n'
                + 'However, the energy labels are missing in the provided dataset.\n'
            )
        use_E_cstr = use_E and use_E_cstr
        ui.progr_toggle(is_done=False, disp_str='Hashing dataset(s)...')
        md5_train = io.dataset_md5(train_dataset)
        md5_valid = io.dataset_md5(valid_dataset)
        ui.progr_toggle(is_done=True, disp_str='Hashing dataset(s)...')
        if model0 is not None and (
            md5_train != model0['md5_train'] or md5_valid != model0['md5_valid']
        ):
            raise ValueError(
                'Provided training and/or validation dataset(s) do(es) not match the ones in the initial model.'
            )
        if model0 is None:
            ui.progr_toggle(
                is_done=False, disp_str='Sampling training and validation subsets...'
            )
            if 'E' in train_dataset:
                idxs_train = self.draw_strat_sample(train_dataset['E'], n_train)
            else:
                idxs_train = np.random.choice(
                    np.arange(train_dataset['F'].shape[0]), n_train, replace=False
                )
            excl_idxs = idxs_train if md5_train == md5_valid else None
            if 'E' in valid_dataset:
                idxs_valid = self.draw_strat_sample(
                    valid_dataset['E'], n_valid, excl_idxs
                )
            else:
                idxs_valid_all = np.setdiff1d(
                    np.arange(valid_dataset['F'].shape[0]),
                    excl_idxs,
                    assume_unique=True,
                )
                idxs_valid = np.random.choice(idxs_valid_all, n_valid, replace=False)
            ui.progr_toggle(
                is_done=True, disp_str='Sampling training and validation subsets...'
            )
        else:
            idxs_train = model0['idxs_train']
            idxs_valid = model0['idxs_valid']
            ui.progr_toggle(
                is_done=True,
                disp_str='Transfering training and validation subsets from initial model...',
            )
        R_train = train_dataset['R'][idxs_train, :, :]
        task = {
            'type': 't',
            'code_version': __version__,
            'dataset_name': train_dataset['name'].astype(str),
            'dataset_theory': train_dataset['theory'].astype(str),
            'z': train_dataset['z'],
            'R_train': R_train,
            'F_train': train_dataset['F'][idxs_train, :, :],
            'idxs_train': idxs_train,
            'md5_train': md5_train,
            'idxs_valid': idxs_valid,
            'md5_valid': md5_valid,
            'sig': sig,
            'lam': lam,
            'use_E': use_E,
            'use_E_cstr': use_E_cstr,
            'use_sym': use_sym,
            'use_cprsn': use_cprsn,
        }
        if use_E:
            task['E_train'] = train_dataset['E'][idxs_train]
        if 'lattice' in train_dataset:
            task['lattice'] = train_dataset['lattice']
        if 'r_unit' in train_dataset and 'e_unit' in train_dataset:
            task['r_unit'] = train_dataset['r_unit']
            task['e_unit'] = train_dataset['e_unit']
        if model0 is None:
            if use_sym:
                n_train = R_train.shape[0]
                R_train_sync_mat = R_train
                if n_train > 1000:
                    R_train_sync_mat = R_train[np.random.choice(n_train, 1000), :, :]
                    self.log.info(
                        'Symmetry search has been restricted to a random subset of 1000/{:d} training points for faster convergence.'.format(
                            n_train
                        )
                    )
                task['perms'] = perm.find_perms(
                    R_train_sync_mat, train_dataset['z'], self._max_processes
                )
            else:
                task['perms'] = np.arange(train_dataset['R'].shape[1])[
                    None, :
                ]  
        else:
            task['perms'] = model0['perms']
            self.log.info('Reusing permutations from initial model.')
        if model0 is not None:
            if 'alphas_F' in model0:
                self.log.info('Reusing alphas from initial model.')
                task['alphas0_F'] = model0['alphas_F']
        return task
def create_task ( self , train_dataset , n_train , valid_dataset , n_valid , sig , lam = 1e-15 , use_sym = True , use_E = True , use_E_cstr = False , use_cprsn = False , model0 = None , ) : if use_E and 'E' not in train_dataset : raise ValueError ( 'No energy labels found in dataset!\n' + 'By default, force fields are always reconstructed including the\n' + 'corresponding potential energy surface (this can be turned off).\n' + 'However, the energy labels are missing in the provided dataset.\n' ) use_E_cstr = use_E and use_E_cstr ui . progr_toggle ( is_done = False , disp_str = 'Hashing dataset(s)...' ) md5_train = io . dataset_md5 ( train_dataset ) md5_valid = io . dataset_md5 ( valid_dataset ) ui . progr_toggle ( is_done = True , disp_str = 'Hashing dataset(s)...' ) if model0 is not None and ( md5_train != model0 [ 'md5_train' ] or md5_valid != model0 [ 'md5_valid' ] ) : raise ValueError ( 'Provided training and/or validation dataset(s) do(es) not match the ones in the initial model.' ) if model0 is None : ui . progr_toggle ( is_done = False , disp_str = 'Sampling training and validation subsets...' ) if 'E' in train_dataset : idxs_train = self . draw_strat_sample ( train_dataset [ 'E' ] , n_train ) else : idxs_train = np . random . choice ( np . arange ( train_dataset [ 'F' ] . shape [ 0 ] ) , n_train , replace = False ) excl_idxs = idxs_train if md5_train == md5_valid else None if 'E' in valid_dataset : idxs_valid = self . draw_strat_sample ( valid_dataset [ 'E' ] , n_valid , excl_idxs ) else : idxs_valid_all = np . setdiff1d ( np . arange ( valid_dataset [ 'F' ] . shape [ 0 ] ) , excl_idxs , assume_unique = True , ) idxs_valid = np . random . choice ( idxs_valid_all , n_valid , replace = False ) ui . progr_toggle ( is_done = True , disp_str = 'Sampling training and validation subsets...' ) else : idxs_train = model0 [ 'idxs_train' ] idxs_valid = model0 [ 'idxs_valid' ] ui . progr_toggle ( is_done = True , disp_str = 'Transfering training and validation subsets from initial model...' , ) R_train = train_dataset [ 'R' ] [ idxs_train , : , : ] task = { 'type' : 't' , 'code_version' : __version__ , 'dataset_name' : train_dataset [ 'name' ] . astype ( str ) , 'dataset_theory' : train_dataset [ 'theory' ] . astype ( str ) , 'z' : train_dataset [ 'z' ] , 'R_train' : R_train , 'F_train' : train_dataset [ 'F' ] [ idxs_train , : , : ] , 'idxs_train' : idxs_train , 'md5_train' : md5_train , 'idxs_valid' : idxs_valid , 'md5_valid' : md5_valid , 'sig' : sig , 'lam' : lam , 'use_E' : use_E , 'use_E_cstr' : use_E_cstr , 'use_sym' : use_sym , 'use_cprsn' : use_cprsn , } if use_E : task [ 'E_train' ] = train_dataset [ 'E' ] [ idxs_train ] if 'lattice' in train_dataset : task [ 'lattice' ] = train_dataset [ 'lattice' ] if 'r_unit' in train_dataset and 'e_unit' in train_dataset : task [ 'r_unit' ] = train_dataset [ 'r_unit' ] task [ 'e_unit' ] = train_dataset [ 'e_unit' ] if model0 is None : if use_sym : n_train = R_train . shape [ 0 ] R_train_sync_mat = R_train if n_train > 1000 : R_train_sync_mat = R_train [ np . random . choice ( n_train , 1000 ) , : , : ] self . log . info ( 'Symmetry search has been restricted to a random subset of 1000/{:d} training points for faster convergence.' . format ( n_train ) ) task [ 'perms' ] = perm . find_perms ( R_train_sync_mat , train_dataset [ 'z' ] , self . _max_processes ) else : task [ 'perms' ] = np . arange ( train_dataset [ 'R' ] . shape [ 1 ] ) [ None , : ] else : task [ 'perms' ] = model0 [ 'perms' ] self . log . info ( 'Reusing permutations from initial model.' ) if model0 is not None : if 'alphas_F' in model0 : self . log . info ( 'Reusing alphas from initial model.' ) task [ 'alphas0_F' ] = model0 [ 'alphas_F' ] return task
Create a data structure of custom type `task`.
Create a data structure of custom type ` task ` .
# TODO: document me """
        Create a data structure of custom type `task`.

        These data structures serve as recipes for model creation,
        summarizing the configuration of one particular training run.
        Training and test points are sampled from the provided dataset,
        without replacement. If the same dataset if given for training
        and testing, the subsets are drawn without overlap.

        Each task also contains a choice for the hyper-parameters of the
        training process and the MD5 fingerprints of the used datasets.

        Parameters
        ----------
            train_dataset : :obj:`dict`
                Data structure of custom type :obj:`dataset` containing
                train dataset.
            n_train : int
                Number of training points to sample.
            valid_dataset : :obj:`dict`
                Data structure of custom type :obj:`dataset` containing
                validation dataset.
            n_valid : int
                Number of validation points to sample.
            sig : int
                Hyper-parameter (kernel length scale).
            lam : float, optional
                Hyper-parameter lambda (regularization strength).
            use_sym : bool, optional
                True: include symmetries (sGDML), False: GDML.
            use_E : bool, optional
                True: reconstruct force field with corresponding potential energy surface,
                False: ignore energy during training, even if energy labels are available
                       in the dataset. The trained model will still be able to predict
                       energies up to an unknown integration constant. Note, that the
                       energy predictions accuracy will be untested.
            use_E_cstr : bool, optional
                True: include energy constraints in the kernel,
                False: default (s)GDML.
            use_cprsn : bool, optional
                True: compress kernel matrix along symmetric degrees of
                freedom,
                False: train using full kernel matrix

        Returns
        -------
            dict
                Data structure of custom type :obj:`task`.

        Raises
        ------
            ValueError
                If a reconstruction of the potential energy surface is requested,
                but the energy labels are missing in the dataset.
        """ # no symmetries
self
train_dataset
n_train
valid_dataset
n_valid
sig
lam
use_sym
use_E
use_E_cstr
use_cprsn
model0