original_string: protected function gradient(array $theta): array
      {
          $costs = [];
          $gradient = [];
          $totalPenalty = 0;
  
          if ($this->gradientCb === null) {
              throw new InvalidOperationException('Gradient callback is not defined');
          }
  
          foreach ($this->samples as $index => $sample) {
              $target = $this->targets[$index];
  
              $result = ($this->gradientCb)($theta, $sample, $target);
              [$cost, $grad, $penalty] = array_pad($result, 3, 0);
  
              $costs[] = $cost;
              $gradient[] = $grad;
              $totalPenalty += $penalty;
          }
  
          $totalPenalty /= $this->sampleCount;
  
          return [$costs, $gradient, $totalPenalty];
      }
code: protected function gradient(array $theta): array
      {
          $costs = [];
          $gradient = [];
          $totalPenalty = 0;
  
          if ($this->gradientCb === null) {
              throw new InvalidOperationException('Gradient callback is not defined');
          }
  
          foreach ($this->samples as $index => $sample) {
              $target = $this->targets[$index];
  
              $result = ($this->gradientCb)($theta, $sample, $target);
              [$cost, $grad, $penalty] = array_pad($result, 3, 0);
  
              $costs[] = $cost;
              $gradient[] = $grad;
              $totalPenalty += $penalty;
          }
  
          $totalPenalty /= $this->sampleCount;
  
          return [$costs, $gradient, $totalPenalty];
      }
code_tokens: protected function gradient ( array $ theta ) : array { $ costs = [ ] ; $ gradient = [ ] ; $ totalPenalty = 0 ; if ( $ this -> gradientCb === null ) { throw new InvalidOperationException ( 'Gradient callback is not defined' ) ; } foreach ( $ this -> samples as $ index => $ sample ) { $ target = $ this -> targets [ $ index ] ; $ result = ( $ this -> gradientCb ) ( $ theta , $ sample , $ target ) ; [ $ cost , $ grad , $ penalty ] = array_pad ( $ result , 3 , 0 ) ; $ costs [ ] = $ cost ; $ gradient [ ] = $ grad ; $ totalPenalty += $ penalty ; } $ totalPenalty /= $ this -> sampleCount ; return [ $ costs , $ gradient , $ totalPenalty ] ; }
docstring: Calculates gradient, cost function and penalty term for each sample
  then returns them as an array of values
docstring_tokens: Calculates gradient cost function and penalty term for each sample then returns them as an array of values