============================================= Choose the random-feature count automatically ============================================= Random Fourier features (RFF) replace the exact GP solve with a Bayesian linear regression on :math:`D` random cosine features. Too few features give a wrong surrogate, not a slow one. This guide turns on automatic selection, reads the report that it leaves, and shows what a refusal looks like. **Status:** implemented, opt-in. The default is ``0`` features, which keeps the exact posterior byte for byte. Automatic selection landed in commit ``fe78e72c7``. :doc:`GP settings ` describes the rule in full. The background and the closed a-priori alternative are in :doc:`Methods: approximations <../explanation/methods/approximations>`. Switches -------- .. table:: +-----------+-----------------------------------------------------------------------+------------------------------------------------------+ | Interface | Automatic selection | Fixed count | +===========+=======================================================================+======================================================+ | CLI | ``--rff-features auto``, ``--rff-force-tol-fraction F`` | ``--rff-features N`` | +-----------+-----------------------------------------------------------------------+------------------------------------------------------+ | TOML | ``[uncertainty] rff_features = "auto"``, ``rff_force_tol_fraction`` | ``rff_features = N`` | +-----------+-----------------------------------------------------------------------+------------------------------------------------------+ | Python | ``GaussianProcess(rff_features``"auto", rff\_force\_tol\_fraction=F)= | ``rff_features=N`` (needs ``uncertainty``"laplace"=) | +-----------+-----------------------------------------------------------------------+------------------------------------------------------+ | C | ``gpr_model_set_rff_features(model, -1, F)`` | ``gpr_model_set_rff_features(model, N, 0)`` | +-----------+-----------------------------------------------------------------------+------------------------------------------------------+ A fraction of ``0`` derives the tolerance from the gradient noise. The fraction must lie in [0, 1). The selection runs on the next train or update, and ``gpr_model_get_rff_selection`` returns the report. Before a train the report is empty. The classic profiles reset a fixed count to 0, but they keep automatic selection on. How the rule decides -------------------- The rule doubles the count from 1: 1, 2, 4, and so on, up to the largest power of two that leaves the design twice overdetermined. For each count it builds an exact GP and an RFF model on the same fit geometries, and compares them on the held-out geometries. It accepts the first count whose largest energy and force differences are below the tolerances. The tolerances come from the observation noise. The energy noise is the larger of the model ``sigma2`` and the floor 1e-6. The gradient noise is the larger of ``jitter_sigma2`` and the floor 1e-4. Both defaults are 1e-4. At the default fraction, 0.194467, the allowed force error is 0.01 eV/A, which is one standard deviation of the gradient noise. The allowed energy error scales with the energy noise by the same rule and is 0.01 eV at the defaults. If no count meets the tolerance, the rule refuses. It installs no count and the report keeps the errors that it measured. It never installs a smaller count than the one that failed. Run it from C ------------- The program trains a two-atom Lennard-Jones model on observations at separations between 0.95 and 1.95 in reduced units, with automatic selection on. Six observations leave too few geometries for the rule. Forty and sixty give it room, but the outcome depends on the data. The library also prints one log line per selection on the standard output. That line lists each count that it tried, with its energy and force differences, and ends with ``accepted`` or ``rejected``. .. code:: c #include #include #include #define N_COORDS 6 #define MAX_OBS 64 static int run(int n_obs) { double R[MAX_OBS * N_COORDS], E[MAX_OBS], G[MAX_OBS * N_COORDS]; gpr_rff_selection_t rep; gpr_atoms_t atoms = gpr_atoms_create_simple(2); gpr_model_t model = gpr_model_create(GPR_MODEL_GP); gpr_model_config_t config = gpr_model_config_defaults(); int i, status; config.report_level = 0; if (!atoms || !model) return 1; if (gpr_model_init(model, &config, atoms) != GPR_SUCCESS) return 1; if (gpr_model_set_rff_features(model, -1, 0.0) != GPR_SUCCESS) return 1; for (i = 0; i < n_obs; ++i) { memset(R + i * N_COORDS, 0, N_COORDS * sizeof *R); R[i * N_COORDS + 3] = 0.95 + (double)i / (n_obs - 1); gpr_potential_lj(R + i * N_COORDS, 2, 1.0, 1.0, E + i, G + i * N_COORDS); } status = gpr_model_train_checked(model, R, n_obs * N_COORDS, E, n_obs, G, n_obs * N_COORDS, n_obs, N_COORDS); if (gpr_model_get_rff_selection(model, &rep) != GPR_SUCCESS) return 1; printf("%d observations: train %s, accepted %d, features %d (ladder %d to %d)\n", n_obs, gpr_status_name(status), rep.accepted, rep.features, rep.start_features, rep.limit_features); printf(" fit %d, holdout %d, joint rows %d\n", rep.fit_geometries, rep.holdout_geometries, rep.joint_rows); printf(" allowed error: %.1e eV, %.1e eV/A (fraction %.4f)\n", rep.energy_tolerance, rep.force_tolerance, rep.force_tol_fraction); printf(" last error: %.1e eV, %.1e eV/A\n", rep.max_energy_error, rep.max_force_error); gpr_model_destroy(model); gpr_atoms_destroy(atoms); return 0; } int main(void) { if (run(6)) return 1; if (run(40)) return 1; if (run(60)) return 1; printf("selection compared\n"); return 0; } :: 6 observations: train GPR_ERR_INTERNAL, accepted 0, features 0 (ladder 1 to 16) fit 5, holdout 1, joint rows 35 allowed error: 1.0e-02 eV, 1.0e-02 eV/A (fraction 0.1945) last error: eV, eV/A 40 observations: train GPR_ERR_INTERNAL, accepted 0, features 0 (ladder 1 to 64) fit 30, holdout 10, joint rows 210 allowed error: 1.0e-02 eV, 1.0e-02 eV/A (fraction 0.1945) last error: eV, eV/A 60 observations: train GPR_SUCCESS, accepted 1, features 64 (ladder 1 to 128) fit 45, holdout 15, joint rows 315 allowed error: 1.0e-02 eV, 1.0e-02 eV/A (fraction 0.1945) last error: eV, eV/A selection compared Read the output as follows. - With 6 observations the full design has 42 rows and the fit split 35. The ladder stops at 16 features, the largest power of two that leaves both twice overdetermined. The error at 16 features, 2.9e-2 eV and 8.5e-1 eV/A, is above both tolerances. The rule refuses, ``gpr_model_train_checked`` returns ``GPR_ERR_INTERNAL``, and ``gpr_last_error_message`` holds the log line. The report keeps the last error. No count was installed. - With 40 observations the rule refuses as well. The ladder reaches 64 features and the held-out differences are 6.5e-4 eV and 1.1e-2 eV/A. The force difference is just above 1.0e-2 eV/A. - With 60 observations the rule accepts 64 features. The held-out differences are 5.1e-4 eV and 9.4e-3 eV/A, just inside the tolerances. The fit uses 45 geometries and holds out 15. A larger set does not always pass: in a run of this program 50 and 80 observations were refused and 30, 60 and 100 were accepted. Check ``accepted`` in the report and do not assume it. - The error does not fall at every step. In this run the force difference at 16 features is larger than at 8. The rule takes the first count that passes and does not interpolate. Run it from Python ------------------ The Python class reads the same report as a dict. The block below uses the same two-atom argon system as :doc:`GP settings `, with 60 configurations. .. code:: python import numpy as np from ase import Atoms from ase.calculators.lj import LennardJones from gpridge.gp import GaussianProcess calc = LennardJones(sigma=1.0, epsilon=1.0) def pair(d): atoms = Atoms("Ar2", positions=[[0, 0, 0], [d, 0, 0]], cell=[20, 20, 20], pbc=False) atoms.calc = calc return atoms gp = GaussianProcess(template_atoms=pair(1.0), rff_features="auto") for d in np.linspace(0.95, 1.95, 60): gp.observe(pair(d)) gp.fit() sel = gp.rff_selection print(f"accepted {sel['accepted']} features {sel['features']} " f"fit {sel['fit_geometries']} holdout {sel['holdout_geometries']}") assert sel["automatic"] and sel["accepted"] assert sel["max_force_error"] <= sel["force_tolerance"] :: accepted True features fit 45 holdout 15 What the record supports ------------------------ - The rule is the answer to a failed a-priori bound. Pull request 223 measured the sufficient count at :math:`2.5\times10^{19}` on an 811-geometry, 135-coordinate gradient GP, where the full Gram matrix is 90.64 GiB (``tests/gpr/ml/RFFFeatureSelectionTest.cpp``). On a four-geometry subsample of that surface the random-feature posterior differed from the exact one by 0.0362 eV/A at 512 features and 0.0025 eV/A at 2048 (``gp_settings.org``). - No wall-time number is recorded for the rule, and none is given here. - A fixed count is not a safe default. A two-atom example in :doc:`GP settings ` shows 64 features raising the force error from 1.2 to 27 meV/A. - Automatic selection does not support the atomic-environment model, the Student-t process, manifold or geodesic-heat kernels or a corrected-noise diagonal. Those combinations are refused. The ZBL and Morse prior means and the secant quadratic mean are all fitted as residuals, in the features and in the exact validation copy alike. - The Hessian-vector product of an RFF-backed model is the exact cosine-feature curvature. ``MeanHessianVectorIsExactAndMakesNoPredictions`` checks it against a Richardson difference and a frozen potential-call count.