Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with latest version of PyTorch #2

Open
AlisaK13003 opened this issue Jun 22, 2023 · 2 comments
Open

Working with latest version of PyTorch #2

AlisaK13003 opened this issue Jun 22, 2023 · 2 comments

Comments

@AlisaK13003
Copy link

AlisaK13003 commented Jun 22, 2023

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).

Screenshot 2023-06-22 135233
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!

@kkew3
Copy link
Owner

kkew3 commented Jun 24, 2023

Hi,

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

@zainsarwar865
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants