<commit_before>package ml.wolfe.term

import ml.wolfe._
import ml.wolfe.util.Math._

/**
 * @author riedel
 */
class TransformerSpecs extends WolfeSpec {

  import TermImplicits._
  import Transformer._

  "A depth first transformer" should {
    "transform a term tree in a depth first fashion" in {
      val x = Doubles.Var
      val y = Doubles.Var
      val term = x * 2.0 + x
      val expected = y * 2.0 + y
      val transformed = depthFirst(term) {
        case t if t == x => y
      }
      transformed should beStringEqual(expected)
    }
  }

  "A reusing depth first transformer" should {
    "return identical transformed terms for identical input terms" in {
      val x = Doubles.Var
      val t = x * x + x
      val transformed = depthFirstAndReuse(t) {
        case `x` => 2.0
      }
      val expected = 2.0.toConst * 2.0.toConst + 2.0.toConst
      transformed._1 should beStringEqual(expected)
      val Sum(Vector(Product(Vector(c1, c2)), c3)) = transformed._1
      (c1 eq c2) should be(true)
      (c1 eq c3) should be(true)
    }

    "return identical transformed terms for identical composed input terms" in {
      val x = Doubles.Var
      val m = mem(x map (_ + 1.0))
      val t = m - m
      val transformed = depthFirstAndReuse(t) {
        case `x` => 2.0
      }
      val m2 = mem(2.0.toConst map (_ + 1.0))
      val expected = m2 - m2
      transformed._1 should beStringEqual(expected)
      val Sum(Vector(t1,Product(Vector(t2,_)))) = transformed._1
      (t1 eq t2) should be (true)
    }

    "not throw a class cast exception when using an ifThenElse term" in {
      @domain case class Theta(params: IndexedSeq[Vect])
      implicit val Thetas = Theta.Values(Seqs(Vectors(2), 3))
      def loss(t: Thetas.Term): DoubleTerm = {
        val score: DoubleTerm = t.params(0) dot t.params(0)
        ifThenElse(true.toConst)(score)(score)
      }
      val init = Settings(Thetas.createRandomSetting(random.nextGaussian()))
      argmax(Thetas)(t => loss(t).argmaxBy(Argmaxer.adaGrad(AdaGradParameters(100, 0.01, initParams = init)))).eval()
    }
  }

  "A reusing depth last transformer" should {
    "return identical transformed terms for identical input terms" in {
      val x = Doubles.Var
      val t = x * x + x
      val transformed = depthLastAndReuse(t) {
        case `x` => 2.0
      }
      val expected = 2.0.toConst * 2.0.toConst + 2.0.toConst
      transformed._1 should beStringEqual(expected)
      val Sum(Vector(Product(Vector(c1, c2)), c3)) = transformed._1
      (c1 eq c2) should be(true)
      (c1 eq c3) should be(true)
    }
    "return identical transformed terms for identical composed input terms" in {
      val x = Doubles.Var
      val m = mem(x map (_ + 1.0))<commit_msg>ignored spec<commit_after>package ml.wolfe.term

import ml.wolfe._
import ml.wolfe.util.Math._

/**
 * @author riedel
 */
class TransformerSpecs extends WolfeSpec {

  import TermImplicits._
  import Transformer._

  "A depth first transformer" should {
    "transform a term tree in a depth first fashion" in {
      val x = Doubles.Var
      val y = Doubles.Var
      val term = x * 2.0 + x
      val expected = y * 2.0 + y
      val transformed = depthFirst(term) {
        case t if t == x => y
      }
      transformed should beStringEqual(expected)
    }
  }

  "A reusing depth first transformer" should {
    "return identical transformed terms for identical input terms" in {
      val x = Doubles.Var
      val t = x * x + x
      val transformed = depthFirstAndReuse(t) {
        case `x` => 2.0
      }
      val expected = 2.0.toConst * 2.0.toConst + 2.0.toConst
      transformed._1 should beStringEqual(expected)
      val Sum(Vector(Product(Vector(c1, c2)), c3)) = transformed._1
      (c1 eq c2) should be(true)
      (c1 eq c3) should be(true)
    }

    "return identical transformed terms for identical composed input terms" in {
      val x = Doubles.Var
      val m = mem(x map (_ + 1.0))
      val t = m - m
      val transformed = depthFirstAndReuse(t) {
        case `x` => 2.0
      }
      val m2 = mem(2.0.toConst map (_ + 1.0))
      val expected = m2 - m2
      transformed._1 should beStringEqual(expected)
      val Sum(Vector(t1,Product(Vector(t2,_)))) = transformed._1
      (t1 eq t2) should be (true)
    }

    "not throw a class cast exception when using an ifThenElse term" ignore {
      @domain case class Theta(params: IndexedSeq[Vect])
      implicit val Thetas = Theta.Values(Seqs(Vectors(2), 3))
      def loss(t: Thetas.Term): DoubleTerm = {
        val score: DoubleTerm = t.params(0) dot t.params(0)
        ifThenElse(true.toConst)(score)(score)
      }
      val init = Settings(Thetas.createRandomSetting(random.nextGaussian()))
      argmax(Thetas)(t => loss(t).argmaxBy(Argmaxer.adaGrad(AdaGradParameters(100, 0.01, initParams = init)))).eval()
    }
  }

  "A reusing depth last transformer" should {
    "return identical transformed terms for identical input terms" in {
      val x = Doubles.Var
      val t = x * x + x
      val transformed = depthLastAndReuse(t) {
        case `x` => 2.0
      }
      val expected = 2.0.toConst * 2.0.toConst + 2.0.toConst
      transformed._1 should beStringEqual(expected)
      val Sum(Vector(Product(Vector(c1, c2)), c3)) = transformed._1
      (c1 eq c2) should be(true)
      (c1 eq c3) should be(true)
    }
    "return identical transformed terms for identical composed input terms" in {
      val x = Doubles.Var
      val m = mem(x map (_ + 1.0))
{'fraction_non_alphanumeric': 0.10983815239134388, 'fraction_numerical': 0.02436806692125841, 'mean_word_length': 2.485424588086185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3632242', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1786, 'n_words': 542}