Beispiel #1
0
    def _predict_contexts(self, contexts: np.ndarray, is_predict: bool,
                          seeds: Optional[np.ndarray] = None, start_index: Optional[int] = None) -> List:

        # Get local copy of model, arm_to_expectation and arms to minimize
        # communication overhead between arms (processes) using shared objects
        arm_to_model = deepcopy(self.arm_to_model)
        arm_to_expectation = deepcopy(self.arm_to_expectation)
        arms = deepcopy(self.arms)

        # Create an empty list of predictions
        predictions = [None] * len(contexts)
        for index, row in enumerate(contexts):
            # Each row needs a separately seeded rng for reproducibility in parallel
            rng = np.random.RandomState(seed=seeds[index])

            for arm in arms:
                # Copy the row rng to the deep copied model in arm_to_model
                arm_to_model[arm].rng = rng

                # Get the expectation of each arm from its trained model
                arm_to_expectation[arm] = arm_to_model[arm].predict(row)

            if is_predict:
                predictions[index] = argmax(arm_to_expectation)
            else:
                predictions[index] = arm_to_expectation.copy()

        # Return list of predictions
        return predictions
Beispiel #2
0
    def predict(self, contexts: np.ndarray = None) -> Arm:

        # Return a random arm with less than epsilon probability
        if self.rng.rand() < self.epsilon:
            return self.arms[self.rng.randint(0, len(self.arms))]

        # Return the first arm with maximum expectation.
        return argmax(self.arm_to_expectation)
Beispiel #3
0
    def predict(self, contexts: np.ndarray = None):

        # Generate expectations based on epsilon greedy policy
        arm_to_exp = super().predict_expectations()

        # Arm with the highest expectation
        max_arm = argmax(arm_to_exp)
        max_exp = arm_to_exp[max_arm]
        print("Max arm: ", max_arm, " with expectation: ", max_exp)

        # Arm with second highest expectation
        del arm_to_exp[max_arm]
        next_max_arm = argmax(arm_to_exp)
        next_max_exp = arm_to_exp[next_max_arm]
        print("Next max arm: ", next_max_arm, " with expectation: ",
              next_max_exp)

        # Regret between best and the second best decision
        regret = max_exp - next_max_exp
        print("Regret: ", regret, " margin: ", self.margin)

        # Return the arm with maximum expectation
        # if and only if the regret beats the given operational margin
        return max_arm if regret >= self.margin else next_max_arm
Beispiel #4
0
    def _predict_contexts(self, contexts: np.ndarray, is_predict: bool,
                          seeds: Optional[np.ndarray] = None, start_index: Optional[int] = None) -> List:

        # Get local copy of model, arm_to_expectation and arms to minimize
        # communication overhead between arms (processes) using shared objects
        arm_to_model = deepcopy(self.arm_to_model)
        arm_to_expectation = deepcopy(self.arm_to_expectation)
        arms = deepcopy(self.arms)

        # Create an empty list of predictions
        predictions = [None] * len(contexts)
        for index, row in enumerate(contexts):

            for arm in arms:
                # Get the expectation of each arm from its trained model
                arm_to_expectation[arm] = arm_to_model[arm].predict(row)
            if is_predict:
                predictions[index] = argmax(arm_to_expectation)
            else:
                predictions[index] = arm_to_expectation.copy()

        # Return list of predictions
        return predictions
Beispiel #5
0
    def predict(self, contexts: np.ndarray = None) -> Arm:

        # Return the arm with maximum expectation. If multiple max value exists, return the first one
        return argmax(self.predict_expectations())
Beispiel #6
0
    def predict(self, contexts: np.ndarray = None) -> Arm:

        # Return the first arm with maximum expectation
        return argmax(self.arm_to_expectation)