package com.naaturel.ANN.implementation.gradientDescent;

import com.naaturel.ANN.domain.abstraction.AlgorithmStrategy;
import com.naaturel.ANN.domain.abstraction.TrainingContext;
import com.naaturel.ANN.implementation.simplePerceptron.SimpleTrainingContext;

import java.util.stream.Stream;

public class SquareLossStrategy implements AlgorithmStrategy {

    private final TrainingContext context;

    public SquareLossStrategy(TrainingContext context) {
        this.context = context;
    }

    @Override
    public void apply() {
        Stream<Float> deltaStream = this.context.deltas.stream();
        this.context.localLoss = deltaStream.reduce(0.0F, (acc, d) -> (float) (acc + Math.pow(d, 2)));
        this.context.localLoss /= 2;
    }
}
