<commit_before>package neuroflow.nets.cpu

import breeze.linalg._
import breeze.numerics._
import breeze.stats._
import neuroflow.core.Network._
import neuroflow.core._
import neuroflow.nets.Registry

import scala.annotation.tailrec
import scala.collection.Seq
import scala.collection.mutable.ArrayBuffer
import scala.collection.parallel.ForkJoinTaskSupport
import scala.concurrent.forkjoin.ForkJoinPool

/**
  *
  * This is a convolutional feed-forward neural network.
  * It uses gradient descent to optimize the error function Σ1/2(y - net(x))².
  *
  * Use the parallelism parameter with care, as it greatly affects memory usage.
  *
  * @author bogdanski
  * @since 31.08.17
  *
  */

object ConvNetwork {
  implicit val constructor: Constructor[ConvNetwork] = new Constructor[ConvNetwork] {
    def apply(ls: Seq[Layer], settings: Settings)(implicit weightProvider: WeightProvider): ConvNetwork = {
      ConvNetwork(ls, settings, weightProvider(ls))
    }
  }
}

private[nets] case class ConvNetwork(layers: Seq[Layer], settings: Settings, weights: Weights,
                                     identifier: String = Registry.register()) extends ConvolutionalNetwork with KeepBestLogic with WaypointLogic {

  import Convolution.IntTupler
  import Network._

  private val _forkJoinTaskSupport = new ForkJoinTaskSupport(new ForkJoinPool(settings.parallelism))

  private val _allLayers = layers.map {
    case Focus(inner)    => inner
    case layer: Layer with HasActivator[Double]
                         => layer
  }.toArray

  private val _clusterLayer   = layers.collect { case c: Focus => c }.headOption

  private val _lastWlayerIdx  = weights.size - 1
  private val _convLayers     = layers.zipWithIndex.map(_.swap).filter {
    case (_, _: Convolution) => true
    case (_, Focus(_: Convolution)) => true
    case _ => false
  }.toMap.mapValues {
    case c: Convolution => c
    case Focus(c: Convolution) => c
  }

  private val _outputDim = _allLayers.last.neurons
  private val _lastC     = _convLayers.maxBy(_._1)._1
  private val _lastL     = _allLayers.indices.last

  private type Indices   = Map[(Int, Int), DenseMatrix[Int]]
  private val _indices   = collection.mutable.Map.empty[Int, Indices]

  /**
    * Computes output for `x`.<commit_msg>cosmetics<commit_after>package neuroflow.nets.cpu

import breeze.linalg._
import breeze.numerics._
import breeze.stats._
import neuroflow.core.Network._
import neuroflow.core._
import neuroflow.nets.Registry

import scala.annotation.tailrec
import scala.collection.Seq
import scala.collection.mutable.ArrayBuffer
import scala.collection.parallel.ForkJoinTaskSupport
import scala.concurrent.forkjoin.ForkJoinPool

/**
  *
  * This is a convolutional feed-forward neural network.
  * It uses gradient descent to optimize the error function Σ1/2(y - net(x))².
  *
  * Use the parallelism parameter with care, as it greatly affects memory usage.
  *
  * @author bogdanski
  * @since 31.08.17
  *
  */

object ConvNetwork {
  implicit val constructor: Constructor[ConvNetwork] = new Constructor[ConvNetwork] {
    def apply(ls: Seq[Layer], settings: Settings)(implicit weightProvider: WeightProvider): ConvNetwork = {
      ConvNetwork(ls, settings, weightProvider(ls))
    }
  }
}

private[nets] case class ConvNetwork(layers: Seq[Layer], settings: Settings, weights: Weights,
                                     identifier: String = Registry.register()) extends ConvolutionalNetwork with KeepBestLogic with WaypointLogic {

  import Convolution.IntTupler
  import Network._

  private val _forkJoinTaskSupport = new ForkJoinTaskSupport(new ForkJoinPool(settings.parallelism))

  private val _allLayers = layers.map {
    case Focus(inner)    => inner
    case layer: Layer with HasActivator[Double]
                         => layer
  }.toArray

  private val _clusterLayer   = layers.collect { case c: Focus => c }.headOption

  private val _lastWlayerIdx  = weights.size - 1
  private val _convLayers     = layers.zipWithIndex.map(_.swap).filter {
    case (_, _: Convolution)        => true
    case (_, Focus(_: Convolution)) => true
    case _                          => false
  }.toMap.mapValues {
    case c: Convolution        => c
    case Focus(c: Convolution) => c
  }

  private val _outputDim = _allLayers.last.neurons
  private val _lastC     = _convLayers.maxBy(_._1)._1
  private val _lastL     = _allLayers.indices.last

  private type Indices   = Map[(Int, Int), DenseMatrix[Int]]
  private val _indices   = collection.mutable.Map.empty[Int, Indices]

  /**
    * Computes output for `x`.
{'fraction_non_alphanumeric': 0.09311475409836066, 'fraction_numerical': 0.004808743169398907, 'mean_word_length': 3.416988416988417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5523725', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1343, 'n_words': 363}