@Override public void trainOnInstanceImpl(MultiLabelInstance instance) { int numOutputs=instance.numberOutputTargets(); if(countVector==null){ countVector=new double[numOutputs]; } double weight=instance.weight(); for(int i=0; i<numOutputs;i++){ countVector[i]+=weight*instance.valueOutputAttribute(i); } numInstances+=weight; }
@Override public void addResult(Example<Instance> example, Prediction prediction) { MultiLabelInstance inst = (MultiLabelInstance) example.getData(); if (numberOutputs == 0) { numberOutputs = inst.numberOutputTargets(); } if (inst.weight() > 0.0) { this.weightObserved += inst.weight(); if (prediction != null ) { for (int i = 0; i< numberOutputs;i++){ double err = inst.classValue(i) - ((prediction.numOutputAttributes()==0) ? 0.0 : prediction.getVote(i,0)); this.squareError += (err) * (err); this.averageError += Math.abs(err); } } //System.out.println(inst.classValue()+", "+prediction); } }
/** * Update the model using the provided instance */ public void updatePerceptron(MultiLabelInstance inst) { // Update attribute statistics instancesSeen += inst.weight(); // Update weights double learningRatio = 0.0; if (tree.learningRatioConstOption.isSet()) { learningRatio = tree.learningRatioOption.getValue(); } else { learningRatio = learningRatioOption.getValue() / (1 + instancesSeen * tree.learningRateDecayFactorOption.getValue()); } // Loop for compatibility with bagging methods for (int i = 0; i < (int) inst.weight(); i++) { updateWeights(inst, learningRatio); } }
/** * Method for updating (training) the model using a new instance */ public void trainOnInstanceImpl(MultiLabelInstance inst) { if (inst.weight() > 0) { checkRoot(); double[] prediction = treeRoot.getPrediction(inst); double[] normalError = getNormalizedError(inst, prediction); //normalError.scaleValues(inst.weight()); processInstance(inst, treeRoot, prediction, normalError, true, false); examplesSeen += inst.weight(); for (int i = 0; i < inst.numberOutputTargets(); i++) { sumOfValues.addToValue(i, inst.weight() * inst.valueOutputAttribute(i)); sumOfSquares.addToValue(i, inst.weight() * inst.valueOutputAttribute(i) * inst.valueOutputAttribute(i)); } for (int i = 0; i < inst.numInputAttributes(); i++) { sumOfAttrValues.addToValue(i, inst.weight() * inst.valueInputAttribute(i)); sumOfAttrSquares.addToValue(i, inst.weight() * inst.valueInputAttribute(i) * inst.valueInputAttribute(i)); } } }
@Override public void addResult(Example<Instance> example, Prediction prediction) { MultiLabelInstance inst = (MultiLabelInstance) example.getData(); if (numberOutputs == 0) { numberOutputs = inst.numberOutputTargets(); } if(this.squareError==null){ this.squareError = new double[numberOutputs]; this.averageError = new double[numberOutputs]; this.averageErrorToTargetMean=new double[numberOutputs]; this.squareErrorToTargetMean=new double[numberOutputs]; this.sumY=new double[numberOutputs]; } if (inst.weight() > 0.0) { this.weightObserved += inst.weight(); if (prediction != null && prediction.numOutputAttributes()>0) { for (int i = 0; i< numberOutputs;i++){ double error=(inst.valueOutputAttribute(i) - prediction.getVote(i, 0)); this.sumY[i]+=inst.valueOutputAttribute(i); double errorTM=(inst.valueOutputAttribute(i) - this.sumY[i]/this.weightObserved); this.averageErrorToTargetMean[i]+=Math.abs(errorTM); this.squareErrorToTargetMean[i]+=errorTM*errorTM; this.averageError[i]+=Math.abs(error); this.squareError[i]+=error*error; } } //System.out.println(inst.classValue()+", "+prediction); } }
@Override public void trainOnInstanceImpl(MultiLabelInstance x) { int L = x.numberOutputTargets(); MultiLabelPrediction y = new MultiLabelPrediction(L); for(int j=0; j<L;j++) y.setVotes(j,new double[]{1- x.classValue(j), x.classValue(j)}); double freq = x.weight(); if (this.vectorCounts.containsKey(y.toString())) { freq += this.vectorCounts.get(y.toString()); } this.vectorCounts.put(y.toString(), (Double)freq); if (freq > this.maxValue) { this.maxValue = freq; this.majorityLabelset = y; } //System.out.println("---"+this.majorityLabelset); }
public void addPrediction(Prediction prediction, MultiLabelInstance inst){ int numOutputs=inst.numberOutputTargets(); Prediction trueClass= new MultiLabelPrediction(numOutputs); for (int i=0; i<numOutputs; i++){ trueClass.setVotes(i, new double[]{inst.valueOutputAttribute(i)}); } addPrediction(prediction, trueClass, inst.weight()); }
double w=instance.weight(); count+=w; Iterator<Integer> it=numericIndices.iterator(); double sumLayer=0; for(int i=0; i<numInputs; i++){ layer1Weights[i][j]+=currentLearningRate * delta * normInputs[i]*instance.weight(); sumLayer+=Math.abs(layer1Weights[i][j]); layer1Weights[numInputs][j]+=currentLearningRate * delta * instance.weight(); sumLayer+=Math.abs(layer1Weights[numInputs][j]); if(sumLayer>(numInputs)){ double sumLayer=0; for(int i=0; i<numOutputs; i++){ layer2Weights[i][j]+=learningRate2ndLayer * delta * firstLayerOutput[i]*instance.weight(); sumLayer+=Math.abs(layer2Weights[i][j]); layer2Weights[numOutputs][j]+=learningRate2ndLayer * delta * instance.weight(); sumLayer+=Math.abs(layer2Weights[numOutputs][j]); if(sumLayer>(numOutputs)){
break; } else { currentNode.examplesSeen += inst.weight();
if(sufficientStatistics==null) sufficientStatistics= new AutoExpandVector<double[]>(); weightSeen+=instance.weight(); for(int i=0; i<instance.numInputAttributes(); i++){ double [] stats=sufficientStatistics.get(i); stats=new double[]{instance.weight()*val,instance.weight()*val*val}; sufficientStatistics.set(i,stats);
@Override public void addResult(Example<Instance> testInst, Prediction prediction) { MultiLabelInstance inst=(MultiLabelInstance) testInst.getData(); double weight = inst.weight(); if (numberOutputs == 0) { numberOutputs = inst.numberOutputTargets(); } if (weight > 0.0) { if (TotalweightObserved == 0) { reset(inst.dataset().numClasses()); } this.TotalweightObserved += weight; this.weightObserved.add(weight); if (prediction!=null) { for (int i = 0; i< numberOutputs;i++){ double error=(inst.valueOutputAttribute(i) - prediction.getVote(i, 0)); this.squareError.add( error*error); this.averageError.add(Math.abs(error)); } } //System.out.println(inst.classValue()+", "+prediction[0]); } } }
examplesSeen += inst.weight(); sumOfValues.addToValue(i, inst.weight() * inst.valueOutputAttribute(i)); sumOfSquares.addToValue(i, inst.weight() * inst.valueOutputAttribute(i) * inst.valueOutputAttribute(i)); for (int j = 0; j < inst.numOutputAttributes(); j++) { observations[j] = new DoubleVector(); observations[j].setValue(0, inst.weight()); observations[j].setValue(1, inst.weight() * inst.valueOutputAttribute(j)); observations[j].setValue(2, inst.weight() * inst.valueOutputAttribute(j) * inst.valueOutputAttribute(j));
if(sufficientStatistics==null) sufficientStatistics= new AutoExpandVector<double[]>(); weightSeen+=instance.weight(); for(int i=0; i<instance.numInputAttributes(); i++){ double [] stats=sufficientStatistics.get(i); stats=new double[]{instance.weight()*val,instance.weight()*val*val}; sufficientStatistics.set(i,stats);
public void trainOnInstanceImpl(MultiLabelInstance instance) { if(featureRanking==null){ featureRanking= (FeatureRanking) getPreparedClassOption(this.featureRankingOption); for (int i = 0; i < this.ensemble.length; i++) { this.ensemble[i].setObserver(featureRanking); } nAttributes=instance.numInputAttributes(); } for (int i = 0; i < this.ensemble.length; i++) { MultiLabelInstance inst=(MultiLabelInstance)instance.copy(); int k = 1; if ( this.useBaggingOption.isSet()) { k = MiscUtils.poisson(1.0, this.classifierRandom); } if (k > 0) { //Instance weightedInst = transformInstance(inst,i); inst.setWeight(inst.weight() * k); //estimate error Prediction p=ensemble[i].getPredictionForInstance(inst); if(p!=null) errorMeasurer[i].addPrediction(p, inst); //train learner this.ensemble[i].trainOnInstance(inst); } } }
nAttributes=instance.numInputAttributes(); numInstances+=instance.weight(); debug("Train",3); debug("Nº instance "+numInstances + " - " + instance.toString(),3); this.numChangesDetected += instance.weight(); //Just for statistics } else { numAnomaliesDetected+=instance.weight();//Just for statistics
double weight = inst.weight(); if (numberOutputs == 0) { numberOutputs = inst.numberOutputTargets();