I use the pyMC to calibrate the parameter in my model. Generally, retrieval model is my model (blackbox) and the input of the loglikelihood is a parameter vector. The code is:
import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pymc as pm
from scipy import stats
import pytensor
import pytensor.tensor as pt
from ProSAIL.RTMS import ProSAIL
import numba as nb
def retrieval_model(parameters, design):
LAI, Cab, N, Cw, Cm, ALA, psoil = parameters
tts, tto, psi, bands = design
ref = ProSAIL(N, Cab, 8, Cw, Cm, LAI, ALA, 0.3, 0.8, psoil, tts, tto, psi, bands)
return ref
def build_cov(y_simu, multi_factor, add_factor, n_observations):
cov = np.zeros((n_observations, n_observations), dtype = np.float32)
for i in range(n_observations):
cov[i,i] = (y_simu[i]*multi_factor + add_factor)**2
return cov
def log_likelihood(RTM_parameters, ref, design):
multi_factor, add_factor = [0.1, 0.01]
y_simu = retrieval_model(RTM_parameters, design)
n_observations = y_simu.shape[0]
cov = build_cov(y_simu, multi_factor, add_factor, n_observations)
log_likelihood_value = stats.multivariate_normal.logpdf(x = ref, mean = y_simu, cov = cov)
return log_likelihood_value
class LogLike(pt.Op):
itypes = [pt.dvector] # input shape
otypes = [pt.dscalar] # outputs a single scalar value (the log likelihood)
def __init__(self, loglike, reflectance, design):
self.likelihood = loglike
self.reflectance = reflectance
self.design = design
def perform(self, node, inputs, outputs):
(RTM_parameters, ) = inputs
logl = self.likelihood(RTM_parameters, self.reflectance, self.design)
outputs[0][0] = np.array(logl) # output the log-likelihood
return outputs
if __name__ == '__main__':
ref = np.load('samples\\reflectance.npy')[0, :]
lai = np.load('samples\\parameter_samples.npy')[:, 0][0]
logl = LogLike(log_likelihood, ref, [30, 0, 0, [482, 561, 655, 865, 1609, 2201]])
with pm.Model():
LAI = pm.TruncatedNormal('LAI', mu = lai, sigma = 0.52, lower = 0.02, upper = 7)
Cab = pm.TruncatedNormal("Cab", mu =35, sigma = 30, lower = 5, upper = 75)
N = pm.Uniform('N', lower = 1.3, upper = 2.5)
Cw = pm.TruncatedNormal('Cw', mu = 0.02, sigma = 0.01, lower = 0.002, upper = 0.05)
Cm = pm.TruncatedNormal("Cm", mu = 0.005, sigma = 0.001, lower = 0.001, upper = 0.03)
ALA = pm.Uniform("ALA", lower = 40, upper = 70)
psoil = pm.Uniform("psoil",lower = 0.01, upper = 1)
rtm_parameters = pt.as_tensor_variable([LAI, Cab, N, Cw, Cm, ALA, psoil])
pm.Potential("likelihood", logl(rtm_parameters))
trace = pm.sample(20000, tune = 300, chains = 4, step=pm.Metropolis())
print(".")
However, i got that:
发生异常: TypeError
non-lazy thunk should return None, not list
Apply node that caused the error: LogLike(MakeVector{dtype='float64'}.0)
Toposort index: 8
Inputs types: [TensorType(float64, shape=(7,))]
Inputs shapes: [(7,)]
Inputs strides: [(8,)]
Inputs values: ['not shown']
Outputs clients: [[output[7](likelihood)]]
Backtrace when the node is created (use PyTensor flag traceback__limit=N to make it longer):
File "f:\conda_env\envs\covariance_calibration\Lib\runpy.py", line 88, in _run_code
exec(code, run_globals)
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
cli.main()
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
run()
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file
runpy.run_path(target, run_name="__main__")
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "D:\code\Error_strcuture_gplus\Sequential_with_KDE.py", line 61, in <module>
pm.Potential("likelihood", logl(rtm_parameters))
HINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.
TypeError: non-lazy thunk should return None, not list
During handling of the above exception, another exception occurred:
File "D:\code\Error_strcuture_gplus\Sequential_with_KDE.py", line 62, in <module>
trace = pm.sample(20000, tune = 300, chains = 4, step=pm.Metropolis())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: non-lazy thunk should return None, not list
Apply node that caused the error: LogLike(MakeVector{dtype='float64'}.0)
Toposort index: 8
Inputs types: [TensorType(float64, shape=(7,))]
Inputs shapes: [(7,)]
Inputs strides: [(8,)]
Inputs values: ['not shown']
Outputs clients: [[output[7](likelihood)]]
Backtrace when the node is created (use PyTensor flag traceback__limit=N to make it longer):
File "f:\conda_env\envs\covariance_calibration\Lib\runpy.py", line 88, in _run_code
exec(code, run_globals)
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
cli.main()
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
run()
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file
runpy.run_path(target, run_name="__main__")
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "c:\Users\fanda\.vscode\extensions\ms-python.debugpy-2024.6.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "D:\code\Error_strcuture_gplus\Sequential_with_KDE.py", line 61, in <module>
pm.Potential("likelihood", logl(rtm_parameters))
HINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.
I cannot find any information to solve this problem. How to process this error?