35 #ifndef SHARK_MODELS_KERNELS_MULTITASKKERNEL_H 36 #define SHARK_MODELS_KERNELS_MULTITASKKERNEL_H 41 #include "Impl/MklKernelBase.h" 53 template <
class InputTypeT>
82 #ifndef DOXYGEN_SHOULD_SKIP_THIS 84 BOOST_FUSION_ADAPT_TPL_STRUCT(
91 template<
class InputType>
92 struct Batch< MultiTaskSample<InputType> >{
128 template <
class InputTypeT >
147 KernelType& inputkernel,
151 , mpe_inputKernel(&inputkernel)
158 {
return "GaussianTaskKernel"; }
161 return mpe_inputKernel->parameterVector() | m_gamma;
165 std::size_t kParams = mpe_inputKernel->numberOfParameters();
166 mpe_inputKernel->setParameterVector(subrange(newParameters,0,kParams));
167 m_gamma = newParameters.back();
172 return mpe_inputKernel->numberOfParameters() + 1;
186 {
return (1.0 / std::sqrt(2 * m_gamma)); }
201 m_gamma = 1.0 / (2.0 * sigma * sigma);
214 base_type::write(ar);
239 const std::size_t tasks = numberOfTasks();
240 std::size_t elements = m_data.numberOfElements();
241 std::vector<std::size_t> ell(tasks, 0);
242 for (std::size_t i=0; i<elements; i++)
243 ell[m_data.element(i).task]++;
246 for (std::size_t i=0; i<elements; i++){
247 const std::size_t task_i = m_data.element(i).task;
248 for (std::size_t j=0; j<i; j++){
249 const std::size_t task_j = m_data.element(j).task;
250 const double k = mpe_inputKernel->eval(m_data.element(i).input, m_data.element(j).input);
251 base_type::m_matrix(task_i, task_j) += k;
252 base_type::m_matrix(task_j, task_i) += k;
254 const double k = mpe_inputKernel->eval(m_data.element(i).input, m_data.element(i).input);
255 base_type::m_matrix(task_i, task_i) += k;
257 for (std::size_t i=0; i<tasks; i++){
258 if (ell[i] == 0)
continue;
259 for (std::size_t j=0; j<tasks; j++){
260 if (ell[j] == 0)
continue;
261 base_type::m_matrix(i, j) /= (double)(ell[i] * ell[j]);
266 for (std::size_t i=0; i<tasks; i++)
268 const double norm2_i = base_type::m_matrix(i, i);
269 for (std::size_t j=0; j<i; j++)
271 const double norm2_j = base_type::m_matrix(j, j);
272 const double dist2 = norm2_i + norm2_j - 2.0 * base_type::m_matrix(i, j);
273 const double k = std::exp(-m_gamma * dist2);
274 base_type::m_matrix(i, j) = base_type::m_matrix(j, i) = k;
277 for (std::size_t i=0; i<tasks; i++) base_type::m_matrix(i, i) = 1.0;
296 template <
class InputTypeT>
298 :
private detail::MklKernelBase<MultiTaskSample<InputTypeT> >
302 typedef detail::MklKernelBase<MultiTaskSample<InputTypeT> > base_type1;
311 InputKernelType* inputkernel,
313 :base_type1(
boost::fusion::make_vector(inputkernel,taskkernel))
314 ,base_type2(base_type1::makeKernelVector())
319 {
return "MultiTaskKernel"; }