title: How can I make pseudo function and function prime for SciPy fmin_l_bfgs_b?
body: <p>I want to use <code>scipy.optimize.fmin_l_bfgs_b</code> to find the minimum of a cost function.</p>
  
  <p>To do this, I want to create an instance of <code>one_batch</code> (the code of <code>one_batch</code> is given below) in the first place to specify the batch of training examples and those parameters that are not included in the loss function but necessary to calculate the loss.</p>
  
  <p>Because the module <code>loss_calc</code> is designed to return the loss and loss prime at the same time, I'm facing with the problem of separating the loss function and loss function prime for <code>scipy.optimize.fmin_l_bfgs_b</code>. </p>
  
  <p>As you can see from the code of <code>one_batch</code>, given a batch of traning examples, the <code>[loss, dloss/dParameters]</code> will be calculated in parallel for each of the examples. I don't want to do the exact same calculations twice for <code>get_loss</code> and <code>get_loss_prime</code>.</p>
  
  <p>So how can I design the methods <code>get_loss</code> and <code>get_loss_prime</code>, so that I only need to do parallel calculation once? </p>
  
  <p>Here is the code of <code>one_batch</code></p>
  
  <pre><code>from calculator import loss_calc
  
  class one_batch:
  
      def __init__(self, 
                   auxiliary_model_parameters, 
                   batch_example):
  
          # auxiliary_model_parameters are parameters need to specify 
          # the loss calculator but are not included in the loss function.
  
          self.auxiliary_model_parameters = auxiliary_model_parameters 
          self.batch_example = batch_example
  
      def parallel(self, func, args):
          pool = multiprocessing.Pool(multiprocessing.cpu_count())
          result = pool.map(func, args)
          return result 
  
      def one_example(self, example):
          temp_instance = loss_calc(self.auxiliary_model_parameters, 
                                    self.model_vector)
          loss, dloss = temp_instance(example).calculate()
          return [loss, dloss]
  
      def main(self, model_vector):
          self.model_vector = model_vector
  
          # model_vector and auxiliary_model_parameters are necessary 
          # for creating an instance of loss function calculator 
  
          result_list = parallel(self.one_example, 
                                 self.batch_examples)
  
          # result_list is a list of sublists, each sublist is 
          # [loss, dloss/dParameter] for each training example 
  
     def get_loss(self):
         ?
  
     def get_loss_prime(self):
         ?
  </code></pre>