def predsFromModel(_modelName): # Initialise nominal model model = Model(len(test[0]), 1) model.load_model(_modelName) # Make a vector of outputs _comp_preds = [] for (batchX, batchY) in next_batch(test, probs_test, batchSize): if batchY.shape[0] < batchSize: print('Batch size insufficient (%s), continuing...' % batchY.shape[0]) continue output = model.evaluate_total(batchX, debug=False) _comp_preds.extend(output.T) # Make a vector of outputs _comp_preds_bkg = [] for (batchX, batchY) in next_batch(test_bkg, probs_test_bkg, batchSize): if batchY.shape[0] < batchSize: print('Batch size insufficient (%s), continuing...' % batchY.shape[0]) continue output = model.evaluate_total(batchX, debug=False) _comp_preds_bkg.extend(output.T) return _comp_preds, _comp_preds_bkg
def convert_model(self, in_model, base_model, datapoints, scaler=None): # Create the list of outputs for the base model refs = [] flattened = [] for b in datapoints: if scaler: b = scaler.transform([b]) prob = base_model.predict_proba(b)[0][0] b = b[0].flatten().tolist() refs.append(prob) flattened.append(b) for q in range(self._num_epochs): # initialize the total loss for the epoch epochloss = [] # loop over our data in batches for (batchX, batchY) in next_batch(datapoints, refs, self._batchSize): if batchX.shape[0] != self._batchSize: print 'Batch size insufficient (%s), continuing...' % batchY.shape[ 0] continue # Find current output and calculate loss for our graph preds = in_model.evaluate_total(batchX, debug=False) loss, error = dot_loss(preds, batchY) epochloss.append(loss) # Update the model in_model.update(batchX, batchY, self._alpha) avloss = np.average(epochloss) diff = 0.0 if q > 0: # Is the fractional difference less than the threshold diff = math.fabs(avloss - self._losses[-1]) / avloss self._diffs.append(diff) self._losses.append(avloss) update = 0 modify = True if (diff < self._threshold) else False if modify: # If it is less than the threshold, is it below # where we last updated modify = (avloss < (self._updatedLoss - (diff * avloss))) if modify: update = 1 print 'Model conversion not sufficient, updating...' print 'Last updated loss: %s' % self._updatedLoss self._updatedLoss = avloss in_model.expand_layer_dynamic(0) self._updates.append(update) print('Epoch: %s, loss %s, diff %.5f, last updated loss %.5f' % (q, avloss, diff, self._updatedLoss)) # update our loss history list by taking the average loss # across all batches self._losses.append(avloss)
probs_test_bkg = test_probabilitiesBkg # Initialise model model = Model(len(test[0]), 1) model.load_model('approx1.pkl') test = np.array(test) probs_test = np.array(probs_test) test_bkg = np.array(test_bkg) probs_test_bkg = np.array(probs_test_bkg) # Make a vector of outputs comp_preds = [] comp_true = [] for (batchX, batchY) in next_batch(test, probs_test, batchSize): if batchY.shape[0] < batchSize: print('Batch size insufficient (%s), continuing...' % batchY.shape[0]) continue output = model.evaluate_total(batchX, debug=False) comp_preds.extend(output.T) comp_true.extend(batchY) # Make a vector of outputs comp_preds_bkg = [] comp_true_bkg = [] for (batchX, batchY) in next_batch(test_bkg, probs_test_bkg, batchSize): if batchY.shape[0] < batchSize: print('Batch size insufficient (%s), continuing...' % batchY.shape[0])
def convert_model(self, drone_model, base_model, datapoints, scaler=None, keras_conv=False): # Create the list of outputs for the base model refs = [] flattened = [] for b in datapoints: if scaler: b = scaler.transform([b]) prob = 0.0 if keras_conv: prob = base_model.predict_proba( np.expand_dims(np.expand_dims(b, axis=2), axis=0))[0][0] else: prob = base_model.predict_proba(b)[0][0] b = b[0].flatten().tolist() refs.append(prob) flattened.append(b) inflate = 0 # to inflate the learning without change iterations q = 0 avloss = 0 # convert until min epochs are passed and leave only if loss at minima while (q < self._num_epochs) or (self._updatedLoss < avloss): # initialize the total loss for the epoch epochloss = [] # loop over our data in batches for (batchX, batchY) in next_batch(datapoints, refs, self._batchSize): if batchX.shape[0] != self._batchSize: print('Batch size insufficient (%s), continuing...' % batchY.shape[0]) continue # Find current output and calculate loss for our graph preds = drone_model.evaluate_total(batchX, debug=False) loss, error = dot_loss(preds, batchY) epochloss.append(loss) # Update the model drone_model.update(batchX, batchY, self._alpha) avloss = np.average(epochloss) diff = 0.0 if q > 0: # is the relative improvement of the loss too small, smaller than threshold diff = math.fabs(avloss - self._losses[-1]) / avloss self._diffs.append(diff) self._losses.append(avloss) update = 0 modify = True if (diff < self._threshold) else False if modify: # If it is less than the threshold, is it below # where we last updated, has the drone learned enough # # - skip checks if we have never updated before # - do at least 6 learning iterations before attempting new update # - use asymptotic exponential to push model to learn # until its loss is far enough away from previous update, inflate += 1 # iterate inflating modify = True if self._updatedLoss == 1000.0 else ( avloss < (self._updatedLoss - (50.0 * (1.0 - np.exp(-0.04 * inflate)) * diff * avloss)) ) and (inflate > 5) if modify: update = 1 inflate = 0 print('Model conversion not sufficient, updating...') print('Last updated loss: %s' % self._updatedLoss) self._updatedLoss = avloss if self._add_layer_dynamic: drone_model.add_layer_dynamic() else: drone_model.expand_layer_dynamic(0) print('Model structure is now:') drone_model.print_layers() self._updates.append(update) print('Epoch: %s, loss %s, diff %.5f, last updated loss %.5f' % (q, avloss, diff, self._updatedLoss)) # update our loss history list by taking the average loss # across all batches if q == 0: # be consistent at the first epoch self._losses.append(avloss) self._diffs.append( math.fabs(avloss - self._updatedLoss) / avloss) self._updates.append(0) q += 1 return drone_model
def convert_model(self, drone_model, base_model, datapoints, scaler=None, cache_data=True, epoch_reset=False): # Get the list of reference outputs for the base model datapoints_for_drone, refs = self.get_refs(base_model, datapoints, scaler, cache_data) inflate = 0 # to inflate the learning without change iterations if epoch_reset: self._epoch = 0 avloss = 0 # convert until min epochs are passed and leave only if loss at minima while (self._epoch < self._num_epochs) or (self._updatedLoss < avloss): # initialize the total loss for the epoch epochloss = [] # loop over our data in batches for (batchX, batchY) in next_batch(datapoints_for_drone, refs, self._batchSize): batchY = np.array(batchY) if batchX.shape[0] != self._batchSize: print('Batch size insufficient ({}), continuing...'.format( batchY.shape[0])) continue # Find current output and calculate loss for our graph preds = drone_model.evaluate_total(batchX, debug=False) loss, error = dot_loss(preds, batchY) epochloss.append(loss) # Update the model drone_model.update(batchX, batchY, self._learning_rate) avloss = np.average(epochloss) diff = 0.0 if self._epoch > 0: # is the relative improvement of the loss too small, smaller than threshold diff = math.fabs(avloss - self._losses[-1]) / avloss self._diffs.append(diff) self._losses.append(avloss) update = 0 modify = True if (diff < self._threshold) else False if modify: # If it is less than the threshold, is it below # where we last updated, has the drone learned enough # # - skip checks if we have never updated before # - do at least 6 learning iterations before attempting new update # - use asymptotic exponential to push model to learn # until its loss is far enough away from previous update, inflate += 1 # iterate inflating modify = True if self._updatedLoss == 1000.0 else ( avloss < (self._updatedLoss - (50.0 * (1.0 - np.exp(-0.04 * inflate)) * diff * avloss)) ) and (inflate > 5) if modify: update = 1 inflate = 0 print('Model conversion not sufficient, updating...') print('Last updated loss: %s' % self._updatedLoss) self._updatedLoss = avloss if self._add_layer_dynamic: drone_model.add_layer_dynamic() elif self._layer_to_expand is not None: drone_model.expand_layer_dynamic(self._layer_to_expand) else: drone_model.expand_layer_dynamic( self.round_robin(drone_model.num_layers())) print('Model structure is now:') drone_model.print_layers() self._updates.append(update) print('Epoch: %s, loss %s, diff %.5f, last updated loss %.5f' % (self._epoch, avloss, diff, self._updatedLoss)) # update our loss history list by taking the average loss # across all batches if self._epoch == 0: # be consistent at the first epoch self._losses.append(avloss) self._diffs.append( math.fabs(avloss - self._updatedLoss) / avloss) self._updates.append(0) self._epoch += 1 return drone_model