You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to say I'm still a beginner to all things machine learning, so I may need some things to be dumbed down.
I am trying to simply recreate your code in Google Colaboratory using Python 3.10 and PyTorch 2.0.1. In my process, I have stumbled upon an IndexError (provided screenshot).
Do you happen to have any thoughts on how to fix this issue, or how I could rewrite the code to work with the latest versions?
I will also provide my code. I have not changed anything, other than not using the inputs_box feature and replacing it with the code "transform = `transforms.Compose([transforms.ToTensor()])"
from binascii import hexlify
import torch
import cw
from torch.utils.data import DataLoader
import torchvision
from torchvision import transforms
import pickle
from torch import nn
I may not have time to reimplement the code using modern PyTorch and Python and look closely into your code right now. Sorry about that. But I may do so, in the near future.
Return to your question:
IndexError: invalid index of a 0-dim tensor. Use `tensor.item()' in Python or tensor.item(()' in C++ to convert a 0-dim tensor to a number
Have you tried following the suggestion of using tensor.item() rather than tensor.data[0]?
P.S. Could you paste your code in the code fence? Thanks
There are really very few changes you need to make to make this compatible with more recent versions of pyTorch and Python.
Using item() instead of the index and getting rid of Variable in your code since they are not required anymore. Moreover, if you're working with python3, you'd need to change the syntax of the print statements.
Hi,
I want to say I'm still a beginner to all things machine learning, so I may need some things to be dumbed down.
I am trying to simply recreate your code in Google Colaboratory using Python 3.10 and PyTorch 2.0.1. In my process, I have stumbled upon an IndexError (provided screenshot).
Do you happen to have any thoughts on how to fix this issue, or how I could rewrite the code to work with the latest versions?
I will also provide my code. I have not changed anything, other than not using the inputs_box feature and replacing it with the code "transform = `transforms.Compose([transforms.ToTensor()])"
from binascii import hexlify
import torch
import cw
from torch.utils.data import DataLoader
import torchvision
from torchvision import transforms
import pickle
from torch import nn
image_path = '/content'
transform = transforms.Compose([transforms.ToTensor()])
mnist_train_dataset = torchvision.datasets.MNIST(
root=image_path,
train=True,
download=True,
transform=transform)
mnist_test_dataset = torchvision.datasets.MNIST(
root=image_path, train=False, download=False,
transform=transform)
batch_size = 100
torch.manual_seed(1)
train_dl = DataLoader(mnist_train_dataset,
batch_size, shuffle=True)
adversary = cw.L2Adversary(targeted=False,
confidence=0.0,
search_steps=10,
optimizer_lr=5e-4)
inputs, targets = next(iter(train_dl))
class net(nn.Module):
def init(self):
super().init()
self.model = nn.Sequential(
nn.Conv2d(1, 100, (3, 3)),
nn.ReLU(),
nn.Conv2d(100, 200, (3, 3)),
nn.ReLU(),
nn.Flatten(),
nn.Linear(200*(28-4)*(28-4), 10),
)
def forward(self, x):
return self.model(x)
targets == [1] #mnist, we have 10 classes
adversarial_examples = adversary(net(),
inputs,
targets,
to_numpy=False)
with open('adversarial_examples.npy', 'wb') as f:
pickle.dump(adversarial_examples, f)
Thank you!
The text was updated successfully, but these errors were encountered: