pythonkerascleverhans

'RandomForestClassifier' object has no attribute 'layers'


I'm trying to attack my random forest classifier.

clf = RandomForestClassifier(max_features="sqrt", n_estimators=500, n_jobs=-1, verbose=1, warm_start=True)
clf.fit(X_train, y_train)

After this definition I do my predictions and after that I did the code below:

from keras import backend
from cleverhans.utils_keras import KerasModelWrapper
from cleverhans.attacks import FastGradientMethod
sess =  backend.get_session()

wrap = KerasModelWrapper(clf)
fgsm = FastGradientMethod(wrap, sess=sess)
fgsm_params = {'eps': 0.15,
               'clip_min': 0.,
               'clip_max': 1.}

adv_x = fgsm.generate_np(X_test, **fgsm_params) 
adv_x.shape

And at --> 10 adv_x = fgsm.generate_np(X_test, **fgsm_params) I get an attribute error.

AttributeError: 'RandomForestClassifier' object has no attribute 'layers'

I mean, my classifier does not have layers but how can I do this fgsm attack? Is there a way to add randomforestclassifier to sequential model to have layers? Or is there another way to attack?


Solution

  • You will not be able to run the FGSM attack on a sklearn model because CleverHans would not be able to compute the gradients needed to find the direction in which to perturb the input to find an adversarial example. To compute these gradients, a symbolic definition of the model is needed, which requires the model to be defined using TensorFlow (either directly or through a high-level abstraction like Keras).

    If you'd like to use a sklearn model, you can however wrap it as a CleverHans model and use only gradient-free attacks such as SPSA.