def testSplitArgStr(self): split = utils.split_arg_str("--a 10 --b foo --c \"--d bar --e 42\"") self.assertEqual(split, ["--a", "10", "--b", "foo", "--c", "--d bar --e 42"], "wrong split (1): %s" % ", ".join(split)) split = utils.split_arg_str("\"--a\" 10 --b foo --c --d bar --e 42") self.assertEqual(split, ["--a", "10", "--b", "foo", "--c", "--d", "bar", "--e", "42"], "wrong split (2): %s" % ", ".join(split)) split = utils.split_arg_str("\"--a\"\" 10\"--b foo --c --d bar --e 42") self.assertEqual(split, ["--a", " 10", "--b", "foo", "--c", "--d", "bar", "--e", "42"], "wrong split (2): %s" % ", ".join(split))
def testSplitArgStr(self): split = utils.split_arg_str("--a 10 --b foo --c \"--d bar --e 42\"") self.assertEqual(split, ["--a", "10", "--b", "foo", "--c", "--d bar --e 42"], "wrong split (1): %s" % ", ".join(split)) split = utils.split_arg_str("\"--a\" 10 --b foo --c --d bar --e 42") self.assertEqual( split, ["--a", "10", "--b", "foo", "--c", "--d", "bar", "--e", "42"], "wrong split (2): %s" % ", ".join(split)) split = utils.split_arg_str("\"--a\"\" 10\"--b foo --c --d bar --e 42") self.assertEqual( split, ["--a", " 10", "--b", "foo", "--c", "--d", "bar", "--e", "42"], "wrong split (2): %s" % ", ".join(split))
def __init__(self, arg_str): ProbabilisticInterleave.__init__(self, arg_str) # parse arguments parser = argparse.ArgumentParser( description="Initialize probabilistic" " interleave with history.", prog="ProbabilisticInterleaveWithHistory", ) parser.add_argument( "-l", "--history_length", type=int, required=True, help="Number of historical data points to keep in memory and use " "to infer feedback.", ) parser.add_argument( "-b", "--biased", default=False, help="Set to true if comparison should be biased (i.e., not use" "importance sampling).", ) if not arg_str: raise (Exception("Comparison arguments missing. " + parser.format_usage())) args = vars(parser.parse_args(split_arg_str(arg_str))) self.history_length = args["history_length"] self.biased = string_to_boolean(args["biased"]) logging.info("Initialized historical data usage to: %r" % self.biased) # initialize history self.history = []
def __init__(self, arg_str): self.biased = False if arg_str.startswith("-"): parser = argparse.ArgumentParser(description="Parse arguments for " "interleaving method.", prog=self.__class__.__name__) parser.add_argument("-k", "--exploration_rate", type="_exploration_rate", required=True) parser.add_argument("-b", "--biased") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) if args["biased"] == "False" or args["biased"] == 0: self.biased = False else: self.biased = True self.k = args["exploration_rate"] else: try: self.k = float(arg_str) except Exception as ex: raise Exception( "arg_str should be parseable by argparse, or " "contain a single float value (the exploration rate k); " "could not parse arg_str:", ex)
def __init__(self, arg_str=None): self.pi = ProbabilisticInterleave(arg_str) if arg_str: parser = argparse.ArgumentParser(description="Parse arguments for " "interleaving method.", prog=self.__class__.__name__) parser.add_argument("-a", "--aggregate", choices=[ "expectation", "log-likelihood-ratio", "likelihood-ratio", "log-ratio", "binary" ], default="expectation") parser.add_argument("-e", "--exploration_rate", type=float, required=True, help="Exploration rate, 0.5 = perfect " "exploration, 0.0 = perfect exploitation.") parser.add_argument("-b", "--biased", default="False") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) self.exploration_rate = args["exploration_rate"] self.aggregate = args["aggregate"] self.biased = string_to_boolean(args["biased"]) else: raise ValueError("Configuration arguments required. Please provide" " at least a value for the exploration rate.")
def __init__(self, feature_count, arg_str): self.feature_count = feature_count # parse arguments parser = argparse.ArgumentParser(description="Initialize retrieval " "system with the specified feedback and learning mechanism.", prog="PairwiseLearningSystem") parser.add_argument("-w", "--init_weights", help="Initialization " "method for weights (random, zero, fixed).", required=True) parser.add_argument("-e", "--epsilon", required=True, type=float) parser.add_argument("-f", "--eta", required=True, type=float) parser.add_argument("-l", "--lamb", type=float, default=0.0) parser.add_argument("-r", "--ranker", required=True) parser.add_argument("-s", "--ranker_args", nargs="*") parser.add_argument("-t", "--ranker_tie", default="random") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) # initialize weights, comparison method, and learner w = self.initialize_weights(args["init_weights"], self.feature_count) self.ranker_class = get_class(args["ranker"]) if "ranker_args" in args and args["ranker_args"] != None: self.ranker_args = " ".join(args["ranker_args"]) self.ranker_args = self.ranker_args.strip("\"") else: self.ranker_args = None self.ranker_tie = args["ranker_tie"] self.ranker = self.ranker_class(self.ranker_args, w, self.ranker_tie) self.epsilon = args["epsilon"] self.eta = args["eta"] self.lamb = args["lamb"]
def __init__(self, feature_count, arg_str): """ @param featur_count: the number of features @param arg_str: "-h HISTORY_LENGTH -e NUM_CANDIDATES \ -s SELECT_CANDIDATE". """ ListwiseLearningSystem.__init__(self, feature_count, arg_str) parser = argparse.ArgumentParser(prog=self.__class__.__name__) parser.add_argument("-e", "--num_candidates", required=True, type=int, help="Number of candidate rankers to explore in each round.") parser.add_argument("-l", "--history_length", required=True, type=int, help="Number of historic data points to take into account when " "pre-selecting candidates.") parser.add_argument("-s", "--select_candidate", required=True, help="Method for selecting a candidate ranker from a ranker pool." " Options: select_candidate_random, select_candidate_simple," " select_candidate_repeated, or own implementation.") parser.add_argument("-b", "--biased", default="False", help="Set to true if comparison should be biased (i.e., not use" "importance sampling).") parser.add_argument("-r", "--num_repetitions", type=int, default=1, help="The number of repetitions for each ranker pair evaluation" "(when the selection method is select_candidate_repeated).") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) self.num_candidates = args["num_candidates"] self.select_candidate = getattr(self, args["select_candidate"]) self.history_length = args["history_length"] self.biased = string_to_boolean(args["biased"]) logging.info("Initialized historical data usage to: %r" % self.biased) self.num_repetitions = args["num_repetitions"] self.history = []
def __init__(self, arg_str=None): if arg_str: parser = argparse.ArgumentParser(description="Parse arguments for " "interleaving method.", prog=self.__class__.__name__) parser.add_argument("-a", "--aggregate", choices=["expectation", "log-likelihood-ratio", "likelihood-ratio", "log-ratio", "binary"]) parser.add_argument("-d", "--det_interleave", type=bool, help="If true, use deterministic interleaving, regardless " "of the ranker type used for comparison.") parser.add_argument("-t", "--compare_td", type=bool, help="If true, compare rankers using observed assignments " "instead of marginalizing over possible assignments.") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) if "aggregate" in args and args["aggregate"]: self.aggregate = args["aggregate"] if "det_interleave" in args and args["det_interleave"]: self.det_interleave = True if "compare_td" in args and args["compare_td"]: self.compare_td = True if not hasattr(self, "aggregate") or not self.aggregate: self.aggregate = "expectation" if not hasattr(self, "det_interleave"): self.det_interleave = False if not hasattr(self, "compare_td"): self.compare_td = False
def __init__(self, feature_count, arg_str): self.feature_count = feature_count # parse arguments parser = argparse.ArgumentParser( description="Initialize retrieval " "system with the specified feedback and learning mechanism.", prog="PairwiseLearningSystem") parser.add_argument("-w", "--init_weights", help="Initialization " "method for weights (random, zero, fixed).", required=True) parser.add_argument("-e", "--epsilon", required=True, type=float) parser.add_argument("-f", "--eta", required=True, type=float) parser.add_argument("-l", "--lamb", type=float, default=0.0) parser.add_argument("-r", "--ranker", required=True) parser.add_argument("-s", "--ranker_args", nargs="*") parser.add_argument("-t", "--ranker_tie", default="random") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) # initialize weights, comparison method, and learner w = self.initialize_weights(args["init_weights"], self.feature_count) self.ranker_class = get_class(args["ranker"]) if "ranker_args" in args and args["ranker_args"] != None: self.ranker_args = " ".join(args["ranker_args"]) self.ranker_args = self.ranker_args.strip("\"") else: self.ranker_args = None self.ranker_tie = args["ranker_tie"] self.ranker = self.ranker_class(self.ranker_args, w, self.ranker_tie) self.epsilon = args["epsilon"] self.eta = args["eta"] self.lamb = args["lamb"]
def __init__(self, arg_str): ProbabilisticInterleave.__init__(self, arg_str) # parse arguments parser = argparse.ArgumentParser( description="Initialize probabilistic" " interleave with history.", prog="ProbabilisticInterleaveWithHistory") parser.add_argument( "-l", "--history_length", type=int, required=True, help="Number of historical data points to keep in memory and use " "to infer feedback.") parser.add_argument( "-b", "--biased", default=False, help="Set to true if comparison should be biased (i.e., not use" "importance sampling).") if not arg_str: raise (Exception("Comparison arguments missing. " + parser.format_usage())) args = vars(parser.parse_args(split_arg_str(arg_str))) self.history_length = args["history_length"] self.biased = string_to_boolean(args["biased"]) logging.info("Initialized historical data usage to: %r" % self.biased) # initialize history self.history = []
def __init__(self, feature_count, d, arg_str): # parse arguments parser = argparse.ArgumentParser( description="Initialize retrieval " "system with the specified feedback and learning mechanism.", prog="ListwiseLearningSystem") parser.add_argument("-w", "--init_weights", help="Initialization " "method for weights (random, zero).", required=True) parser.add_argument("--sample_weights", default="sample_unit_sphere") parser.add_argument("-c", "--comparison", required=True) parser.add_argument("-f", "--comparison_args", nargs="*") parser.add_argument("-r", "--ranker", required=True) parser.add_argument("-s", "--ranker_args", nargs="*") parser.add_argument("-t", "--ranker_tie", default="random") parser.add_argument("-d", "--delta", required=True, type=str) parser.add_argument("-a", "--alpha", required=True, type=str) parser.add_argument("--anneal", type=int, default=0) parser.add_argument("--normalize", default="False") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) self.ranker_class = get_class(args["ranker"]) self.ranker_args = args["ranker_args"] self.ranker_tie = args["ranker_tie"] self.sample_weights = args["sample_weights"] self.init_weights = args["init_weights"] self.feature_count = feature_count self.ranker = self.ranker_class(d, self.ranker_args, self.ranker_tie, self.feature_count, sample=self.sample_weights, init=self.init_weights) if "," in args["delta"]: self.delta = array([float(x) for x in args["delta"].split(",")]) else: self.delta = float(args["delta"]) if "," in args["alpha"]: self.alpha = array([float(x) for x in args["alpha"].split(",")]) else: self.alpha = float(args["alpha"]) self.anneal = args["anneal"] self.comparison_class = get_class(args["comparison"]) if "comparison_args" in args and args["comparison_args"] != None: self.comparison_args = " ".join(args["comparison_args"]) self.comparison_args = self.comparison_args.strip("\"") else: self.comparison_args = None self.comparison = self.comparison_class(self.comparison_args) self.query_count = 0
def __init__(self, arg_str="random"): if arg_str.startswith("--"): parser = argparse.ArgumentParser(description="Parse arguments for " "interleaving method.", prog=self.__class__.__name__) parser.add_argument("-s", "--startinglist") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) if "startinglist" in args: self.startinglist = args["startinglist"] else: self.startinglist = "random" else: self.startinglist = arg_str
def __init__(self, arg_str=None): self.verbose = False self.credit = getattr(self, "linear_credit") if not arg_str is None: parser = argparse.ArgumentParser(description=self.__doc__, prog=self.__class__.__name__) parser.add_argument("-c", "--credit", choices=["linear_credit", "binary_credit", "inverse_credit"], required=True) args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) self.credit = getattr(self, args["credit"])
def __init__(self, feature_count, arg_str): """ @param featur_count: the number of features @param arg_str: "-h HISTORY_LENGTH -e NUM_CANDIDATES \ -s SELECT_CANDIDATE". """ ListwiseLearningSystem.__init__(self, feature_count, arg_str) parser = argparse.ArgumentParser(prog=self.__class__.__name__) parser.add_argument( "-e", "--num_candidates", required=True, type=int, help="Number of candidate rankers to explore in each round.") parser.add_argument( "-l", "--history_length", required=True, type=int, help="Number of historic data points to take into account when " "pre-selecting candidates.") parser.add_argument( "-s", "--select_candidate", required=True, help="Method for selecting a candidate ranker from a ranker pool." " Options: select_candidate_random, select_candidate_simple," " select_candidate_repeated, or own implementation.") parser.add_argument( "-b", "--biased", default="False", help="Set to true if comparison should be biased (i.e., not use" "importance sampling).") parser.add_argument( "-r", "--num_repetitions", type=int, default=1, help="The number of repetitions for each ranker pair evaluation" "(when the selection method is select_candidate_repeated).") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) self.num_candidates = args["num_candidates"] self.select_candidate = getattr(self, args["select_candidate"]) self.history_length = args["history_length"] self.biased = string_to_boolean(args["biased"]) logging.info("Initialized historical data usage to: %r" % self.biased) self.num_repetitions = args["num_repetitions"] self.history = []
def __init__(self, feature_count, d, arg_str): # parse arguments parser = argparse.ArgumentParser(description="Initialize retrieval " "system with the specified feedback and learning mechanism.", prog="ListwiseLearningSystem") parser.add_argument("-w", "--init_weights", help="Initialization " "method for weights (random, zero).", required=True) parser.add_argument("--sample_weights", default="sample_unit_sphere") parser.add_argument("-c", "--comparison", required=True) parser.add_argument("-f", "--comparison_args", nargs="*") parser.add_argument("-r", "--ranker", required=True) parser.add_argument("-s", "--ranker_args", nargs="*") parser.add_argument("-t", "--ranker_tie", default="random") parser.add_argument("-d", "--delta", required=True, type=str) parser.add_argument("-a", "--alpha", required=True, type=str) parser.add_argument("--anneal", type=int, default=0) parser.add_argument("--normalize", default="False") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) self.ranker_class = get_class(args["ranker"]) self.ranker_args = args["ranker_args"] self.ranker_tie = args["ranker_tie"] self.sample_weights = args["sample_weights"] self.init_weights = args["init_weights"] self.feature_count = feature_count self.ranker = self.ranker_class(d,self.ranker_args, self.ranker_tie, self.feature_count, sample=self.sample_weights, init=self.init_weights) if "," in args["delta"]: self.delta = array([float(x) for x in args["delta"].split(",")]) else: self.delta = float(args["delta"]) if "," in args["alpha"]: self.alpha = array([float(x) for x in args["alpha"].split(",")]) else: self.alpha = float(args["alpha"]) self.anneal = args["anneal"] self.comparison_class = get_class(args["comparison"]) if "comparison_args" in args and args["comparison_args"] != None: self.comparison_args = " ".join(args["comparison_args"]) self.comparison_args = self.comparison_args.strip("\"") else: self.comparison_args = None self.comparison = self.comparison_class(self.comparison_args) self.query_count = 0
def __init__(self, arg_str=""): OptimizedInterleave.__init__(self, arg_str) parser = argparse.ArgumentParser(description=self.__doc__, prog=self.__class__.__name__) parser.add_argument("-c", "--credit", choices=["inverse_credit", "negative_credit"], default="inverse_credit") parser.add_argument("--bias", choices=["per_k_bias", "position_bias"], default="per_k_bias") parser.add_argument("--sensitivity", choices=["Floor", "Shimon"], default="Floor") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) self.credit = getattr(self, args["credit"]) self.bias = args["bias"] self.sensitivity = args["sensitivity"]
def __init__(self, arg_str="random"): if arg_str.startswith("--"): parser = argparse.ArgumentParser(description="Parse arguments for " "interleaving method.", prog=self.__class__.__name__) parser.add_argument("-s", "--startinglist", default="random") parser.add_argument("-c", "--constraints", type=int, default=3, help="Specify which constraint types should be considered. Pos" "sible values: 1 - only infer constraints between clicked and " "previous non-clicked documents; 2 - in addition, infer const" "raints with the document immediately following a clicked one," " if it was not clicked; 3: in addition infer constraints \w t" "he next document that was not clicked.") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) self.startinglist = args["startinglist"] self.constraints = args["constraints"] else: self.startinglist = arg_str self.constraints = 3
def __init__(self, arg_str=None): self.pi = ProbabilisticInterleave(arg_str) self.biased = False self.marginalize = True if arg_str: parser = argparse.ArgumentParser(description="Parse arguments for " "interleaving method.", prog=self.__class__.__name__) parser.add_argument("-b", "--biased") parser.add_argument("-m", "--marginalize") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) if args["biased"] == "False" or args["biased"] == None \ or args["biased"] == 0: self.biased = False else: self.biased = True if args["marginalize"] == "False" or args["marginalize"] == 0: self.marginalize = False else: self.marginalize = True
def __init__(self, arg_str): parser = argparse.ArgumentParser(description="Initialize a cascade " "user model with click and stop probabilities.", prog="CascadeUserModel") parser.add_argument("-c", "--p_click", nargs="+") parser.add_argument("-s", "--p_stop", nargs="+") args = vars(parser.parse_args(split_arg_str(arg_str))) # allow arbitrary hash maps to map relevance labels to click and stop # probabilities p_click_str = "".join(args["p_click"]).strip("\"") self.p_click = {} for entry in re.split("\s*,\s*", p_click_str): (key, value) = re.split("\s*:\s*", entry) self.p_click[int(key)] = float(value) self.p_stop = {} p_stop_str = "".join(args["p_stop"]).strip("\"") for entry in re.split("\s*,\s*", p_stop_str): (key, value) = re.split("\s*:\s*", entry) self.p_stop[int(key)] = float(value)
def __init__(self, arg_str): parser = argparse.ArgumentParser( description="Initialize a cascade " "user model with click and stop probabilities.", prog="CascadeUserModel") parser.add_argument("-c", "--p_click", nargs="+") parser.add_argument("-s", "--p_stop", nargs="+") args = vars(parser.parse_args(split_arg_str(arg_str))) # allow arbitrary hash maps to map relevance labels to click and stop # probabilities p_click_str = "".join(args["p_click"]).strip("\"") self.p_click = {} for entry in re.split("\s*,\s*", p_click_str): (key, value) = re.split("\s*:\s*", entry) self.p_click[int(key)] = float(value) self.p_stop = {} p_stop_str = "".join(args["p_stop"]).strip("\"") for entry in re.split("\s*,\s*", p_stop_str): (key, value) = re.split("\s*:\s*", entry) self.p_stop[int(key)] = float(value)
def __init__(self, arg_str): self.biased = False if arg_str.startswith("-"): parser = argparse.ArgumentParser(description="Parse arguments for " "interleaving method.", prog=self.__class__.__name__) parser.add_argument("-k", "--exploration_rate", type="_exploration_rate", required=True) parser.add_argument("-b", "--biased") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) if args["biased"] == "False" or args["biased"] == 0: self.biased = False else: self.biased = True self.k = args["exploration_rate"] else: try: self.k = float(arg_str) except Exception as ex: raise Exception("arg_str should be parseable by argparse, or " "contain a single float value (the exploration rate k); " "could not parse arg_str:", ex)
def __init__(self, arg_str=None): self.pi = ProbabilisticInterleave(arg_str) if arg_str: parser = argparse.ArgumentParser(description="Parse arguments for " "interleaving method.", prog=self.__class__.__name__) parser.add_argument("-a", "--aggregate", choices=["expectation", "log-likelihood-ratio", "likelihood-ratio", "log-ratio", "binary"], default="expectation") parser.add_argument("-e", "--exploration_rate", type=float, required=True, help="Exploration rate, 0.5 = perfect " "exploration, 0.0 = perfect exploitation.") parser.add_argument("-b", "--biased", default="False") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) self.exploration_rate = args["exploration_rate"] self.aggregate = args["aggregate"] self.biased = string_to_boolean(args["biased"]) else: raise ValueError("Configuration arguments required. Please provide" " at least a value for the exploration rate.")
def __init__(self, arg_str="random"): if arg_str.startswith("--"): parser = argparse.ArgumentParser(description="Parse arguments for " "interleaving method.", prog=self.__class__.__name__) parser.add_argument("-s", "--startinglist", default="random") parser.add_argument( "-c", "--constraints", type=int, default=3, help="Specify which constraint types should be considered. Pos" "sible values: 1 - only infer constraints between clicked and " "previous non-clicked documents; 2 - in addition, infer const" "raints with the document immediately following a clicked one," " if it was not clicked; 3: in addition infer constraints \w t" "he next document that was not clicked.") args = vars(parser.parse_known_args(split_arg_str(arg_str))[0]) self.startinglist = args["startinglist"] self.constraints = args["constraints"] else: self.startinglist = arg_str self.constraints = 3