16 #include "./base/base_uses.f90"
22 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'pao_ml_gaussprocess'
33 TYPE(pao_env_type),
POINTER :: pao
35 INTEGER :: i, ikind, info, j, npoints
36 REAL(
dp),
DIMENSION(:),
POINTER :: idescr, jdescr
37 TYPE(training_matrix_type),
POINTER :: training_matrix
40 DO ikind = 1,
SIZE(pao%ml_training_matrices)
41 training_matrix => pao%ml_training_matrices(ikind)
42 npoints =
SIZE(training_matrix%inputs, 2)
43 cpassert(
SIZE(training_matrix%outputs, 2) == npoints)
44 IF (npoints == 0) cycle
46 IF (pao%iw > 0)
WRITE (pao%iw, *)
"PAO|ML| Building covariance matrix for kind: ", &
47 trim(training_matrix%kindname),
" from ", npoints,
"training points."
50 ALLOCATE (training_matrix%GP(npoints, npoints))
53 idescr => training_matrix%inputs(:, i)
54 jdescr => training_matrix%inputs(:, j)
55 training_matrix%GP(i, j) = kernel(pao%gp_scale, idescr, jdescr)
56 training_matrix%GP(j, i) = training_matrix%GP(i, j)
62 training_matrix%GP(i, i) = training_matrix%GP(i, i) + pao%gp_noise_var**2
66 CALL dpotrf(
"U", npoints, training_matrix%GP, npoints, info)
81 TYPE(pao_env_type),
POINTER :: pao
82 INTEGER,
INTENT(IN) :: ikind
83 REAL(
dp),
DIMENSION(:),
INTENT(IN) :: descriptor
84 REAL(
dp),
DIMENSION(:),
INTENT(OUT) :: output
85 REAL(
dp),
INTENT(OUT) :: variance
87 INTEGER :: i, info, npoints
88 REAL(
dp),
ALLOCATABLE,
DIMENSION(:) :: cov, weights
89 TYPE(training_matrix_type),
POINTER :: training_matrix
91 training_matrix => pao%ml_training_matrices(ikind)
92 npoints =
SIZE(training_matrix%outputs, 2)
95 ALLOCATE (cov(npoints))
97 cov(i) = kernel(pao%gp_scale, descriptor, training_matrix%inputs(:, i))
101 ALLOCATE (weights(npoints))
103 CALL dpotrs(
"U", npoints, 1, training_matrix%GP, npoints, weights, npoints, info)
109 output(:) = output + weights(i)*training_matrix%outputs(:, i)
113 variance = kernel(pao%gp_scale, descriptor, descriptor) - dot_product(weights, cov)
115 IF (variance < 0.0_dp) &
116 cpabort(
"PAO gaussian process found negative variance")
118 DEALLOCATE (cov, weights)
130 TYPE(pao_env_type),
POINTER :: pao
131 INTEGER,
INTENT(IN) :: ikind
132 REAL(
dp),
DIMENSION(:),
INTENT(IN),
TARGET :: descriptor
133 REAL(
dp),
DIMENSION(:),
INTENT(IN) :: outer_deriv
134 REAL(
dp),
DIMENSION(:),
INTENT(OUT) :: gradient
136 INTEGER :: i, info, npoints
137 REAL(
dp),
ALLOCATABLE,
DIMENSION(:) :: cov_deriv, weights_deriv
138 REAL(
dp),
DIMENSION(SIZE(descriptor)) :: kg
139 TYPE(training_matrix_type),
POINTER :: training_matrix
141 training_matrix => pao%ml_training_matrices(ikind)
142 npoints =
SIZE(training_matrix%outputs, 2)
145 ALLOCATE (weights_deriv(npoints))
147 weights_deriv(i) = sum(outer_deriv*training_matrix%outputs(:, i))
151 ALLOCATE (cov_deriv(npoints))
152 cov_deriv(:) = weights_deriv(:)
153 CALL dpotrs(
"U", npoints, 1, training_matrix%GP, npoints, cov_deriv, npoints, info)
159 kg = kernel_grad(pao%gp_scale, descriptor, training_matrix%inputs(:, i))
160 gradient(:) = gradient(:) + kg(:)*cov_deriv(i)
163 DEALLOCATE (cov_deriv, weights_deriv)
173 PURE FUNCTION kernel(scale, descr1, descr2)
RESULT(cov)
174 REAL(
dp),
INTENT(IN) :: scale
175 REAL(
dp),
DIMENSION(:),
INTENT(IN) :: descr1, descr2
179 REAL(
dp),
DIMENSION(SIZE(descr1)) :: diff
181 diff = descr1 - descr2
182 fdist2 = sum((diff/scale)**2)
183 cov = exp(-fdist2/2.0_dp)
193 PURE FUNCTION kernel_grad(scale, descr1, descr2)
RESULT(grad)
194 REAL(
dp),
INTENT(IN) :: scale
195 REAL(
dp),
DIMENSION(:),
INTENT(IN) :: descr1, descr2
196 REAL(
dp),
DIMENSION(SIZE(descr1)) :: grad
198 REAL(
dp) :: cov, fdist2
199 REAL(
dp),
DIMENSION(SIZE(descr1)) :: diff
201 diff = descr1 - descr2
202 fdist2 = sum((diff/scale)**2)
203 cov = exp(-fdist2/2.0_dp)
204 grad(:) = cov*(-diff/scale**2)
206 END FUNCTION kernel_grad
Defines the basic variable types.
integer, parameter, public dp
Gaussian Process implementation.
subroutine, public pao_ml_gp_gradient(pao, ikind, descriptor, outer_deriv, gradient)
Calculate gradient of Gaussian process.
subroutine, public pao_ml_gp_train(pao)
Builds the covariance matrix.
subroutine, public pao_ml_gp_predict(pao, ikind, descriptor, output, variance)
Uses covariance matrix to make prediction.
Types used by the PAO machinery.