<commit_before>/* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
 * ------------------------------------------------------------------*/

package com.kotlinnlp.simplednn.core.optimizer

import com.kotlinnlp.simplednn.core.neuralnetwork.NetworkParameters
import com.kotlinnlp.simplednn.core.neuralnetwork.NeuralNetwork

/**
 *
 * @param neuralNetwork
 */
class ParamsErrorsAccumulator(val neuralNetwork: NeuralNetwork) {

  /**
   * A counter of times when errors were accumulated.
   */
  var count = 0
    private set

  /**
   * The structure in which to accumulate the errors of the network parameters.
   */
  private var paramsErrors: NetworkParameters = this.neuralNetwork.parametersErrorsFactory()

  /**
   * A boolean which indicates if [paramsErrors] is a reference of one given by the user or is created privately.
   */
  private var paramsErrorsByReference: Boolean = false

  /**
   * A boolean indicating if any errors are accumulated.
   */
  private val isEmpty: Boolean get() = this.count == 0

  /**
   * A boolean indicating if no errors were accumulated.
   */
  private val isNotEmpty: Boolean get() = this.count > 0

  /**
   * @param copy a Boolean indicating if the returned errors must be a copy or a reference
   *
   * @return the accumulated errors of the network parameters
   */
  fun getParamsErrors(copy: Boolean = true): NetworkParameters {
    require(this.isNotEmpty) { "Cannot get params errors without accumulating before" }

    val paramsError: NetworkParameters

    return if (copy) {
      paramsError = this.neuralNetwork.parametersErrorsFactory()
      paramsError.assignValues(this.paramsErrors)
      paramsError

    } else {
      this.paramsErrors
    }
  }

  /**
   * Reset the accumulated errors.
   */
  fun reset() {
    this.count = 0
  }

  /**
   * Divide the accumulated errors by the number of accumulations.
   */
  fun averageErrors() {

    if (this.count != 1) {<commit_msg>Fix average condition<commit_after>/* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
 * ------------------------------------------------------------------*/

package com.kotlinnlp.simplednn.core.optimizer

import com.kotlinnlp.simplednn.core.neuralnetwork.NetworkParameters
import com.kotlinnlp.simplednn.core.neuralnetwork.NeuralNetwork

/**
 *
 * @param neuralNetwork
 */
class ParamsErrorsAccumulator(val neuralNetwork: NeuralNetwork) {

  /**
   * A counter of times when errors were accumulated.
   */
  var count = 0
    private set

  /**
   * The structure in which to accumulate the errors of the network parameters.
   */
  private var paramsErrors: NetworkParameters = this.neuralNetwork.parametersErrorsFactory()

  /**
   * A boolean which indicates if [paramsErrors] is a reference of one given by the user or is created privately.
   */
  private var paramsErrorsByReference: Boolean = false

  /**
   * A boolean indicating if any errors are accumulated.
   */
  private val isEmpty: Boolean get() = this.count == 0

  /**
   * A boolean indicating if no errors were accumulated.
   */
  private val isNotEmpty: Boolean get() = this.count > 0

  /**
   * @param copy a Boolean indicating if the returned errors must be a copy or a reference
   *
   * @return the accumulated errors of the network parameters
   */
  fun getParamsErrors(copy: Boolean = true): NetworkParameters {
    require(this.isNotEmpty) { "Cannot get params errors without accumulating before" }

    val paramsError: NetworkParameters

    return if (copy) {
      paramsError = this.neuralNetwork.parametersErrorsFactory()
      paramsError.assignValues(this.paramsErrors)
      paramsError

    } else {
      this.paramsErrors
    }
  }

  /**
   * Reset the accumulated errors.
   */
  fun reset() {
    this.count = 0
  }

  /**
   * Divide the accumulated errors by the number of accumulations.
   */
  fun averageErrors() {

    if (this.count > 1) {
{'fraction_non_alphanumeric': 0.11516270482344795, 'fraction_numerical': 0.006000461573967228, 'mean_word_length': 2.7851528384279476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '5399483', 'n_tokens_mistral': 1229, 'n_tokens_neox': 1152, 'n_words': 424}