Executes a Gradient Descent algorithm for
the given cost function

@param array    $samples
@param array    $targets
@param \Closure $gradientFunc
@param bool     $isBatch
176260
protected function runGradientDescent(array $samples, array $targets, \Closure $gradientFunc, bool $isBatch = false)
    {
        $class = $isBatch ? GD::class : StochasticGD::class;

        if (empty($this->optimizer)) {
            $this->optimizer = (new $class($this->featureCount))
                ->setLearningRate($this->learningRate)
                ->setMaxIterations($this->maxIterations)
                ->setChangeThreshold(1e-6)
                ->setEarlyStop($this->enableEarlyStop);
        }

        $this->weights = $this->optimizer->runOptimization($samples, $targets, $gradientFunc);
        $this->costValues = $this->optimizer->getCostValues();
    }
382526
public static function fromIterator(iterable $samples) : self
    {
        $samples = is_array($samples)
            ? $samples
            : iterator_to_array($samples, false);

        return self::build($samples);
    }
617837
public function copyRecursive($source, $target, $fileFilter = '/.*/')
    {
        $filesystem = new Filesystem();
        $finder = new Finder();
        $finder->files()->in($source)->name($fileFilter);

        /** @var SplFileInfo $file */
        foreach ($finder as $file) {
            $filesystem->copy($file, $target . DIRECTORY_SEPARATOR . $file->getRelativePathname());
        }
    }
837705
protected function doBatchInsert(array &$a, array $options = array())
    {
        $options = isset($options['safe']) ? $this->convertWriteConcern($options) : $options;
        $options = isset($options['wtimeout']) ? $this->convertWriteTimeout($options) : $options;
        $options = isset($options['timeout']) ? $this->convertSocketTimeout($options) : $options;
        return $this->riakCollection->batchInsert($a, $options);
    }
554637
def batch_fn(dataset, training, shapes, target_names,
             batch_size=32, eval_batch_size=32, bucket_batch_length=32,
             bucket_max_length=256, bucket_min_length=8,
             bucket_length_step=1.1, buckets=None):
  """"""
  del target_names
  # If bucketing is not specified, check if target shapes are variable.
  cur_batch_size = batch_size if training else eval_batch_size
  if buckets is None:
    variable_target_shapes = False
    target_shape = shapes[1]
    for dim in target_shape:
      if dim is None:
        variable_target_shapes = True
    tf.logging.info("Heuristically setting bucketing to %s based on shapes "
                    "of target tensors." % variable_target_shapes)
    if variable_target_shapes:
      batch_size_per_token = cur_batch_size * bucket_batch_length
      scheme = data_reader.batching_scheme(batch_size_per_token,
                                           bucket_max_length,
                                           bucket_min_length,
                                           bucket_length_step,
                                           drop_long_sequences=training)
      buckets = (scheme["boundaries"], scheme["batch_sizes"])

  if buckets:
    tf.logging.info("Bucketing with buckets %s." % str(buckets))
    def example_length(_, target):
      return tf.shape(target)[0]
    boundaries, batch_sizes = buckets
    dataset = dataset.apply(tf.data.experimental.bucket_by_sequence_length(
        example_length, boundaries, batch_sizes))
  else:
    dataset = dataset.padded_batch(cur_batch_size, shapes)
  return dataset
1128744
protected function runTraining(array $samples, array $targets): void
    {
        // The cost function is the sum of squares
        $callback = function ($weights, $sample, $target) {
            $this->weights = $weights;

            $output = $this->output($sample);
            $gradient = $output - $target;
            $error = $gradient ** 2;

            return [$error, $gradient];
        };

        $isBatch = $this->trainingType == self::BATCH_TRAINING;

        parent::runGradientDescent($samples, $targets, $callback, $isBatch);
    }
224493
public function batchRun($is_hook, $method_name, array $params = NULL)
    {
        // Prevents annoying notices.
		if($params == NULL) {
			$params = array();
		}

        // We collect the results into an array.
        $results = array();
        if(!$this->modules) $this->modules = array();
        foreach($this->modules as $name => $module) {
            if(method_exists($module, $method_name)) {
                $result = call_user_func_array(array($module, $method_name), $params);
                if($is_hook) { // Hooks are pre-emptive if they return something.
                    if($result) {
                        return $result;
                    }
                } else { // Collecting
                    $results[$name] = $result;
                }
            }
		}

        return $is_hook? false : $results;
    }
597604
protected function trainByLabel(array $samples, array $targets, array $allLabels = [])
    {
        // Overwrites the current value if it exist. $allLabels must be provided for each partialTrain run.
        if (!empty($allLabels)) {
            $this->allLabels = $allLabels;
        } else {
            $this->allLabels = array_keys(array_count_values($targets));
        }
        sort($this->allLabels, SORT_STRING);

        // If there are only two targets, then there is no need to perform OvR
        if (count($this->allLabels) == 2) {
            // Init classifier if required.
            if (empty($this->classifiers)) {
                $this->classifiers[0] = $this->getClassifierCopy();
            }

            $this->classifiers[0]->trainBinary($samples, $targets, $this->allLabels);
        } else {
            // Train a separate classifier for each label and memorize them

            foreach ($this->allLabels as $label) {
                // Init classifier if required.
                if (empty($this->classifiers[$label])) {
                    $this->classifiers[$label] = $this->getClassifierCopy();
                }

                list($binarizedTargets, $classifierLabels) = $this->binarizeTargets($targets, $label);
                $this->classifiers[$label]->trainBinary($samples, $binarizedTargets, $classifierLabels);
            }
        }

        // If the underlying classifier is capable of giving the cost values
        // during the training, then assign it to the relevant variable
        // Adding just the first classifier cost values to avoid complex average calculations.
        $classifierref = reset($this->classifiers);
        if (method_exists($classifierref, 'getCostValues')) {
            $this->costValues = $classifierref->getCostValues();
        }
    }
1079035
public function transform(array &$samples) : void
    {
        if ($this->categories === null) {
            throw new RuntimeException('Transformer has not been fitted.');
        }

        $templates = [];

        foreach ($this->categories as $column => $categories) {
            $templates[$column] = array_fill(0, count($categories), 0);
        }

        foreach ($samples as &$sample) {
            $vector = [];

            foreach ($this->categories as $column => $categories) {
                $category = $sample[$column];
                $features = $templates[$column];

                if (isset($categories[$category])) {
                    $features[$categories[$category]] = 1;
                }

                $vector = array_merge($vector, $features);

                unset($sample[$column]);
            }

            $sample = array_merge($sample, $vector);
        }
    }
1283028
protected function runTraining(array $samples, array $targets)
    {
        // The cost function is the sum of squares
        $callback = function ($weights, $sample, $target) {
            $this->weights = $weights;

            $prediction = $this->outputClass($sample);
            $gradient = $prediction - $target;
            $error = $gradient**2;

            return [$error, $gradient];
        };

        $this->runGradientDescent($samples, $targets, $callback);
    }
1110862
def on_batch_begin(self, last_input, last_target, **kwargs):
        ""
        self.acc_samples += last_input.shape[0]
        self.acc_batches += 1
705895
async function runSample() {
  const res = await mirror.locations.list({});
  console.log(res.data);
}
865187
public function transform(array &$samples) : void
    {
        if ($this->mins === null or $this->scales === null) {
            throw new RuntimeException('Transformer has not been fitted.');
        }

        foreach ($samples as &$sample) {
            foreach ($this->scales as $column => $scale) {
                $sample[$column] *= $scale;
                $sample[$column] += $this->mins[$column];
            }
        }
    }
383350
private void trainOnOneSample(Instance instance, int partialTreeIter, int i, int dataCount, CompletionService<ArrayList<BeamElement>> pool) throws InterruptedException, ExecutionException
    {
        boolean isPartial = instance.isPartial(options.rootFirst);

        if (partialTreeIter > i && isPartial)
            return;

        Configuration initialConfiguration = new Configuration(instance.getSentence(), options.rootFirst);
        Configuration firstOracle = initialConfiguration.clone();
        ArrayList<Configuration> beam = new ArrayList<Configuration>(options.beamWidth);
        beam.add(initialConfiguration);

        /**
         * The float is the oracle's cost
         * For more information see:
         * Yoav Goldberg and Joakim Nivre. "Training Deterministic Parsers with Non-Deterministic Oracles."
         * TACL 1 (2013): 403-414.
         * for the mean while we just use zero-cost oracles
         */
        Collection<Configuration> oracles = new HashSet<Configuration>();

        oracles.add(firstOracle);

        /**
         * For keeping track of the violations
         * For more information see:
         * Liang Huang, Suphan Fayong and Yang Guo. "Structured perceptron with inexact search."
         * In Proceedings of the 2012 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies,
         * pp. 142-151. Association for Computational Linguistics, 2012.
         */
        float maxViol = Float.NEGATIVE_INFINITY;
        Pair<Configuration, Configuration> maxViolPair = null;

        Configuration bestScoringOracle = null;
        boolean oracleInBeam = false;

        while (!ArcEager.isTerminal(beam) && beam.size() > 0)
        {
            /**
             *  generating new oracles
             *  it keeps the oracles which are in the terminal state
             */
            Collection<Configuration> newOracles = new HashSet<Configuration>();

            if (options.useDynamicOracle || isPartial)
            {
                bestScoringOracle = zeroCostDynamicOracle(instance, oracles, newOracles);
            }
            else
            {
                bestScoringOracle = staticOracle(instance, oracles, newOracles);
            }
            // try explore non-optimal transitions

            if (newOracles.size() == 0)
            {
//                System.err.println("...no oracle(" + dataCount + ")...");
                bestScoringOracle = staticOracle(instance, oracles, newOracles);
            }
            oracles = newOracles;

            TreeSet<BeamElement> beamPreserver = new TreeSet<BeamElement>();

            if (options.numOfThreads == 1 || beam.size() == 1)
            {
                beamSortOneThread(beam, beamPreserver);
            }
            else
            {
                for (int b = 0; b < beam.size(); b++)
                {
                    pool.submit(new BeamScorerThread(false, classifier, beam.get(b),
                                                     dependencyRelations, featureLength, b, options.rootFirst));
                }
                for (int b = 0; b < beam.size(); b++)
                {
                    for (BeamElement element : pool.take().get())
                    {
                        beamPreserver.add(element);
                        if (beamPreserver.size() > options.beamWidth)
                            beamPreserver.pollFirst();
                    }
                }
            }

            if (beamPreserver.size() == 0 || beam.size() == 0)
            {
                break;
            }
            else
            {
                oracleInBeam = false;

                ArrayList<Configuration> repBeam = new ArrayList<Configuration>(options.beamWidth);
                for (BeamElement beamElement : beamPreserver.descendingSet())
                {
//                    if (repBeam.size() >= options.beamWidth) // 只要beamWidth个configuration（这句是多余的）
//                        break;
                    int b = beamElement.index;
                    int action = beamElement.action;
                    int label = beamElement.label;
                    float score = beamElement.score;

                    Configuration newConfig = beam.get(b).clone();

                    ArcEager.commitAction(action, label, score, dependencyRelations, newConfig);
                    repBeam.add(newConfig);

                    if (!oracleInBeam && oracles.contains(newConfig))
                        oracleInBeam = true;
                }
                beam = repBeam;

                if (beam.size() > 0 && oracles.size() > 0)
                {
                    Configuration bestConfig = beam.get(0);
                    if (oracles.contains(bestConfig)) // 模型认为的最大分值configuration是zero cost
                    {
                        oracles = new HashSet<Configuration>();
                        oracles.add(bestConfig);
                    }
                    else // 否则
                    {
                        if (options.useRandomOracleSelection) // 随机选择一个 oracle
                        { // choosing randomly, otherwise using latent structured Perceptron
                            List<Configuration> keys = new ArrayList<Configuration>(oracles);
                            Configuration randomKey = keys.get(randGen.nextInt(keys.size()));
                            oracles = new HashSet<Configuration>();
                            oracles.add(randomKey);
                            bestScoringOracle = randomKey;
                        }
                        else // 选择 oracle中被模型认为分值最大的那个
                        {
                            oracles = new HashSet<Configuration>();
                            oracles.add(bestScoringOracle);
                        }
                    }

                    // do early update
                    if (!oracleInBeam && updateMode.equals("early"))
                        break;

                    // keep violations
                    if (!oracleInBeam && updateMode.equals("max_violation"))
                    {
                        float violation = bestConfig.getScore(true) - bestScoringOracle.getScore(true);//Math.abs(beam.get(0).getScore(true) - bestScoringOracle.getScore(true));
                        if (violation > maxViol)
                        {
                            maxViol = violation;
                            maxViolPair = new Pair<Configuration, Configuration>(bestConfig, bestScoringOracle);
                        }
                    }
                }
                else
                    break;
            }
        }

        // updating weights
        if (!oracleInBeam ||
                !bestScoringOracle.equals(beam.get(0)) // 虽然oracle在beam里面，但在最后时刻，它的得分不是最高
                )
        {
            updateWeights(initialConfiguration, maxViol, isPartial, bestScoringOracle, maxViolPair, beam);
        }
    }
2271
public double tune(AbstractStochasticCachingDiffUpdateFunction function, double[] initial, int sampleSize, double seta)
  {
    Timing timer = new Timing();
    int[] sample = function.getSample(sampleSize);
    double sobj = getObjective(function, initial, 1, sample);
    double besteta = 1;
    double bestobj = sobj;
    double eta = seta;
    int totest = 10;
    double factor = 2;
    boolean phase2 = false;
    while (totest > 0 || !phase2)
    {
      double obj = tryEta(function, initial, sample, eta);
      boolean okay = (obj < sobj);
      sayln("  Trying eta=" + eta + "  obj=" + obj + ((okay)? "(possible)":"(too large)"));
      if (okay)
      {
        totest -= 1;
        if (obj < bestobj) {
          bestobj = obj;
          besteta = eta;
        }
      }
      if (! phase2)
      {
        if (okay) {
          eta = eta * factor;
        } else {
          phase2 = true;
          eta = seta;
        }
      }
      if (phase2) {
        eta = eta / factor;
      }
    }
    // take it on the safe side (implicit regularization)
    besteta /= factor;
    // determine t
    t0 = (int) (1 / (besteta * lambda));
    sayln("  Taking eta=" + besteta + " t0=" + t0);
    sayln("  Tuning completed in: " + Timing.toSecondsString(timer.report()) + " s");
    return besteta;
  }
618218
public void setSampleUtterances(java.util.Collection<String> sampleUtterances) {
        if (sampleUtterances == null) {
            this.sampleUtterances = null;
            return;
        }

        this.sampleUtterances = new java.util.ArrayList<String>(sampleUtterances);
    }
1137376
public function transform(array &$samples) : void
    {
        foreach ($samples as &$sample) {
            foreach ($sample as &$feature) {
                if (is_string($feature) and is_numeric($feature)) {
                    $feature = (int) $feature == $feature
                        ? (int) $feature
                        : (float) $feature;
                }
            }
        }
    }