Skip to content

Commit e0b2986

Browse files
include_tape -> check beta
1 parent 27f4be9 commit e0b2986

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

theforce/calculator/active.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ def __init__(self, covariance=None, calculator=None, process_group=None, meta=No
157157
trajectory. A tape can be used for training a model by
158158
calc.include_tape(file)
159159
160+
test:
161+
For instance, if test=100 and 100 steps have passed since the last
162+
exact calculation, an exact calculation will be performed.
163+
This can be used for monitoring the on-the-fly ML accuracy and
164+
has no effect on training. The exact calculation (FP) will be saved
165+
in 'active_FP.traj' while the models predictions (ML) will be saved
166+
in 'active_ML.traj'. These files will be overwritten in the next
167+
simulation. The following command can be used for a quick description
168+
of ML errors
169+
170+
python -m theforce.regression.scores active_ML.traj active_FP.traj
171+
160172
ediff, ediff_lb, ediff_ub: -> for sampling the LCEs for sparse representation
161173
ediff controls the LCE sampling rate. You decrease ediff for higher accuracy
162174
or increase ediff for higher speed.
@@ -383,9 +395,9 @@ def _test(self):
383395
self._ktest += 1
384396
mode = 'a' if self._ktest > 1 else 'w'
385397
if self.rank == 0:
386-
ase.io.Trajectory('active_test.traj', mode).write(tmp)
398+
ase.io.Trajectory('active_FP.traj', mode).write(tmp)
387399
tmp.set_calculator(SinglePointCalculator(tmp, **self.results))
388-
ase.io.Trajectory('active_pred.traj', mode).write(tmp)
400+
ase.io.Trajectory('active_ML.traj', mode).write(tmp)
389401
# log
390402
self.log('testing energy: {}'.format(energy))
391403
dE = self.results['energy'] - energy
@@ -472,13 +484,18 @@ def get_covloss(self):
472484
return beta*vscale
473485

474486
def update_lce(self, loc, beta=None):
487+
if beta is None:
488+
k = self.model.gp.kern(loc, self.model.X)
489+
b = self.model.choli@k.detach().t()
490+
c = (b*b).sum() # /self.model.gp.kern(loc, loc)
491+
beta = ((1-c)*self.model._vscale[loc.number]).clamp(min=0.).sqrt()
475492
added = 0
476493
m = self.model.indu_counts[loc.number]
477494
if loc.number in self.model.gp.species:
478-
if beta and beta >= self.ediff_ub:
495+
if beta >= self.ediff_ub:
479496
self.model.add_inducing(loc)
480497
added = -1 if m < 2 else 1
481-
elif beta and beta < self.ediff_lb:
498+
elif beta < self.ediff_lb:
482499
pass
483500
else:
484501
ediff = (self.ediff if m > 1
@@ -557,6 +574,8 @@ def update(self, inducing=True, data=True):
557574
m = self.update_inducing() if inducing else 0
558575
try_real = self.blind or type(self._calc) == SinglePointCalculator
559576
update_data = (m > 0 and data) or not inducing
577+
if update_data and not inducing: # for include_tape
578+
update_data = self.get_covloss().max() > self.ediff
560579
n = self.update_data(try_fake=not try_real) if update_data else 0
561580
if m > 0 or n > 0:
562581
self.log('fit error (mean,std): E: {:.2g} {:.2g} F: {:.2g} {:.2g} R2: {:.4g}'.format(
@@ -565,8 +584,6 @@ def update(self, inducing=True, data=True):
565584
self.log(f'noise: {self.model.scaled_noise}')
566585
if self.pckl:
567586
self.model.to_folder(self.pckl)
568-
if n == 0 and type(self._calc) == SinglePointCalculator:
569-
self._test()
570587
self._update_args = {}
571588
return m, n
572589

@@ -578,6 +595,7 @@ def include_data(self, data):
578595
self._calc = atoms.calc
579596
atoms.set_calculator(self)
580597
atoms.get_potential_energy()
598+
atoms.set_calculator(self._calc)
581599
self._calc = _calc
582600

583601
def include_tape(self, tape):
@@ -594,6 +612,7 @@ def include_tape(self, tape):
594612
self._calc = obj.calc
595613
obj.set_calculator(self)
596614
obj.get_potential_energy()
615+
obj.set_calculator(self._calc)
597616
added_lce = [0, 0]
598617
elif cls == 'local':
599618
obj.stage(self.model.descriptors, True)

theforce/cl/train.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def train(*args, r=None):
88
calc = cline.gen_active_calc()
99
for arg in args:
1010
if arg.endswith('.sgpr'):
11-
if r is not None:
11+
if r is not None and r != '::':
1212
w = f'-r {r} option is ignored for {arg}'
1313
warnings.warn(w)
1414
calc.include_tape(arg)

theforce/regression/gppotential.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ def kldiv_normal(y, sigma):
935935
compared to the number of bins.
936936
"""
937937
delta = sigma/10
938-
width = 10*sigma
938+
width = 10*sigma
939939
x = torch.arange(0, width, delta)
940940
x = torch.cat([-x.flip(0)[:-1], x])
941941
p = (y.view(-1)-x.view(-1, 1)).div(delta).pow(2).mul(-0.5).exp().sum(dim=1)

0 commit comments

Comments
 (0)