-
Notifications
You must be signed in to change notification settings - Fork 3
/
LinearKernelCombination.java
101 lines (84 loc) · 2.81 KB
/
LinearKernelCombination.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright 2014 Simone Filice and Giuseppe Castellucci and Danilo Croce and Roberto Basili
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package it.uniroma2.sag.kelp.kernel.standard;
import it.uniroma2.sag.kelp.data.example.Example;
import it.uniroma2.sag.kelp.kernel.Kernel;
import it.uniroma2.sag.kelp.kernel.KernelCombination;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonTypeName;
/**
* Weighted Linear Combination of Kernels
* <br>Given the kernels \(K_1 \ldots K_n\), with weights \(c_1 \ldots c_n\), the combination formula is:
* <br> \(\sum_{i}c_iK_i\)
* @author Simone Filice
*/
@JsonTypeName("linearComb")
public class LinearKernelCombination extends KernelCombination{
private Logger logger = LoggerFactory.getLogger(LinearKernelCombination.class);
private List<Float> weights;
/**
* @return the weights
*/
public List<Float> getWeights() {
return weights;
}
/**
* @param weights the weights to set
*/
public void setWeights(List<Float> weights) {
this.weights = weights;
}
public LinearKernelCombination(){
super();
this.weights = new ArrayList<Float>();
}
/**
* Adds a kernel with a corresponding weight to the linear combination of kernels
*
* @param weight the weight of the kernel to be added
* @param kernel the kernel to be added
*/
public void addKernel(float weight, Kernel kernel){
this.weights.add(weight);
this.toCombine.add(kernel);
}
/**
* Scales the weights in order to make their sum being equal to 1
*/
public void normalizeWeights() {
float sum = 0;
for(float weight : weights){
sum+=weight;
}
for (int i = 0; i < this.weights.size(); i++) {
float val = this.weights.get(i).floatValue();
this.weights.set(i, new Float(val / sum));
}
}
@Override
protected float kernelComputation(Example exA, Example exB) {
float sum = 0;
for (int i = 0; i < this.toCombine.size(); i++) {
logger.debug(i+" "+weights.get(i).floatValue()+"*"+toCombine.get(i).innerProduct(exA, exB));
if(weights.get(i).floatValue()!=0)
sum += weights.get(i).floatValue()
* toCombine.get(i).innerProduct(exA, exB);
}
return sum;
}
}