首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >决策树分类器我一直收到NaN错误

决策树分类器我一直收到NaN错误
EN

Stack Overflow用户
提问于 2019-05-27 09:16:52
回答 1查看 1.5K关注 0票数 2

我有一个小的决策树代码,我相信我把所有的东西都转换成int了,我已经用isnan,max等检查了我的训练/测试数据。

我真的不知道为什么它会给出那个错误。

因此,我尝试传递决策树中的Mnist数据集,然后使用类进行攻击。

代码如下:

代码语言:javascript
复制
 from AttackUtils import Attack
    from AttackUtils import calc_output_weighted_weights, targeted_gradient, non_targeted_gradient, non_targeted_sign_gradient
    (X_train_woae, y_train_woae), (X_test_woae, y_test_woae) = mnist.load_data()
    X_train_woae = X_train_woae.reshape((len(X_train_woae), np.prod(X_train_woae.shape[1:])))
    X_test_woae = X_test_woae.reshape((len(X_test_woae), np.prod(X_test_woae.shape[1:])))

    from sklearn import tree
    #model_woae = LogisticRegression(multi_class='multinomial', solver='lbfgs', fit_intercept=False)
    model_woae = tree.DecisionTreeClassifier(class_weight='balanced')
    model_woae.fit(X_train_woae, y_train_woae)
    #model_woae.coef_ = model_woae.feature_importances_
    coef_int = np.round(model_woae.tree_.compute_feature_importances(normalize=False) * X_train_woae.size).astype(int)
    attack_woae = Attack(model_woae)
    attack_woae.prepare(X_train_woae, y_train_woae, X_test_woae, y_test_woae)
    weights_woae = attack_woae.weights
    num_classes_woae = len(np.unique(y_train_woae))
    attack_woae.create_one_hot_targets(y_test_woae)
    attack_woae.attack_to_max_epsilon(non_targeted_gradient, 50)
    non_targeted_scores_woae = attack_woae.scores

因此,攻击类进行扰动和非目标梯度攻击。,这是攻击类:

代码语言:javascript
复制
import numpy as np
from sklearn.metrics import accuracy_score


def calc_output_weighted_weights(output, w):
    for c in range(len(output)):
        if c == 0:
            weighted_weights = output[c] * w[c]
        else:
            weighted_weights += output[c] * w[c]
    return weighted_weights


def targeted_gradient(foolingtarget, output, w):
    ww = calc_output_weighted_weights(output, w)
    for k in range(len(output)):
        if k == 0:
            gradient = foolingtarget[k] * (w[k]-ww)
        else:
            gradient += foolingtarget[k] * (w[k]-ww)
    return gradient


def non_targeted_gradient(target, output, w):
    ww = calc_output_weighted_weights(output, w)
    for k in range(len(target)):
        if k == 0:
            gradient = (1-target[k]) * (w[k]-ww)
        else:
            gradient += (1-target[k]) * (w[k]-ww)
    return gradient


def non_targeted_sign_gradient(target, output, w):
    gradient = non_targeted_gradient(target, output, w)
    return np.sign(gradient)


class Attack:

    def __init__(self, model):
        self.fooling_targets = None
        self.model = model

    def prepare(self, X_train, y_train, X_test, y_test):
        self.images = X_test
        self.true_targets = y_test
        self.num_samples = X_test.shape[0]
        self.train(X_train, y_train)
        print("Model training finished.")
        self.test(X_test, y_test)
        print("Model testing finished. Initial accuracy score: " + str(self.initial_score))

    def set_fooling_targets(self, fooling_targets):
        self.fooling_targets = fooling_targets

    def train(self, X_train, y_train):
        self.model.fit(X_train, y_train)
        self.weights = self.model.coef_
        self.num_classes = self.weights.shape[0]

    def test(self, X_test, y_test):
        self.preds = self.model.predict(X_test)
        self.preds_proba = self.model.predict_proba(X_test)
        self.initial_score = accuracy_score(y_test, self.preds)

    def create_one_hot_targets(self, targets):
        self.one_hot_targets = np.zeros(self.preds_proba.shape)
        for n in range(targets.shape[0]):
            self.one_hot_targets[n, targets[n]] = 1

    def attack(self, attackmethod, epsilon):
        perturbed_images, highest_epsilon = self.perturb_images(epsilon, attackmethod)
        perturbed_preds = self.model.predict(perturbed_images)
        score = accuracy_score(self.true_targets, perturbed_preds)
        return perturbed_images, perturbed_preds, score, highest_epsilon

    def perturb_images(self, epsilon, gradient_method):
        perturbed = np.zeros(self.images.shape)
        max_perturbations = []
        for n in range(self.images.shape[0]):
            perturbation = self.get_perturbation(epsilon, gradient_method, self.one_hot_targets[n], self.preds_proba[n])
            perturbed[n] = self.images[n] + perturbation
            max_perturbations.append(np.max(perturbation))
        highest_epsilon = np.max(np.array(max_perturbations))
        return perturbed, highest_epsilon

    def get_perturbation(self, epsilon, gradient_method, target, pred_proba):
        gradient = gradient_method(target, pred_proba, self.weights)
        inf_norm = np.max(gradient)
        perturbation = epsilon / inf_norm * gradient
        return perturbation

    def attack_to_max_epsilon(self, attackmethod, max_epsilon):
        self.max_epsilon = max_epsilon
        self.scores = []
        self.epsilons = []
        self.perturbed_images_per_epsilon = []
        self.perturbed_outputs_per_epsilon = []
        for epsilon in range(0, self.max_epsilon):
            perturbed_images, perturbed_preds, score, highest_epsilon = self.attack(attackmethod, epsilon)
            self.epsilons.append(highest_epsilon)
            self.scores.append(score)
            self.perturbed_images_per_epsilon.append(perturbed_images)
            self.perturbed_outputs_per_epsilon.append(perturbed_preds)

这是它给出的回溯:

ValueError

4 attack_woae.create_one_hot_targets(y_test_woae) =len( attack_woae.attack_to_max_epsilon(non_targeted_gradient,(Y_train_woae))5 attack_woae.create_one_hot_targets(y_test_woae)->6 attack_woae.attack_to_max_epsilon(non_targeted_gradient,50) 7 non_targeted_scores_woae = attack_woae.scores中的

回溯(最近一次呼叫)

~\MULTIATTACK\AttackUtils.py in attack_to_max_epsilon(self,attackmethod,max_epsilon) 106 self.perturbed_outputs_per_epsilon = [] 107for epsilon in range(0,self.max_epsilon):--> 108 perturbed_images,perturbed_preds,score,highest_epsilon = self.attack(attackmethod,epsilon) 109 self.epsilons.append(highest_epsilon) 110 self.scores.append(分数)

攻击中的~\MULTIATTACK\AttackUtils.py (自身,攻击方法,epsilon) 79 def攻击(自身,攻击方法,epsilon):80 perturbed_images,highest_epsilon = self.perturb_images(epsilon,攻击方法)-> 81 perturbed_preds = self.model.predict(perturbed_images) 82 ~\MULTIATTACK\AttackUtils.py= accuracy_score(self.true_targets,perturbed_preds) 83 return perturbed_images,perturbed_preds,score,highest_epsilon

预测(self,X,check_input) 413“414 check_is_fitted(self,'tree_') --> 415 X= self._validate_X_predict(X,check_input) 416 proba = self.tree_.predict(X) 417 n_samples = X.shape

...\appdata\local\programs\python\python35\lib\site-packages\sklearn\tree\tree.py in _validate_X_predict(self,X,check_input) 374“每当尝试预测,应用,predict_proba”375if check_input:--> 376X= check_array(X,dtype=DTYPE,accept_sparse="csr") 377 if issparse(X) and (X.indices.dtype != np.intc or 378 X.indptr.dtype != np.intc):

...\appdata\local\programs\python\python35\lib\site-packages\sklearn\utils\validation.py in check_array(array,accept_sparse,accept_large_sparse,dtype,order,copy,force_all_finite,ensure_2d,allow_nd,ensure_min_samples,ensure_min_features,warn_on_dtype,estimator) 566if force_all_finite: 567 _assert_all_finite(array,--> 568 allow_nan=force_all_finite == 'allow-nan') 569 570 shape_repr = _shape_repr(array.shape)

...\appdata\local\programs\python\python35\lib\site-packages\sklearn\utils\validation.py in _assert_all_finite(X,allow_nan) 54 not allow_nan and not np.isfinite(X).all():55 type_err = 'infinity‘if allow_nan 'NaN,infinity’-> 56 ...\appdata\local\programs\python\python35\lib\site-packages\sklearn\utils\validation.py ValueError(msg_err.format(type_err,X.dtype)) 57 58

ValueError:输入包含NaN、无穷大或对于dtype('float32')来说太大的值。

编辑:

我已经将系数数字添加为0,现在在attack.attack_to_max_epsilon(non_targeted_gradient, epsilon_number)的直线下方给出了相同的错误

EN

回答 1

Stack Overflow用户

发布于 2019-05-28 02:41:56

尝试在训练之前对标签应用一次热编码。

代码语言:javascript
复制
from sklearn.preprocessing import LabelEncoder

mylabels= ["label1", "label2", "label2"..."n.label"]
le = LabelEncoder()
labels = le.fit_transform(mylabels)

然后尝试拆分您的数据:

代码语言:javascript
复制
from sklearn.model_selection import train_test_split
(x_train, x_test, y_train, y_test) = train_test_split(data,
                                                     labels,
                                                     test_size=0.25)

现在,你的标签可能会用数字编码,这对训练机器学习算法很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56318601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档