<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.functionalities.initializers

import com.kotlinnlp.simplednn.core.functionalities.randomgenerators.FixedRangeRandom
import com.kotlinnlp.simplednn.simplemath.ndarray.dense.DenseNDArray

/**
 * An initializer of dense arrays with the 'Glorot Initialization', as explained by <NAME>.
 *
 * References:
 * [Understanding the difficulty of training deep feedforward neural networks]
 * (http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf)
 *
 * @param enablePseudoRandom if true use pseudo-random with a seed
 * @param seed seed used for the pseudo-random
 */
class GlorotInitializer(private val enablePseudoRandom: Boolean = true, private val seed: Long = 743) : Initializer {

  companion object {

    /**
     * Private val used to serialize the class (needed from Serializable)
     */
    @Suppress("unused")
    private const val serialVersionUID: Long = 1L
  }

  /**
   * Initialize the values of the given [array].
   *
   * @param array a dense array
   */
  override fun initialize(array: DenseNDArray) {

    val randomGenerator = FixedRangeRandom(
      radius = Math.sqrt(6.0 / (array.rows + array.columns)),
      enablePseudoRandom = this.enablePseudoRandom,
      seed = this.seed)

    array.randomize(randomGenerator)
  }
}
<commit_msg>Fix pseudo-random seed generation<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.functionalities.initializers

import com.kotlinnlp.simplednn.core.functionalities.randomgenerators.FixedRangeRandom
import com.kotlinnlp.simplednn.simplemath.ndarray.dense.DenseNDArray
import java.util.*

/**
 * An initializer of dense arrays with the 'Glorot Initialization', as explained by <NAME>.
 *
 * References:
 * [Understanding the difficulty of training deep feedforward neural networks]
 * (http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf)
 *
 * @param enablePseudoRandom if true use pseudo-random with a seed
 * @param seed seed used for the pseudo-random
 */
class GlorotInitializer(private val enablePseudoRandom: Boolean = true, private val seed: Long = 743) : Initializer {

  companion object {

    /**
     * Private val used to serialize the class (needed from Serializable)
     */
    @Suppress("unused")
    private const val serialVersionUID: Long = 1L
  }

  /**
   * The random generator of seeds for the pseudo-random initialization.
   */
  private val seedGenerator = Random(this.seed)

  /**
   * Initialize the values of the given [array].
   *
   * @param array a dense array
   */
  override fun initialize(array: DenseNDArray) {

    val randomGenerator = FixedRangeRandom(
      radius = Math.sqrt(6.0 / (array.rows + array.columns)),
      enablePseudoRandom = this.enablePseudoRandom,
      seed = this.seedGenerator.nextLong())

    array.randomize(randomGenerator)
  }
}

{'fraction_non_alphanumeric': 0.12859994297120045, 'fraction_numerical': 0.010835471913316224, 'mean_word_length': 3.1125439624853457, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3638819', 'n_tokens_mistral': 1039, 'n_tokens_neox': 985, 'n_words': 314}