def get_importances(self, target_examples, update=False): print('\t in get importances ...') api = VWAPI() print('\t getting targets ...') #utils.debug_print(str(target_examples)) # assume target_examples are in perserved index order examples = [example['meta']['features'] for example in target_examples] print('\t calling my battle heavy get_bulk_responses...') answers = api.get_bulk_responses(examples) #utils.debug_print(str(answers)) print('\t closing/shutting down api') api.vw.close() # del doesn't seemt to close socket :-/ del api print('\t returning importances, apply np.argsort') importances = [answer.importance for answer in answers] ordered_importances = np.argsort(importances) if update: butler.algorithms.set(key='importances', value=ordered_importances) # ordered by importance on indices into target_examples return ordered_importances
def teach(self, butler, target_index, target_label): api = VWAPI() example = butler.targets.get_target_item(butler.exp_uid, target_index) vw_example = api.to_vw_examples([example]) api.vw.send_example(target_label, features=vw_example) api.vw.close() del api
def getModel(self, butler): precision = random.random() num_reported_answers = butler.experiment.get( key='num_reported_answers') butler.algorithms.set(key='precision', value=precision) ret = butler.algorithms.get(key=['precision', 'num_reported_answers']) #print ret num_reported_answers = ret[ 'num_reported_answers'] # to prove that we can make it non None print('num reported', num_reported_answers, '<') print type(num_reported_answers), type(precision) # Debug area for getting hold out accuracy hold_out = None if butler.experiment.get(key='hold_out'): hold_out = set(butler.experiment.get(key='hold_out')) print('\t ***', hold_out, ' is hold out') if hold_out: # during inital queries it can be null beause nothing was held out print('\t *** len of hold out: ', len(hold_out)) # have length # so here we have a hold out and we need to test with it hold_out_features = [butler.targets.get_target_item(butler.exp_uid, value[0])['meta']['features']\ for value in hold_out] print(len(hold_out_features)) api = VWAPI() answers = api.get_bulk_responses(hold_out_features) api.vw.close() # del doesn't seemt to close socket :-/ del api # Here we compare signs for the predicted answer and the held example # a cheap way to threshold the linear regression to a categorical variable scores = [(answer.prediction < 0) == (held_out_example[1] < 0) for answer, held_out_example in zip(answers, hold_out)] precision = sum(scores) / len(scores) print(scores, precision) # this return is identical to get() #return butler.algorithms.get(key=['precision', 'num_reported_answers']) return { 'precision': precision, 'num_reported_answers': num_reported_answers }
def get_importances(self, butler=None, target_examples=None, update=False): print('\t in get importances ...') api = VWAPI() print('\t getting targets ...') # assume target_examples are in perserved index order examples = [example['meta']['features'] for example in target_examples] answers = api.get_bulk_responses(examples) print('\t closing/shutting down api') api.vw.close() # del doesn't seemt to close socket :-/ del api importances = [answer.importance for answer in answers] ordered_importances = np.argsort(importances) if update: butler.algorithms.set(key='importances', value=ordered_importances) # ordered by importance on indices into target_examples return ordered_importances
def teach(self, butler, args): #print(args) args = json.loads(args) target_label = args['args']['target_label'] example = args['args']['example'] print('\t*** about to teach ...') api = VWAPI() # products are represented as a bag of words (and maybe syntax too) vw_example = api.to_vw_examples([example]) #print("\t*** vw_example: ", str(vw_example)) api.vw.add_namespaces(vw_example) # important to send namespaces, not features, because # to_vw_examples creates namespaces api.vw.send_example(response=target_label) api.vw.close() del api print('\t*** ... taught')