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

import com.kotlinnlp.simplednn.simplemath.NDArray

/**
 * Augmented Mean Squared Error calculator
 *
 * @property pi pi (0.1 by default)
 * @property c c (10 by default)
 */
class AugmentedMSECalculator(val pi: Double = 0.1, val c: Double = 10.0) : LossCalculator {

  /**
   *
   */
  var injectedError: Double = 0.0

  /**
   *
   */
  private val isLossPartitionEnabled: Boolean = pi != 0.0

  /**
   *
   */
  private val lossPartition: Double = 1.0 - pi

  /**
   * Calculate the loss within an output and its gold.
   *
   * @param output  current output layer
   * @param outputGold expected binary output
   *
   * @return the loss within [output] and [outputGold]
   */
  override fun calculateLoss(output: NDArray, outputGold: NDArray): NDArray {

    if (!this.isLossPartitionEnabled) {
      return output.sub(outputGold).assignPow(2.0).assignProd(0.5)

    } else {
      val lossContribute = outputGold.sum(output).assignPow(2.0)
      val injectedContribute = output.prod(this.calculateRegularization()).assignPow(2.0)

      // 0.5 * ((1 - pi) * (g - o)^2 + pi * (o * reg)^2)
      return lossContribute.assignProd(this.lossPartition)
        .assignSum(injectedContribute.assignProd(this.pi))
        .assignProd(0.5)
    }
  }

  /**
   * Calculate the errors within an output and its gold.
   *
   * @param output current output layer
   * @param outputGold expected binary output
   *
   * @return the derivative of the loss within [output] and [outputGold]
   */
  override fun calculateErrors(output: NDArray, outputGold: NDArray): NDArray {

    if (!this.isLossPartitionEnabled) {
      return output.sub(outputGold)

    } else {
      val regularization: Double = this.calculateRegularization()

      // (1 - pi) * (o - g) + pi * (o * reg)
      return output.sub(outputGold).assignProd(this.lossPartition)
        .assignSum(
          output.prod(regularization).assignProd(pi))
    }<commit_msg>Fix bug in calculations<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.losses

import com.kotlinnlp.simplednn.simplemath.NDArray

/**
 * Augmented Mean Squared Error calculator
 *
 * @property pi pi (0.1 by default)
 * @property c c (10 by default)
 */
class AugmentedMSECalculator(val pi: Double = 0.1, val c: Double = 10.0) : LossCalculator {

  /**
   *
   */
  var injectedError: Double = 0.0

  /**
   *
   */
  private val isLossPartitionEnabled: Boolean = pi != 0.0

  /**
   *
   */
  private val lossPartition: Double = 1.0 - pi

  /**
   * Calculate the loss within an output and its gold.
   *
   * @param output  current output layer
   * @param outputGold expected binary output
   *
   * @return the loss within [output] and [outputGold]
   */
  override fun calculateLoss(output: NDArray, outputGold: NDArray): NDArray {

    if (!this.isLossPartitionEnabled) {
      return output.sub(outputGold).assignPow(2.0).assignProd(0.5)

    } else {
      val lossContribute = outputGold.sub(output).assignPow(2.0)
      val injectedContribute = output.prod(this.calculateRegularization()).assignPow(2.0)

      // 0.5 * ((1 - pi) * (g - o)^2 + pi * (o * reg)^2)
      return lossContribute.assignProd(this.lossPartition)
        .assignSum(injectedContribute.assignProd(this.pi))
        .assignProd(0.5)
    }
  }

  /**
   * Calculate the errors within an output and its gold.
   *
   * @param output current output layer
   * @param outputGold expected binary output
   *
   * @return the derivative of the loss within [output] and [outputGold]
   */
  override fun calculateErrors(output: NDArray, outputGold: NDArray): NDArray {

    if (!this.isLossPartitionEnabled) {
      return output.sub(outputGold)

    } else {
      val regularization: Double = this.calculateRegularization()

      // (1 - pi) * (o - g) + pi * (o * reg)
      return output.sub(outputGold).assignProd(this.lossPartition)
        .assignSum(
          output.prod(regularization).assignProd(pi))
    }
{'fraction_non_alphanumeric': 0.14146134239592184, 'fraction_numerical': 0.01656754460492778, 'mean_word_length': 2.6391035548686244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3407522', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1420, 'n_words': 407}