Example #1
0
 def post_discover(self, s, terminal, a, td_error, phi_s):
     """
     returns the number of added features
     """
     # Indices of non-zero elements of vector phi_s
     activeFeatures = phi_s.nonzero()[0]
     discovered = 0
     for g_index, h_index in combinations(activeFeatures, 2):
         discovered += self.inspectPair(g_index, h_index, td_error)
     return discovered
Example #2
0
 def post_discover(self, s, terminal, a, td_error, phi_s):
     """
     returns the number of added features
     """
     # Indices of non-zero elements of vector phi_s
     activeFeatures = phi_s.nonzero()[0]
     discovered = 0
     for g_index, h_index in combinations(activeFeatures, 2):
         discovered += self.inspectPair(g_index, h_index, td_error)
     return discovered
Example #3
0
    def post_discover(self, s, terminal, a, td_error, phi_s, rho=1):
        """
        returns the number of added features
        """
        self.t += 1
        discovered = 0
        plus = self.random_state.rand() >= self.kappa
        # if self.t == 22:
        #    import ipdb; ipdb.set_trace()
        activeFeatures = phi_s.nonzero()[
            0]  # Indices of non-zero elements of vector phi_s
        # print "Active Features:", activeFeatures, phi_s
        if not self.lazy:
            dd = defaultdict(float)
            for g_index, h_index in combinations(activeFeatures, 2):
                # create potential if necessary
                dd[self.get_potential(
                    g_index, h_index).f_set] = phi_s[g_index] * phi_s[h_index]

            for f, potential in self.iFDD_potentials.items():
                potential.update_statistics(rho, td_error, self.lambda_,
                                            self.discount_factor, dd[f],
                                            self.n_rho)
                # print plus, potential.relevance(plus=plus)
                if potential.relevance(plus=plus) >= self.discovery_threshold:
                    self.addFeature(potential)
                    del self.iFDD_potentials[f]
                    discovered += 1
            # print "t", self.t, "td_error", td_error, discovered
            # self.showCache()
            # print [ f.f_set for f in self.sortediFDDFeatures.toList()]
            # self.showPotentials()

            return discovered

        for g_index, h_index in combinations(activeFeatures, 2):
            discovered += self.inspectPair(g_index, h_index, td_error, phi_s,
                                           rho, plus)

        if rho > 0:
            self.w += np.log(rho)
        else:
            # cut e-traces
            self.n_rho += 1
            self.w = 0
            self.t_rho[self.n_rho] = self.t
        # "rescale" to avoid numerical problems
        # if self.t_rho[self.n_rho] + 300 < self.t:
        #    self.y_a[self.n_rho] *= (self.discount_factor * self.lambda_) ** (-300)
        #    self.y_b[self.n_rho] *= (self.discount_factor * self.lambda_) ** (-300)
        #    self.t_rho[self.n_rho] += 300
        if self.lambda_ > 0:
            self.y_a[self.n_rho] += np.exp(
                self.w) * (self.discount_factor * self.lambda_)**(
                    self.t - self.t_rho[self.n_rho]) * np.abs(td_error)
            self.y_b[self.n_rho] += np.exp(
                self.w) * (self.discount_factor * self.lambda_)**(
                    self.t - self.t_rho[self.n_rho]) * td_error
            assert (np.isfinite(self.y_a[self.n_rho]))
            assert (np.isfinite(self.y_b[self.n_rho]))

        # print "t", self.t, "td_error", td_error, discovered
        # self.showCache()
        # print [ f.f_set for f in self.sortediFDDFeatures.toList()]
        # self.showPotentials()
        return discovered
Example #4
0
    def findFinalActiveFeatures(self, intialActiveFeatures):
        """
        Given the active indices of phi_0(s) find the final active indices of phi(s) based on discovered features
        """
        finalActiveFeatures = []
        k = len(intialActiveFeatures)
        initialSet = set(intialActiveFeatures)

        if 2**k <= self.features_num:
            # k can be big which can cause this part to be very slow
            # if k is large then find active features by enumerating on the
            # discovered features.
            if self.use_chirstoph_ordered_features:
                for i in range(k, 0, -1):
                    if len(initialSet) == 0:
                        break
                    # generate list of all combinations with i elements
                    cand_i = [(c, self.iFDD_features[frozenset(c)].index)
                              for c in combinations(initialSet, i)
                              if frozenset(c) in self.iFDD_features]
                    # sort (recent features (big ids) first)
                    cand_i.sort(key=lambda x: x[1], reverse=True)
                    # idx = -1
                    for candidate, ind in cand_i:
                        # the next block is for testing only
                        # cur_idx = self.iFDD_features[frozenset(candidate)].index
                        # if idx > 0:
                        #    assert(idx > cur_idx)
                        # idx = cur_idx

                        if len(initialSet) == 0:
                            # No more initial features to be mapped to extended
                            # ones
                            break

                        # This was missing from ICML 2011 paper algorithm.
                        # Example: [0,1,20], [0,20] is discovered, but if [0]
                        # is checked before [1] it will be added even though it
                        # is already covered by [0,20]
                        if initialSet.issuperset(set(candidate)):
                            feature = self.iFDD_features.get(
                                frozenset(candidate))
                            if feature is not None:
                                finalActiveFeatures.append(feature.index)
                                if self.sparsify:
                                    # print "Sets:", initialSet, feature.f_set
                                    initialSet = initialSet - feature.f_set
                                    # print "Remaining Set:", initialSet
            else:
                for candidate in powerset(initialSet, ascending=0):
                    if len(initialSet) == 0:
                        # No more initial features to be mapped to extended
                        # ones
                        break

                    # This was missing from ICML 2011 paper algorithm. Example:
                    # [0,1,20], [0,20] is discovered, but if [0] is checked
                    # before [1] it will be added even though it is already
                    # covered by [0,20]
                    if initialSet.issuperset(set(candidate)):
                        feature = self.iFDD_features.get(frozenset(candidate))
                        if feature is not None:
                            finalActiveFeatures.append(feature.index)
                            if self.sparsify:
                                # print "Sets:", initialSet, feature.f_set
                                initialSet = initialSet - feature.f_set
                                # print "Remaining Set:", initialSet
        else:
            # print "********** Using Alternative: %d > %d" % (2**k, self.features_num)
            # Loop on all features sorted on their size and then novelty and
            # activate features
            for feature in self.sortediFDDFeatures.toList():
                if len(initialSet) == 0:
                    # No more initial features to be mapped to extended ones
                    break

                if initialSet.issuperset(set(feature.f_set)):
                    finalActiveFeatures.append(feature.index)
                    if self.sparsify:
                        # print "Sets:", initialSet, feature.f_set
                        initialSet = initialSet - feature.f_set
                        # print "Remaining Set:", initialSet

        if self.useCache:
            self.cache[frozenset(intialActiveFeatures)] = finalActiveFeatures
        return finalActiveFeatures
Example #5
0
    def post_discover(self, s, terminal, a, td_error, phi_s, rho=1):
        """
        returns the number of added features
        """
        self.t += 1
        discovered = 0
        plus = np.random.rand() >= self.kappa
        # if self.t == 22:
        #    import ipdb; ipdb.set_trace()
        activeFeatures = phi_s.nonzero()[
                                       0]  # Indices of non-zero elements of vector phi_s
        #print "Active Features:", activeFeatures, phi_s
        if not self.lazy:
            dd = defaultdict(float)
            for g_index, h_index in combinations(activeFeatures, 2):
                # create potential if necessary
                dd[self.get_potential(g_index, h_index).f_set] = phi_s[g_index] * phi_s[h_index]

            for f, potential in self.iFDD_potentials.items():
                potential.update_statistics(
                    rho, td_error, self.lambda_, self.discount_factor, dd[f], self.n_rho)
                # print plus, potential.relevance(plus=plus)
                if potential.relevance(plus=plus) >= self.discovery_threshold:
                    self.addFeature(potential)
                    del self.iFDD_potentials[f]
                    discovered += 1
            #print "t", self.t, "td_error", td_error, discovered
            #self.showCache()
            #print [ f.f_set for f in self.sortediFDDFeatures.toList()]
            #self.showPotentials()

            return discovered

        for g_index, h_index in combinations(activeFeatures, 2):
            discovered += self.inspectPair(g_index, h_index, td_error, phi_s, rho, plus)

        if rho > 0:
            self.w += np.log(rho)
        else:
            # cut e-traces
            self.n_rho += 1
            self.w = 0
            self.t_rho[self.n_rho] = self.t
        # "rescale" to avoid numerical problems
        # if self.t_rho[self.n_rho] + 300 < self.t:
        #    self.y_a[self.n_rho] *= (self.discount_factor * self.lambda_) ** (-300)
        #    self.y_b[self.n_rho] *= (self.discount_factor * self.lambda_) ** (-300)
        #    self.t_rho[self.n_rho] += 300
        if self.lambda_ > 0:
            self.y_a[self.n_rho] += np.exp(self.w) * (self.discount_factor * self.lambda_) ** (
                self.t - self.t_rho[self.n_rho]) * np.abs(td_error)
            self.y_b[self.n_rho] += np.exp(self.w) * (self.discount_factor * self.lambda_) ** (
                self.t - self.t_rho[self.n_rho]) * td_error
            assert(np.isfinite(self.y_a[self.n_rho]))
            assert(np.isfinite(self.y_b[self.n_rho]))

        #print "t", self.t, "td_error", td_error, discovered
        #self.showCache()
        #print [ f.f_set for f in self.sortediFDDFeatures.toList()]
        #self.showPotentials()
        return discovered
Example #6
0
    def findFinalActiveFeatures(self, intialActiveFeatures):
        """
        Given the active indices of phi_0(s) find the final active indices of phi(s) based on discovered features
        """
        finalActiveFeatures = []
        k = len(intialActiveFeatures)
        initialSet = set(intialActiveFeatures)

        if 2 ** k <= self.features_num:
            # k can be big which can cause this part to be very slow
            # if k is large then find active features by enumerating on the
            # discovered features.
            if self.use_chirstoph_ordered_features:
                for i in range(k, 0, -1):
                    if len(initialSet) == 0:
                        break
                    # generate list of all combinations with i elements
                    cand_i = [(c, self.iFDD_features[frozenset(c)].index)
                              for c in combinations(initialSet, i)
                              if frozenset(c) in self.iFDD_features]
                    # sort (recent features (big ids) first)
                    cand_i.sort(key=lambda x: x[1], reverse=True)
                    #idx = -1
                    for candidate, ind in cand_i:
                        # the next block is for testing only
                        #cur_idx = self.iFDD_features[frozenset(candidate)].index
                        # if idx > 0:
                        #    assert(idx > cur_idx)
                        #idx = cur_idx

                        if len(initialSet) == 0:
                            # No more initial features to be mapped to extended
                            # ones
                            break

                        # This was missing from ICML 2011 paper algorithm.
                        # Example: [0,1,20], [0,20] is discovered, but if [0]
                        # is checked before [1] it will be added even though it
                        # is already covered by [0,20]
                        if initialSet.issuperset(set(candidate)):
                            feature = self.iFDD_features.get(
                                frozenset(candidate))
                            if feature is not None:
                                finalActiveFeatures.append(feature.index)
                                if self.sparsify:
                                    # print "Sets:", initialSet, feature.f_set
                                    initialSet = initialSet - feature.f_set
                                    # print "Remaining Set:", initialSet
            else:
                for candidate in powerset(initialSet, ascending=0):
                    if len(initialSet) == 0:
                        # No more initial features to be mapped to extended
                        # ones
                        break

                    # This was missing from ICML 2011 paper algorithm. Example:
                    # [0,1,20], [0,20] is discovered, but if [0] is checked
                    # before [1] it will be added even though it is already
                    # covered by [0,20]
                    if initialSet.issuperset(set(candidate)):
                        feature = self.iFDD_features.get(frozenset(candidate))
                        if feature is not None:
                            finalActiveFeatures.append(feature.index)
                            if self.sparsify:
                                # print "Sets:", initialSet, feature.f_set
                                initialSet = initialSet - feature.f_set
                                # print "Remaining Set:", initialSet
        else:
            # print "********** Using Alternative: %d > %d" % (2**k, self.features_num)
            # Loop on all features sorted on their size and then novelty and
            # activate features
            for feature in self.sortediFDDFeatures.toList():
                if len(initialSet) == 0:
                    # No more initial features to be mapped to extended ones
                    break

                if initialSet.issuperset(set(feature.f_set)):
                    finalActiveFeatures.append(feature.index)
                    if self.sparsify:
                        # print "Sets:", initialSet, feature.f_set
                        initialSet = initialSet - feature.f_set
                        # print "Remaining Set:", initialSet

        if self.useCache:
            self.cache[frozenset(intialActiveFeatures)] = finalActiveFeatures
        return finalActiveFeatures