Ejemplo n.º 1
0
    def on_validation_epoch_end(self, trainer, pl_module):
        x = pl_module.generate(1000)
        y_hat = pl_module.forward(x)

        # Log class balance of predictions #
        # f_fake = pred_label_fraction(y_hat, 0)
        # pl_module.log("generator/f_fake", f_fake, on_step=False, on_epoch=True)

        # Softmax entropy #
        H = entropy(y_hat)
        trainer.logger.experiment.log({"softmax entropy": wandb.Histogram(H)})

        H = H.tolist()
        data = [[h, pl_module.current_epoch] for h in H]
        table = wandb.Table(data=data, columns=["entropy", "epoch"])
        trainer.logger.experiment.log(
            {
                "generator/entropy": wandb.plot.histogram(
                    table, "entropy", title="softmax entropy (fake)"
                )
            }
        )
        trainer.logger.experiment.log({"softmax entropy": wandb.Histogram(H)})

        if config["train"]["fid"]:
            fid_l = pl_module.fid(pl_module.trainer.datamodule.train_l)
            fid_u = pl_module.fid(pl_module.trainer.datamodule.train_u)
            pl_module.log("u/fid", fid_u)
            pl_module.log("l/fid", fid_l)
Ejemplo n.º 2
0
    def compute_marginals(self, entropy_threshold):
        self.marginals = []
        self.entropies = []
        self.most_probable_states = []
        os.system(self.merlin_path + '/merlin -f ' + self.files_folder +
                  self.file + ' -t MAR -a bte -e ' + self.files_folder +
                  self.evidence_file + ' -o ' + self.files_folder + self.file +
                  ' > ' + self.merlin_path + '/merlin_mar')
        with open(self.files_folder + self.file + '.MAR') as fp:
            line = fp.readline()
            cnt = 1
            while line:
                line = fp.readline()
                if cnt == 5:
                    marginals = line.strip().split(' ')
                cnt += 1
        step = 2

        for variable in range(self.size):
            k = self.cardinalities[variable]
            mass_function = [
                1.0 / k for _ in range(self.cardinalities[variable])
            ]
            if variable not in self.observed_variables:
                for state in range(self.cardinalities[variable]):
                    mass_function[state] = float(marginals[step + state])
            step += self.cardinalities[variable] + 1
            self.marginals.append(mass_function)

            self.compute_joint(
                variable=variable,
                folder=self.files_folder,
                file=self.file,
            )

            self.entropies.append(
                entropy(mass_function, self.cardinalities[variable]))
            self.most_probable_states.append(
                mass_function.index(max(mass_function)))

        self.new_observed_variables = [
            i for i, ent in enumerate(self.entropies)
            if ent < entropy_threshold
        ]
        self.new_observed_states = [
            self.most_probable_states[i] for i in self.new_observed_variables
        ]

        return
    def build_tree(self, X, y, feature_indices, depth):
        """ Recursivly builds a decision tree. """

        if depth is self.max_depth or len(y) < self.min_samples_split or entropy(y) is 0:
            return mode(y)[0][0]
        
        feature_index, threshold = find_split(X, y, feature_indices)

        X_true, y_true, X_false, y_false = split(X, y, feature_index, threshold)
        if y_true.shape[0] is 0 or y_false.shape[0] is 0:
            return mode(y)[0][0]
        
        branch_true = self.build_tree(X_true, y_true, feature_indices, depth + 1)
        branch_false = self.build_tree(X_false, y_false, feature_indices, depth + 1)

        return Node(feature_index, threshold, branch_true, branch_false)
Ejemplo n.º 4
0
    def build_tree(self, X, y, feature_indices, depth):
        """ Recursivly builds a decision tree. """

        if depth is self.max_depth or len(
                y) < self.min_samples_split or entropy(y) is 0:
            return mode(y)[0][0]
        #判断是否满足停止条件
        feature_index, threshold = find_split(X, y, feature_indices)
        #寻找最佳分割点
        X_true, y_true, X_false, y_false = split(X, y, feature_index,
                                                 threshold)
        if y_true.shape[0] is 0 or y_false.shape[0] is 0:
            return mode(y)[0][0]

        branch_true = self.build_tree(X_true, y_true, feature_indices,
                                      depth + 1)
        branch_false = self.build_tree(X_false, y_false, feature_indices,
                                       depth + 1)

        return Node(feature_index, threshold, branch_true, branch_false)
Ejemplo n.º 5
0
    def test_entropy(self):
        s = self.df.values[:, 3]
        A = self.df.values[:, 0:3]
        entrpy = entropy(s)
        entrpy_cond = []
        for a in A.T:
            entrpy_cond.append(entropy_cond(s, a))

        expected_entrpy = -3 / 5 * log(3 / 5, 2) - 2 / 5 * log(2 / 5, 2)
        expected_entrpy_cond = []
        expected_entrpy_cond.append(
            3 / 5 *
            (-1 / 3 * log(1 / 3, 2) - 2 / 3 * log(2 / 3, 2)))  #operacio major
        expected_entrpy_cond.append(
            4 / 5 * (-3 / 4 * log(3 / 4, 2) - 1 / 4 * log(1 / 4, 2)))  #familia
        expected_entrpy_cond.append(
            2 / 5 * (-1 / 2 * log(1 / 2, 2) - 1 / 2 * log(1 / 2, 2)) + 3 / 5 *
            (-2 / 3 * log(2 / 3, 2) - 1 / 3 * log(1 / 3, 2)))  # gran

        self.assertTrue(entrpy == expected_entrpy)
        for i in range(3):
            self.assertTrue(entrpy_cond[i] == expected_entrpy_cond[i])
Ejemplo n.º 6
0
  def evaluate(self, batch, discrete=False, **kwargs):

    states = U.to_tensor(batch.state, device=self._device, dtype=torch.float).view(-1, self._input_size[0])

    value_estimates = U.to_tensor(batch.value_estimate, device=self._device, dtype=torch.float)

    advantages = U.to_tensor(batch.advantage, device=self._device, dtype=torch.float)

    discounted_returns = U.to_tensor(batch.discounted_return, device=self._device, dtype=torch.float)

    value_error = (value_estimates - discounted_returns).pow(2).mean()

    advantage = (advantages - advantages.mean()) / (advantages.std() + self._divide_by_zero_safety)

    action_probs = U.to_tensor(batch.action_prob, device=self._device, dtype=torch.float) \
      .view(-1, self._output_size[0])
    _, _, action_probs_target, *_ = self._actor_critic_target(states)

    if discrete:
      actions = U.to_tensor(batch.action, device=self._device, dtype=torch.float) \
        .view(-1, self._output_size[0])
      action_probs = action_probs.gather(1, actions)
      action_probs_target = action_probs_target.gather(1, actions)

    ratio = torch.exp(action_probs - action_probs_target)

    surrogate = ratio * advantage

    clamped_ratio = torch.clamp(ratio, min=1. - self._surrogate_clip, max=1. + self._surrogate_clip)
    surrogate_clipped = clamped_ratio * advantage  # (L^CLIP)

    policy_loss = -torch.min(surrogate, surrogate_clipped).mean()

    entropy_loss = U.entropy(action_probs).mean()

    collective_cost = policy_loss + value_error * self._value_reg_coef + entropy_loss * self._entropy_reg_coef

    return collective_cost, policy_loss, value_error
Ejemplo n.º 7
0
            break

        ###################################################################################
        # sample hit or miss from plume
        # c = plume.sample(pos, t)
        # h = int(c >= th)    
        
        c = plume.sample(pos, t)        #read value from ARX
        h = int(c >= config.th)
        hs.append(h)
        #########################################################


        # update source posterior
        log_p_src = update_log_p_src(pos=pos, xs=xs, ys=ys, h=h, log_p_src=log_p_src)
        s = entropy(log_p_src)

        # pick next move so as to maximally decrease expected entropy
        # moves = get_moves(pos, xs, ys, step=speed*dt)
        moves = get_moves(pos, step=config.step_size)
        delta_s_expecteds = []

        # estimate expected decrease in p_source entropy for each possible move
        for move in moves:
            # set entropy increase to inf if move is out of bounds
            if not round(config.x_bounds[0], 6) <= round(move[0], 6) <= round(config.x_bounds[1], 6):
                delta_s_expecteds.append(np.inf)
                continue
            elif not round(config.y_bounds[0], 6) <= round(move[1], 6) <= round(config.y_bounds[1], 6):
                delta_s_expecteds.append(np.inf)
                continue
Ejemplo n.º 8
0
    def build_tree(self, X, y, feature_indices,fa_feature_index,select_feature_fa, father_node,depth):
        """
        建立决策树
        X :
        y:
        feature_indices:随机选择的特征集合
        fa_feature_index:父节点选择的哪个特征作为分裂特征,、初始时为-1,
        depth :树的深度
        select_feature_fa :记录当前节点的父节点的最优分割属性
        """
        select_feature_fa.append(fa_feature_index)
        n_features = X.shape[1]
        n_features_list = [i for i in range(n_features)]
        #记录选择的特征
        self.select_feature.append(feature_indices)
        self.sample_num.append(len(y))

        node_data_set = np.column_stack((X, y))

        # 树终止条件
        if self.criterion == 'entropy':
            if depth is self.max_depth or len(y) < self.min_samples_split or entropy(y) is 0:
                return mode(y)[0][0]# 返回y数组的众数

        # 树终止条件
        if self.criterion == 'gini':
            temp_gini = gini(y)
            self.gini_.append(temp_gini)
            sample_num = len(y)
            if depth is self.max_depth or sample_num < self.min_samples_split or temp_gini < self.min_impurity_split:
            # if depth is self.max_depth or temp_gini < self.min_impurity_split:

                #所有的特征都已经被选择了,就随机选择一个特征,使得叶子节点构成双特征
                if set(n_features_list) == set(select_feature_fa):
                    index = random.randrange(len(n_features_list))
                    current_feature_index = n_features_list[index]
                    current_max_value = np.max(X[:, current_feature_index])
                    current_min_value = np.min(X[:, current_feature_index])

                else:
                    to_be_select = list(set(n_features_list) - set(select_feature_fa))
                    index = random.randrange(len(to_be_select))

                    current_feature_index = to_be_select[index]
                    current_max_value = np.max(X[:, current_feature_index])
                    current_min_value = np.min(X[:, current_feature_index])

                leaf = Leaf(mode(y)[0][0],fa_feature_index , np.max(X[:,fa_feature_index]),
                            np.min(X[:,fa_feature_index]),current_feature_index,current_max_value,
                            current_min_value,select_feature_fa,node_data_set,sample_num,prior_node= father_node)
                self.leaf_list.append(leaf)
                return leaf

        # feature_index最佳分割属性, threshold 最佳分割属性值,gini_ 系数
        feature_index, threshold, max_value ,min_value ,gini_ = find_split(X, y, self.criterion, feature_indices)

        fa_max_value = np.max(X[:, fa_feature_index])  # 该节点记录父节点分裂特征的最大值
        fa_min_value = np.min(X[:, fa_feature_index])  # 该节点记录父节点分裂特征的最小值

        X_true, y_true, X_false, y_false = split(X, y, feature_index, threshold)# 分成左子树和右子树

        # 没有元素
        if y_true.shape[0] is 0 or y_false.shape[0] is 0:

            if set(n_features_list) == set(select_feature_fa):
                index = random.randrange(len(n_features_list))
                current_feature_index = n_features_list[index]
                current_max_value = np.max(X[:, current_feature_index])
                current_min_value = np.min(X[:, current_feature_index])

            else:
                to_be_select = list(set(n_features_list) - set(select_feature_fa))
                index = random.randrange(len(to_be_select))

                current_feature_index = to_be_select[index]
                current_max_value = np.max(X[:, current_feature_index])
                current_min_value = np.min(X[:, current_feature_index])

            leaf = Leaf(mode(y)[0][0], fa_feature_index, np.max(X[:, fa_feature_index]), np.min(X[:, fa_feature_index]),
                        current_feature_index,current_max_value,current_min_value,select_feature_fa,node_data_set,prior_node= father_node,sample_num= 0)

            self.leaf_list.append(leaf)
            return leaf

        node = Node(feature_index=feature_index,
                    fa_feature_index = fa_feature_index,
                    threshold = threshold, max_value = max_value, min_value = min_value,
                    fa_max_value = fa_max_value, fa_min_value = fa_min_value,
                    gini_coefficient = gini_,
                    node_data_set = node_data_set)


        # # 随机的选特征
        n_features = X.shape[1]
        n_sub_features = int(self.max_features)
        #
        feature_indices = random.sample(range(n_features), n_sub_features)
        select_feature = list()
        select_feature += select_feature_fa  # 记录节点选择的特征
        ## 递归的创建左子树
        node.branch_true = self.build_tree(X_true, y_true, feature_indices,feature_index,
                                           select_feature,node,depth + 1)

        ## 随机的选特征
        feature_indices = random.sample(range(n_features), n_sub_features)
        # 递归的创建右子树
        select_feature = list()
        select_feature += select_feature_fa  # 记录节点选择的特征
        node.branch_false = self.build_tree(X_false, y_false, feature_indices,feature_index,
                                            select_feature,node,depth + 1)

        node.prior_node = father_node #指向前驱节点

        return node
Ejemplo n.º 9
0
    def training_step(self, batch, batch_idx, optimizer_idx):
        x_l, y_l = batch["l"]
        x_u, _ = batch["u"]
        x_real, _ = batch["real"]

        ##### DISCRIMINATOR #####
        if optimizer_idx == 0:
            ## Generate fake ##
            z = torch.rand(x_real.shape[0], n_z, 1, 1)
            z = z.type_as(x_real)
            x_fake = self.G(z).detach()

            ## Pass through discriminator ##
            l_real, _, _ = self.D(x_real)
            l_l, _, _ = self.D(x_l)
            l_fake, _, _ = self.D(x_fake)
            _, y_u, _ = self.D(x_u)

            ## Supervised Loss l_l
            sup_loss = F.cross_entropy(l_l, y_l)
            self.log("disc/supervised loss", sup_loss)

            ## Unsupervised Loss ##
            u_loss = logit_loss(l_real, l_fake)
            self.log("disc/unsupervised loss", u_loss)

            ## Conditional Entropy Loss ##
            ent_loss = config["train"]["ent_weight"] * entropy(y_u, loss=True)
            self.log("disc/conditional entropy loss", ent_loss)

            ## Total Loss ##
            loss = u_loss + sup_loss + ent_loss
            self.log("disc/loss", loss)

            return loss

        ##### GENERATOR #####
        if optimizer_idx == 1:
            ## Generate fake ##
            z = torch.rand(x_u.shape[0], n_z, 1, 1)
            z = z.type_as(x_u)
            x_fake = self.G(z)

            ## Pass through discriminator to get feature maps ##
            _, _, f_real = self.D(x_u)
            _, _, f_fake = self.D(x_fake)

            ## Calculate L2 norm of feature map difference ##
            Df = torch.mean(f_real, 0) - torch.mean(f_fake, 0)
            f_loss = torch.linalg.norm(Df, ord=2)
            self.log("generator/f_loss", f_loss)

            if config["train"]["pt_weight"]:
                pt_loss = pull_away_loss(f_fake)
                self.log("generator/pt_loss", pt_loss)
                loss = f_loss + pt_loss
                self.log("generator/loss", loss)
            else:
                loss = f_loss

            return loss
Ejemplo n.º 10
0
    def training_step(self, batch, batch_idx, optimizer_idx):
        x_l, y_l = batch["l"]
        x_u, _ = batch["u"]
        x_real, _ = batch["real"]

        ##### DISCRIMINATOR #####
        if optimizer_idx == 0:
            ## Generate fakes and real/fake labels ##
            z = torch.rand(x_l.shape[0], n_z, 1, 1).type_as(x_l)
            # y_fake = torch.randint(2, (x_u.shape[0],)).type_as(x_u)
            x_g = self.G(z, y_l).detach()

            ## Generate pseudolabels ##
            y_c = self.C(x_real, predict=True).detach()

            ## Pass through discriminator ##
            p_l = self.D(x_l, labels=y_l)
            p_c = self.D(x_real, labels=y_c)
            p_g = self.D(x_g, labels=y_l)

            loss_l = F.binary_cross_entropy(p_l, ones_labels(y_l))
            self.log("discriminator/loss_l", loss_l)

            loss_c = 0.5 * F.binary_cross_entropy(p_c, zeros_labels(y_c))
            self.log("discriminator/loss_c", loss_c)

            loss_g = 0.5 * F.binary_cross_entropy(p_g, zeros_labels(y_l))
            self.log("discriminator/loss_g", loss_g)

            loss = loss_l + loss_c + loss_g
            self.log("discriminator/loss", loss)

            return loss

        ##### CLASSIFIER #####
        if optimizer_idx == 1:
            ## Generate fakes and pseudo-classify them ##
            z = torch.rand(x_l.shape[0], n_z, 1, 1).type_as(x_l)
            # y_fake = torch.randint(2, (x_u.shape[0],)).type_as(x_u)
            x_g = self.G(z, y_l).detach()
            y_g = self.C(x_g)

            ## Generate pseudolabels and pass through discriminator ##
            y_c = self.C(x_real, predict=True)
            p_c = self.D(x_real, labels=y_c)

            ## Discriminatory loss ##
            disc_loss = F.binary_cross_entropy(p_c, ones_labels(y_c))
            self.log("classifier/discriminatory loss", disc_loss)

            ## Supervised loss using labelled data ##
            y_l_pred = self.C(x_l)
            sup_loss = self.ce_loss(y_l_pred, y_l)
            self.log("classifier/supervised loss", sup_loss)

            ## Pseudo-discriminative loss ##
            pseudo_loss = self.ce_loss(y_g, y_l)
            self.log("classifier/pseudo-discriminative loss", pseudo_loss)

            ## Confidence loss ##
            y_c = self.C(x_real)
            H_y_c = entropy(y_c, loss=True)
            p_y_c = torch.sum(y_c, 0).expand(y_l.shape[0], -1)
            R_U = H_y_c + config["train"]["alpha_B"] * self.ce_loss(p_y_c, y_l)
            self.log("classifier/confidence loss", R_U)

            loss = sup_loss + pseudo_loss + disc_loss + config["train"][
                "alpha_P"] * R_U

            return loss

        ##### GENERATOR #####
        if optimizer_idx == 2:
            ## Generate fakes and 'real' labels ##
            z = torch.rand(x_l.shape[0], n_z, 1, 1).type_as(x_l)
            x_g = self.G(z, y_l)

            ## Pass through discriminator and calculate loss ##
            p_g = self.D(x_g, labels=y_l)
            loss = F.binary_cross_entropy(p_g, ones_labels(y_l))

            self.log("generator/loss", loss)

            return loss