<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.neuralnetwork

import com.kotlinnlp.simplednn.core.layers.*
import com.kotlinnlp.simplednn.core.arrays.UpdatableArray
import com.kotlinnlp.simplednn.core.optimizer.IterableParams

/**
 * [NetworkParameters] contains all the parameters of the layers defined in [layersConfiguration],
 * grouped per layer.
 *
 * @property layersConfiguration a list of configurations, one per layer
 * @property sparseInput whether the input is sparse or not
 */
class NetworkParameters(
  val layersConfiguration: List<LayerConfiguration>,
  private val sparseInput: Boolean = false
) : IterableParams<NetworkParameters>() {

  /**
   * An [Array] containing a [LayerParameters] for each layer.
   *
   * In [layersConfiguration] layers are defined as a list [x, y, z], but the structure
   * contains layers as input-output pairs [x-y, y-z].
   * The output of a layer is a reference of the input of the next layer.
   */
  val paramsPerLayer: Array<LayerParameters<*>> = Array(size = layersConfiguration.size - 1, init = {
    LayerParametersFactory(
      inputSize = layersConfiguration[it].size,
      outputSize = layersConfiguration[it + 1].size,
      connectionType = layersConfiguration[it + 1].connectionType!!,
      sparseInput = this.sparseInput && it == 0)
  })

  /**
   * The list of all parameters.
   */
  override val paramsList: Array<UpdatableArray<*>> = this.buildParamsList()

  /**
   * @return a new [NetworkParameters] containing a copy of all parameters of this
   */
  override fun copy(): NetworkParameters {

    val clonedParams = NetworkParameters(layersConfiguration = this.layersConfiguration, sparseInput = this.sparseInput)

    clonedParams.zip(this) { cloned, params ->
      cloned.values.assignValues(params.values)
    }

    return clonedParams
  }

  /**
   * @return the list with parameters of all layers
   */
  private fun buildParamsList(): Array<UpdatableArray<*>> {

    var layerIndex = 0
    var paramIndex = 0

    return Array(
      size = paramsPerLayer.sumBy { it.size },
      init = {

        if (paramIndex == this.paramsPerLayer[layerIndex].size) {
          layerIndex++
          paramIndex = 0
        }

        this.paramsPerLayer[layerIndex][paramIndex++] }
    )
  }
}<commit_msg>Code refactoring<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.neuralnetwork

import com.kotlinnlp.simplednn.core.layers.*
import com.kotlinnlp.simplednn.core.arrays.UpdatableArray
import com.kotlinnlp.simplednn.core.optimizer.IterableParams

/**
 * [NetworkParameters] contains all the parameters of the layers defined in [layersConfiguration],
 * grouped per layer.
 *
 * @property layersConfiguration a list of configurations, one per layer
 * @property sparseInput whether the input is sparse or not
 */
class NetworkParameters(
  val layersConfiguration: List<LayerConfiguration>,
  private val sparseInput: Boolean = false
) : IterableParams<NetworkParameters>() {

  /**
   * An [Array] containing a [LayerParameters] for each layer.
   *
   * In [layersConfiguration] layers are defined as a list [x, y, z], but the structure
   * contains layers as input-output pairs [x-y, y-z].
   * The output of a layer is a reference of the input of the next layer.
   */
  val paramsPerLayer: Array<LayerParameters<*>> = Array(size = layersConfiguration.size - 1, init = {
    LayerParametersFactory(
      inputSize = layersConfiguration[it].size,
      outputSize = layersConfiguration[it + 1].size,
      connectionType = layersConfiguration[it + 1].connectionType!!,
      sparseInput = this.sparseInput && it == 0)
  })

  /**
   * The list of all parameters.
   */
  override val paramsList: Array<UpdatableArray<*>> = this.buildParamsList()

  /**
   * @return a new [NetworkParameters] containing a copy of all parameters of this
   */
  override fun copy(): NetworkParameters {

    val clonedParams = NetworkParameters(layersConfiguration = this.layersConfiguration, sparseInput = this.sparseInput)

    clonedParams.zip(this) { cloned, params ->
      cloned.values.assignValues(params.values)
    }

    return clonedParams
  }

  /**
   * @return the list with parameters of all layers
   */
  private fun buildParamsList(): Array<UpdatableArray<*>> {

    var layerIndex = 0
    var paramIndex = 0

    return Array(
      size = this.paramsPerLayer.sumBy { it.size },
      init = {

        if (paramIndex == this.paramsPerLayer[layerIndex].size) {
          layerIndex++
          paramIndex = 0
        }

        this.paramsPerLayer[layerIndex][paramIndex++] }
    )
  }
}
{'fraction_non_alphanumeric': 0.12305650360257869, 'fraction_numerical': 0.005688282138794084, 'mean_word_length': 3.079659706109822, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '1376661', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1436, 'n_words': 455}