bethgelab / foolbox

A Python toolbox to create adversarial examples that fool neural networks in PyTorch, TensorFlow, and JAX
https://foolbox.jonasrauber.de
MIT License
2.78k stars 426 forks source link

"nll_loss_forward_no_reduce_cuda_kernel_index" not implemented for 'Float' #701

Closed Rivendellad closed 1 year ago

Rivendellad commented 2 years ago

I've had a similar issue in close, but after I use the latest version, I still get an error like this "nll_loss_forward_no_reduce_cuda_kernel_index" not implemented for 'Float' Please help me

zimmerrol commented 2 years ago

Please post a minimal working example that reproduces your problem.

Rivendellad commented 2 years ago

X_train=torch.from_numpy(X_train) X_train=X_train.reshape([7699,2,128]).unsqueeze(dim=1) X_test=torch.from_numpy(X_test) X_test=X_test.reshape([3301,2,128]).unsqueeze(dim=1)

x_train, x_test = torch.Tensor(X_train), torch.Tensor(X_test) y_train, y_test = torch.Tensor(Y_train), torch.Tensor(Y_test) x_train=x_train[0:1000,:] y_train=y_train[:1000] x_train=x_train.to(device) y_train=y_train.to(device) print(y_train.dtype) print(x_train.shape) print(x_test.shape) print(y_train.shape) print(y_test.shape) train_dataset = TensorDataset(x_train, y_train.type(torch.LongTensor)) test_dataset = TensorDataset(x_test, y_test.type(torch.LongTensor)) batch_size = 128 TrainLoader = DataLoader(train_dataset, batch_size=batch_size, shuffle=False) TestLoader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)

model = torch.load('VTCNN2_9c.pth').eval() # fmodel = PyTorchModel(model,bounds=(-1,1)) attack = LinfFastGradientAttack() clean_acc = accuracy(fmodel, x_train, y_train,) print(f"clean accuracy: {clean_acc * 100:.1f} %")

images, labels = fb.utils.samples(fmodel, dataset=TrainLoader, batchsize=16)

raw_advs, clipped_advs, success = attack(fmodel, x_train, y_train, epsilons=0.3)

torch.float32 torch.Size([1000, 1, 2, 128]) torch.Size([3301, 1, 2, 128]) torch.Size([1000]) torch.Size([3301])

    Layer (type)               Output Shape         Param #

================================================================ Conv2d-1 [-1, 256, 2, 128] 2,048 BatchNorm2d-2 [-1, 256, 2, 128] 512 Dropout-3 [-1, 256, 2, 128] 0 Conv2d-4 [-1, 128, 2, 128] 229,504 Dropout-5 [-1, 128, 2, 128] 0 BatchNorm2d-6 [-1, 128, 2, 128] 256 Conv2d-7 [-1, 80, 1, 128] 143,440 Dropout-8 [-1, 80, 1, 128] 0 BatchNorm2d-9 [-1, 80, 1, 128] 160 Linear-10 [-1, 256] 2,621,696 Linear-11 [-1, 128] 32,896 Dropout-12 [-1, 128] 0 Linear-13 [-1, 64] 8,256 Linear-14 [-1, 11] 715

Total params: 3,039,483 Trainable params: 3,039,483 Non-trainable params: 0

Input size (MB): 0.00 Forward/backward pass size (MB): 2.49 Params size (MB): 11.59 Estimated Total Size (MB): 14.08

clean accuracy: 73.1 % Traceback (most recent call last): File "D://code/advsamples.py", line 89, in raw_advs, clipped_advs, success = attack(fmodel, x_train, y_train, epsilons=0.3) File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\foolbox\attacks\base.py", line 283, in call xp = self.run(model, x, criterion, epsilon=epsilon, kwargs) File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\foolbox\attacks\fast_gradient_method.py", line 98, in run return super().run( File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\foolbox\attacks\gradient_descentbase.py", line 155, in run , gradients = self.value_and_grad(loss_fn, x) File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\foolbox\attacks\gradient_descent_base.py", line 111, in value_and_grad return ep.value_and_grad(loss_fn, x) File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\eagerpy\framework.py", line 360, in value_and_grad return t.value_and_grad(f, *args, *kwargs) File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\eagerpy\tensor\tensor.py", line 553, in value_and_grad return self._value_and_grad_fn(f, has_aux=False)(self, args, kwargs) File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\eagerpy\tensor\pytorch.py", line 505, in value_and_grad loss = f(x, *args, **kwargs) File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\foolbox\attacks\gradient_descent_base.py", line 97, in loss_fn return ep.crossentropy(logits, labels).sum() File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\eagerpy\framework.py", line 325, in crossentropy return logits.crossentropy(labels) File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\eagerpy\tensor\pytorch.py", line 470, in crossentropy torch.nn.functional.cross_entropy(self.raw, labels.raw, reduction="none") File "C:\Users\Lenovo.conda\envs\pytorch\lib\site-packages\torch\nn\functional.py", line 2846, in cross_entropy return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing) RuntimeError: "nll_loss_forward_no_reduce_cuda_kernel_index" not implemented for 'Float'

Rivendellad commented 2 years ago

I found the problem, the data type of the sample and the tag is not correct, the sample should be float32, and the tag should be int64 Thank you very much for your help