35 #ifndef SHARK_MODELS_KERNELS_POLYNOMIAL_KERNEL_H 36 #define SHARK_MODELS_KERNELS_POLYNOMIAL_KERNEL_H 51 template<
class InputType = RealVector>
57 struct InternalState:
public State{
59 RealMatrix exponentedProd;
61 void resize(std::size_t sizeX1, std::size_t sizeX2){
62 base.resize(sizeX1, sizeX2);
63 exponentedProd.resize(sizeX1, sizeX2);
77 PolynomialKernel(
unsigned int degree = 2,
double offset = 0.0,
bool degree_is_parameter =
true,
bool unconstrained =
false )
91 {
return "PolynomialKernel"; }
125 SHARK_ASSERT(std::abs((
unsigned int)newParameters(0) - newParameters(0)) <= 2*std::numeric_limits<double>::epsilon());
127 m_degree = (
unsigned int)newParameters(0);
129 m_offset = std::exp(newParameters(1));
137 m_offset = std::exp(newParameters(0));
154 return boost::shared_ptr<State>(
new InternalState());
160 double eval(ConstInputReference x1, ConstInputReference x2)
const{
162 double base = inner_prod(x1, x2) +
m_offset;
166 void eval(ConstBatchInputReference batchX1,ConstBatchInputReference batchX2, RealMatrix& result)
const {
167 SIZE_CHECK(batchX1.size2() == batchX2.size2());
168 std::size_t sizeX1 = batchX1.size1();
169 std::size_t sizeX2 = batchX2.size1();
170 result.resize(sizeX1,sizeX2);
173 noalias(result) = prod(batchX1,trans(batchX2));
177 noalias(result) = pow(result,
m_degree);
180 void eval(ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix& result,
State& state)
const{
181 SIZE_CHECK(batchX1.size2() == batchX2.size2());
183 std::size_t sizeX1 = batchX1.size1();
184 std::size_t sizeX2 = batchX2.size1();
186 InternalState& s = state.
toState<InternalState>();
187 s.resize(sizeX1,sizeX2);
188 result.resize(sizeX1,sizeX2);
191 noalias(s.base) = prod(batchX1,trans(batchX2));
196 noalias(result) = pow(s.base,
m_degree);
198 noalias(result) = s.base;
199 noalias(s.exponentedProd) = result;
205 ConstBatchInputReference batchX1,
206 ConstBatchInputReference batchX2,
207 RealMatrix
const& coefficients,
212 std::size_t sizeX1 = batchX1.size1();
213 std::size_t sizeX2 = batchX2.size1();
215 InternalState
const& s = state.
toState<InternalState>();
218 SIZE_CHECK(batchX1.size2() == batchX2.size2());
221 SIZE_CHECK(s.exponentedProd.size1() == sizeX1);
222 SIZE_CHECK(s.exponentedProd.size2() == sizeX2);
227 gradient(0) = sum(coefficients);
233 gradient(0) =
m_degree * sum(safe_div(s.exponentedProd,s.base,0.0) * coefficients);
242 ConstBatchInputReference batchX1,
243 ConstBatchInputReference batchX2,
244 RealMatrix
const& coefficientsX2,
246 BatchInputType& gradient
249 std::size_t sizeX1 = batchX1.size1();
250 std::size_t sizeX2 = batchX2.size1();
251 gradient.resize(sizeX1,batchX1.size2());
253 InternalState
const& s = state.
toState<InternalState>();
256 SIZE_CHECK(batchX1.size2() == batchX2.size2());
259 SIZE_CHECK(s.exponentedProd.size1() == sizeX1);
260 SIZE_CHECK(s.exponentedProd.size2() == sizeX2);
268 noalias(gradient) = prod(coefficientsX2,batchX2);
274 RealMatrix weights = coefficientsX2 * safe_div(s.exponentedProd,s.base,0.0);
278 noalias(gradient) =
m_degree * prod(weights,batchX2);