My partner and I are trying to utilize Stan’s autograd function in the example found at this page:
https://pystan.readthedocs.io/en/latest/external_cpp.html
We are doing this in an attempt to increase the number of arguments that can be passed in at a given time. However, we are running into several errors when we try to pass in more than five arguments to our external c++ function. The code we are trying to implement is provided below.
This is the external c++ file:
template <typename T1, typename T2, typename T3, typename T4, typename T5>
typename boost::math::tools::promote_args<T1, T2, T3, T4, T5>::type
my_other_func (const T1& A, const T2& B, const T3& C, const T4& X, const T5& Y, std::ostream* pstream) {
typedef typename boost::math::tools::promote_args<T1, T2, T3, T4, T5>::type T;
T Z = A*X*X+B*X+C+Y;
return Z;
}
This is the PyStan code we are using, we are running PyStan 2.19.1.1:
import pystan
import os
model_code = """
functions {
real my_other_func(real A, real B, real C, real X, real Y);
}
data {
real X;
real Y;
}
parameters {
real A;
real B;
real C;
}
transformed parameters {
real E = my_other_func(A, B, C, X, Y);
}
model {
E ~ std_normal();
}
"""
include_dir = [os.path.join(".", "external_cpp")]
include_files = ["external_manual.hpp", "external_autograd.hpp"]
stan_model = pystan.StanModel(model_code=model_code,
verbose=True,
allow_undefined=True,
includes=include_files,
include_dirs=include_dir,
)
dict = {'X': 4,
'Y': 1}
fit = stan_model.sampling(data=dict, iter=1000, chains=4)
print(fit)
These are some of the errors output to the terminal on Ubuntu 18.04.3. These errors occur multiple times but they all reference the same type of error. I could post the entire terminal output if necessary; however, it is exceptionally long.
/tmp/pystan_i2r5z6dp/anon_model_aa7fce6b9639a550505fc2f8eeaafc68.hpp: In member function ‘void anon_model_aa7fce6b9639a550505fc2f8eeaafc68_namespace::anon_model_aa7fce6b9639a550505fc2f8eeaafc68::write_array(RNG&, std::vector<double>&, std::vector<int>&, std::vector<double>&, bool, bool, std::ostream*) const’:
/tmp/pystan_i2r5z6dp/anon_model_aa7fce6b9639a550505fc2f8eeaafc68.hpp:335:72: error: call of overloaded ‘my_other_func(double&, double&, double&, const double&, const double&, std::ostream*&)’ is ambiguous
stan::math::assign(E,my_other_func(A, B, C, X, Y, pstream__));
^
/tmp/pystan_i2r5z6dp/anon_model_aa7fce6b9639a550505fc2f8eeaafc68.hpp:231:47: error: call of overloaded ‘my_other_func(local_scalar_t__&, local_scalar_t__&, local_scalar_t__&, const double&, const double&, std::ostream*&)’ is ambiguous
stan::math::assign(E,my_other_func(A, B, C, X, Y, pstream__));
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
We appreciate any help or advice that can be provided! If there is any information that we failed to provide, please let us know and we will get back with you as soon as possible.