<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.deeplearning.multipredictionscorer

import com.kotlinnlp.simplednn.core.functionalities.updatemethods.UpdateMethod

import com.kotlinnlp.simplednn.core.neuralnetwork.NetworkParameters
import com.kotlinnlp.simplednn.core.optimizer.Optimizer
import com.kotlinnlp.simplednn.core.optimizer.ParamsOptimizer

/**
 * The [Optimizer] of the MultiPredictionScorer.
 *
 * @param model the [MultiPredictionModel] to optimize
 * @property updateMethod the update method helper (Learning Rate, ADAM, AdaGrad, ...)
 */
class MultiPredictionOptimizer(
  model: MultiPredictionModel,
  updateMethod: UpdateMethod<*>
) : Optimizer(updateMethod) {

  /**
   * A list of [ParamsOptimizer]s, one for each sub-network.
   */
  private val networksOptimizers: List<ParamsOptimizer<NetworkParameters>> = List(
    size = model.networks.size,
    init = { i -> ParamsOptimizer(params = model.networks[i].model, updateMethod = updateMethod) }
  )

  /**
   * Update the parameters of all the sub-networks of the [MultiPredictionScorer].
   */
  override fun update() {
    this.networksOptimizers.forEach { it.update() }
  }

  /**
   * Accumulate the given [networksErrors] for each sub-network of the [MultiPredictionScorer].
   *
   * @param networksErrors a map of sub-networks indices to their params errors
   * @param copy a Boolean indicating if the [networksErrors] can be used as reference or must be copied.
   *             Set copy = false to optimize the accumulation when the amount of the errors to accumulate is 1.
   *             (default = true)
   */
  fun accumulate(networksErrors: Map<Int, NetworkParameters>, copy: Boolean = true) {

    networksErrors.forEach { networkIndex, errors ->
      this.networksOptimizers[networkIndex].accumulate(errors, copy = copy)
    }
  }
}
<commit_msg>Add accumulate() method for a given sub-network<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.deeplearning.multipredictionscorer

import com.kotlinnlp.simplednn.core.functionalities.updatemethods.UpdateMethod

import com.kotlinnlp.simplednn.core.neuralnetwork.NetworkParameters
import com.kotlinnlp.simplednn.core.optimizer.Optimizer
import com.kotlinnlp.simplednn.core.optimizer.ParamsOptimizer

/**
 * The [Optimizer] of the MultiPredictionScorer.
 *
 * @param model the [MultiPredictionModel] to optimize
 * @property updateMethod the update method helper (Learning Rate, ADAM, AdaGrad, ...)
 */
class MultiPredictionOptimizer(
  model: MultiPredictionModel,
  updateMethod: UpdateMethod<*>
) : Optimizer(updateMethod) {

  /**
   * A list of [ParamsOptimizer]s, one for each sub-network.
   */
  private val networksOptimizers: List<ParamsOptimizer<NetworkParameters>> = List(
    size = model.networks.size,
    init = { i -> ParamsOptimizer(params = model.networks[i].model, updateMethod = updateMethod) }
  )

  /**
   * Update the parameters of all the sub-networks of the [MultiPredictionScorer].
   */
  override fun update() {
    this.networksOptimizers.forEach { it.update() }
  }

  /**
   * Accumulate the given [networksErrors] for each sub-network of the [MultiPredictionScorer].
   *
   * @param networksErrors a map of sub-networks indices to their params errors
   * @param copy a Boolean indicating if the [networksErrors] can be used as reference or must be copied.
   *             Set copy = false to optimize the accumulation when the amount of the errors to accumulate is 1.
   *             (default = true)
   */
  fun accumulate(networksErrors: Map<Int, NetworkParameters>, copy: Boolean = true) {

    networksErrors.forEach { networkIndex, errors ->
      this.networksOptimizers[networkIndex].accumulate(errors, copy = copy)
    }
  }

  /**
   * Accumulate the given [errors] of the model sub-network with the given [networkIndex].
   *
   * @param networkIndex the index of a network of the model
   * @param errors the errors of the network with the given [networkIndex]
   * @param copy a Boolean indicating if the [errors] can be used as reference or must be copied.
   *             Set copy = false to optimize the accumulation when the amount of the errors to accumulate is 1.
   *             (default = true)
   */
  fun accumulate(networkIndex: Int, errors: NetworkParameters, copy: Boolean = true) {
    this.networksOptimizers[networkIndex].accumulate(errors, copy = copy)
  }
}

{'fraction_non_alphanumeric': 0.11636651494161884, 'fraction_numerical': 0.0037601424896101326, 'mean_word_length': 3.2976190476190474, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '4829259', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1432, 'n_words': 460}