<commit_before>#ifndef OPENGM_LEARNING_GRADIENT_ACCUMULATOR_H__
#define OPENGM_LEARNING_GRADIENT_ACCUMULATOR_H__

namespace opengm {
namespace learning {

/**
 * Model function visitor to accumulate the gradient for each model weight, 
 * given a configuration.
 */
template <typename ModelWeights, typename ConfigurationType>
class GradientAccumulator {

public:

	/**
	 * How to accumulate the gradient on the provided ModelWeights.
	 */
	enum Mode {

		Add,

		Subtract
	};

	/**
	 * @param gradient
	 *              ModelWeights reference to store the gradients. Gradient 
	 *              values will only be added (or subtracted, if mode == 
	 *              Subtract), so you have to make sure gradient is properly 
	 *              initialized to zero.
	 *
	 * @param configuration
	 *              Configuration of the variables in the model, to evaluate the 
	 *              gradient for.
	 *
	 * @param mode
	 *              Add or Subtract the weight gradients from gradient.
	 */
	GradientAccumulator(ModelWeights& gradient, const ConfigurationType& configuration, Mode mode = Add) :
		_gradient(gradient),
		_configuration(configuration),
		_mode(mode) {

		for (size_t i = 0; i < gradient.numberOfWeights(); i++)
			gradient[i] = 0;
	}

	template <typename Iterator, typename FunctionType>
	void operator()(Iterator begin, Iterator end, const FunctionType& function) {

		ConfigurationType localConfiguration;
		for (Iterator j = begin; j != end; j++)
			localConfiguration.push_back(_configuration[*j]);

		std::cout << "asking a function for gradient with configuration " << localConfiguration << std::endl;

		for (int i = 0; i < function.numberOfWeights(); i++) {

			int index = function.weightIndex(i);

			double g = function.weightGradient(i, localConfiguration.begin());

			std::cout << "gradient for weight " << index << " is " << g << std::endl;<commit_msg>removed debug code from gradient-accumulator<commit_after>#ifndef OPENGM_LEARNING_GRADIENT_ACCUMULATOR_H__
#define OPENGM_LEARNING_GRADIENT_ACCUMULATOR_H__

namespace opengm {
namespace learning {

/**
 * Model function visitor to accumulate the gradient for each model weight, 
 * given a configuration.
 */
template <typename ModelWeights, typename ConfigurationType>
class GradientAccumulator {

public:

	/**
	 * How to accumulate the gradient on the provided ModelWeights.
	 */
	enum Mode {

		Add,

		Subtract
	};

	/**
	 * @param gradient
	 *              ModelWeights reference to store the gradients. Gradient 
	 *              values will only be added (or subtracted, if mode == 
	 *              Subtract), so you have to make sure gradient is properly 
	 *              initialized to zero.
	 *
	 * @param configuration
	 *              Configuration of the variables in the model, to evaluate the 
	 *              gradient for.
	 *
	 * @param mode
	 *              Add or Subtract the weight gradients from gradient.
	 */
	GradientAccumulator(ModelWeights& gradient, const ConfigurationType& configuration, Mode mode = Add) :
		_gradient(gradient),
		_configuration(configuration),
		_mode(mode) {

		for (size_t i = 0; i < gradient.numberOfWeights(); i++)
			gradient[i] = 0;
	}

	template <typename Iterator, typename FunctionType>
	void operator()(Iterator begin, Iterator end, const FunctionType& function) {

		ConfigurationType localConfiguration;
		for (Iterator j = begin; j != end; j++)
			localConfiguration.push_back(_configuration[*j]);

		for (int i = 0; i < function.numberOfWeights(); i++) {

			int index = function.weightIndex(i);

			double g = function.weightGradient(i, localConfiguration.begin());
{'fraction_non_alphanumeric': 0.09203980099502487, 'fraction_numerical': 0.001658374792703151, 'mean_word_length': 3.354993983152828, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6881312', 'n_tokens_mistral': 1095, 'n_tokens_neox': 957, 'n_words': 331}