36 #ifndef SHARK_MODELS_KERNELS_KERNELHELPERS_H 37 #define SHARK_MODELS_KERNELS_KERNELHELPERS_H 51 template<
class InputType,
class M,
class Device>
55 blas::matrix_expression<M, Device>& matrix,
56 double regularizer = 0
62 std::vector<std::size_t> batchStart(B+1,0);
63 for(std::size_t i = 1; i != B+1; ++i){
67 std::size_t N = batchStart[B];
68 ensure_size(matrix,N,N);
71 for (std::size_t i=0; i<B; i++){
72 std::size_t startX = batchStart[i];
73 std::size_t endX = batchStart[i+1];
75 std::size_t startY = batchStart[j];
76 std::size_t endY = batchStart[j+1];
77 RealMatrix submatrix = kernel(dataset.
batch(i), dataset.
batch(j));
78 noalias(subrange(matrix(),startX,endX,startY,endY))=submatrix;
82 for(std::size_t k = startX; k != endX; ++k){
83 matrix()(k,k) += static_cast<typename M::value_type>(regularizer);
94 template<
class InputType,
class M,
class Device>
99 blas::matrix_expression<M, Device>& matrix
105 std::vector<std::size_t> batchStart1(B1+1,0);
106 for(std::size_t i = 1; i != B1+1; ++i){
107 batchStart1[i] = batchStart1[i-1]+
batchSize(dataset1.
batch(i-1));
109 std::vector<std::size_t> batchStart2(B2+1,0);
110 for(std::size_t i = 1; i != B2+1; ++i){
111 batchStart2[i] = batchStart2[i-1]+
batchSize(dataset2.
batch(i-1));
115 std::size_t N1 = batchStart1[B1];
116 std::size_t N2 = batchStart2[B2];
117 ensure_size(matrix,N1,N2);
119 for (std::size_t i=0; i<B1; i++){
120 std::size_t startX = batchStart1[i];
121 std::size_t endX = batchStart1[i+1];
123 std::size_t startY = batchStart2[j];
124 std::size_t endY = batchStart2[j+1];
125 RealMatrix submatrix = kernel(dataset1.
batch(i), dataset2.
batch(j));
126 noalias(subrange(matrix(),startX,endX,startY,endY))=submatrix;
140 template<
class InputType>
144 double regularizer = 0
158 template<
class InputType>
180 template<
class InputType,
class WeightMatrix>
184 WeightMatrix
const& weights
188 RealVector kernelGradient(kp);
189 kernelGradient.clear();
192 RealVector blockGradient(kp);
193 boost::shared_ptr<State> state = kernel.
createState();
194 std::size_t startX = 0;
197 std::size_t startY = 0;
198 for (std::size_t j=0; j <= i; j++){
203 subrange(weights,startX,startX+sizeX,startY,startY+sizeY),
208 kernelGradient+=2*blockGradient;
210 kernelGradient+=blockGradient;
215 return kernelGradient;