<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.layers.helpers

import com.kotlinnlp.simplednn.core.layers.LayerParameters
import com.kotlinnlp.simplednn.core.layers.Layer
import com.kotlinnlp.simplednn.simplemath.ndarray.NDArray

/**
 * The helper which executes the backward on a [layer].
 */
interface BackwardHelper<InputNDArrayType : NDArray<InputNDArrayType>> {

  /**
   * The [Layer] in which the backward is executed.
   */
  val layer: Layer<InputNDArrayType>

  /**
   * Executes the backward calculating the errors of the parameters and eventually of the input through the SGD
   * algorithm, starting from the preset errors of the output array.
   *
   * @param paramsErrors the errors of the parameters which will be filled
   * @param propagateToInput whether to propagate the errors to the input array
   */
  fun backward(paramsErrors: LayerParameters<*>, propagateToInput: Boolean = false)
}<commit_msg>Change from Interface to Abstract class and add the paramsErrorsCollector as cache of params errors<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.layers.helpers

import com.kotlinnlp.simplednn.core.arrays.ParamsArray
import com.kotlinnlp.simplednn.core.layers.Layer
import com.kotlinnlp.simplednn.core.optimizer.GenericParamsErrorsCollector
import com.kotlinnlp.simplednn.core.optimizer.ParamsErrorsList
import com.kotlinnlp.simplednn.simplemath.ndarray.NDArray

/**
 * The helper which executes the backward on a [layer].
 *
 * @param layer the layer in which the backward is executed
 */
abstract class BackwardHelper<InputNDArrayType : NDArray<InputNDArrayType>>(
  protected open val layer: Layer<InputNDArrayType>
) {

  /**
   * The errors of the parameters which will be filled at each [backward].
   */
  private val paramsErrorsCollector = GenericParamsErrorsCollector()

  /**
   * Executes the backward calculating the errors of the parameters and eventually of the input through the SGD
   * algorithm, starting from the preset errors of the output array.
   *
   * @param propagateToInput whether to propagate the errors to the input array
   */
  fun backward(propagateToInput: Boolean = false): ParamsErrorsList {

    this.execBackward(propagateToInput)

    return this.paramsErrorsCollector.getAll()
  }

  /**
   * Executes the backward calculating the errors of the parameters and eventually of the input through the SGD
   * algorithm, starting from the preset errors of the output array.
   *
   * @param propagateToInput whether to propagate the errors to the input array
   */
  abstract fun execBackward(propagateToInput: Boolean = false)

  /**
   *
   */
  val ParamsArray.errors: ParamsArray.Errors<*> get() = paramsErrorsCollector.getErrors(this)
}
{'fraction_non_alphanumeric': 0.11375661375661375, 'fraction_numerical': 0.004703115814226925, 'mean_word_length': 3.4080310880829017, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '536363', 'n_tokens_mistral': 966, 'n_tokens_neox': 910, 'n_words': 336}