(DataPoint dp)
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public DataPoint transform(DataPoint dp) |
| 226 | { |
| 227 | final Vec x = dp.getNumericalValues(); |
| 228 | final List<Double> qi = dm.getQueryInfo(x); |
| 229 | Vec sv = new SparseVector(numCentroids); |
| 230 | double sum = 0; |
| 231 | /* |
| 232 | * Keep track of the highest activation in case none of the neurons have |
| 233 | * a numericaly stable activation value. if this occurs we will do our |
| 234 | * best by simply setting the one largest activation |
| 235 | */ |
| 236 | double maxActivation = Double.NEGATIVE_INFINITY; |
| 237 | int highestNeuron = -1; |
| 238 | |
| 239 | for(int i = 0; i < centroids.size(); i++) |
| 240 | { |
| 241 | double dist = dm.dist(i, x, qi, centroids, centroidDistCache); |
| 242 | double sig = bandwidths[i]; |
| 243 | double activation = Math.exp(-(dist*dist)/(sig*sig*2)); |
| 244 | |
| 245 | if(activation > maxActivation) |
| 246 | { |
| 247 | maxActivation = activation; |
| 248 | highestNeuron = i; |
| 249 | } |
| 250 | |
| 251 | if(activation > 1e-16) |
| 252 | { |
| 253 | sv.set(i, activation); |
| 254 | sum += activation; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if(sv.nnz() == 0)//no activations |
| 259 | { |
| 260 | sv.set(highestNeuron, maxActivation); |
| 261 | sum = maxActivation; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | if(normalize && sum != 0.0)//-0.0 not an issue with rbf kernel |
| 266 | sv.mutableDivide(sum); |
| 267 | if(sv.nnz() > sv.length()/2)//at this point we would be using more memory than needed. Just switch to dense |
| 268 | sv = new DenseVector(sv); |
| 269 | |
| 270 | return new DataPoint(sv, dp.getCategoricalValues(), dp.getCategoricalData(), dp.getWeight()); |
| 271 | } |
| 272 | /** |
| 273 | * The first phase of learning a RBF Neural Network is to determine the |
| 274 | * neuron locations. This enum controls which method is used. |
no test coverage detected