35 #ifndef SHARK_MODELS_KERNELS_GAUSSIAN_ARD_KERNEL_H 36 #define SHARK_MODELS_KERNELS_GAUSSIAN_ARD_KERNEL_H 58 template<
class InputType=RealVector>
64 struct InternalState:
public State{
67 void resize(std::size_t sizeX1,std::size_t sizeX2){
68 kxy.resize(sizeX1,sizeX2);
80 SHARK_RUNTIME_CHECK( gamma_init > 0,
"[ARDKernelUnconstrained::ARDKernelUnconstrained] Expected positive weight.");
97 {
return "ARDKernelUnconstrained"; }
104 noalias(
m_gammas) = exp(newParameters);
112 return boost::shared_ptr<State>(
new InternalState());
133 double eval(ConstInputReference x1, ConstInputReference x2)
const{
136 double dmnorm2 = diagonalMahalanobisDistanceSqr(x1, x2,
m_gammas);
137 return std::exp(-dmnorm2);
144 void eval(ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix& result)
const{
145 SIZE_CHECK(batchX1.size2() == batchX2.size2());
148 std::size_t sizeX1 = batchX1.size1();
149 std::size_t sizeX2 = batchX2.size1();
151 ensure_size(result,sizeX1,sizeX2);
153 for(std::size_t i = 0; i != sizeX1; ++i){
154 for(std::size_t j = 0; j != sizeX2; ++j){
155 double dmnorm2 = diagonalMahalanobisDistanceSqr(row(batchX1,i), row(batchX2,j),
m_gammas);
156 result(i,j)=std::exp(-dmnorm2);
165 void eval(ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix& result,
State& state)
const{
166 SIZE_CHECK(batchX1.size2() == batchX2.size2());
169 std::size_t sizeX1 = batchX1.size1();
170 std::size_t sizeX2 = batchX2.size1();
172 InternalState& s = state.
toState<InternalState>();
173 s.resize(sizeX1,sizeX2);
175 ensure_size(result,sizeX1,sizeX2);
177 for(std::size_t i = 0; i != sizeX1; ++i){
178 for(std::size_t j = 0; j != sizeX2; ++j){
179 double dmnorm2 = diagonalMahalanobisDistanceSqr(row(batchX1,i), row(batchX2,j),
m_gammas);
180 result(i,j) = std::exp(-dmnorm2);
181 s.kxy(i,j) = result(i,j);
193 ConstBatchInputReference batchX1,
194 ConstBatchInputReference batchX2,
195 RealMatrix
const& coefficients,
199 SIZE_CHECK(batchX1.size2() == batchX2.size2());
202 std::size_t sizeX1 = batchX1.size1();
203 std::size_t sizeX2 = batchX2.size1();
207 InternalState
const& s = state.
toState<InternalState>();
209 for(std::size_t i = 0; i != sizeX1; ++i){
210 for(std::size_t j = 0; j != sizeX2; ++j){
211 double coeff = coefficients(i,j) * s.kxy(i,j);
212 gradient -= coeff *
m_gammas *
sqr(row(batchX1,i)-row(batchX2,j));
222 ConstBatchInputReference batchX1,
223 ConstBatchInputReference batchX2,
224 RealMatrix
const& coefficientsX2,
226 BatchInputType& gradient
228 SIZE_CHECK(batchX1.size2() == batchX2.size2());
231 std::size_t sizeX1 = batchX1.size1();
232 std::size_t sizeX2 = batchX2.size1();
234 InternalState
const& s = state.
toState<InternalState>();
238 for(std::size_t i = 0; i != sizeX1; ++i){
239 for(std::size_t j = 0; j != sizeX2; ++j){
240 double coeff = coefficientsX2(i,j) * s.kxy(i,j);
241 row(gradient,i) += coeff *
m_gammas * (row(batchX1,i)-row(batchX2,j));