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

Pytorch TypeError when running examples #34

Open
ravenslav opened this issue Jun 24, 2022 · 3 comments
Open

Pytorch TypeError when running examples #34

ravenslav opened this issue Jun 24, 2022 · 3 comments

Comments

@ravenslav
Copy link

Hi,
I am trying to run your example files.
I first ran train_influence_functions.py, which ran fine, and then test_influence_functions.py, where I get the following output and error:

Files already downloaded and verified
Files already downloaded and verified
2022-06-24 10:58:49,474: Running on: 1 images per class.
2022-06-24 10:58:49,475: Starting at img number: 0 per class.

/usr/local/lib/python3.7/dist-packages/pytorch_influence_functions/influence_function.py:70: UserWarning: Implicit dimension choice for log_softmax has been deprecated. Change the call to include dim=X as an argument.
  y = torch.nn.functional.log_softmax(y)

Calc. s_test recursions: [=================================================] 1 / 1
Averaging r-times: [=======================================================] 1 / 1
Calc. influence function: [========================================] 49999 / 50000
Calc. influence function: [========================================] 50000 / 50000

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

[<ipython-input-17-f738cfd2c21e>](https://localhost:8080/#) in <module>()
      3 trainloader, testloader = load_data()
      4 ptif.init_logging()
----> 5 inf = ptif.calc_img_wise(config, model, trainloader, testloader)

5 frames

[/usr/local/lib/python3.7/dist-packages/pytorch_influence_functions/calc_influence_function.py](https://localhost:8080/#) in calc_img_wise(config, model, train_loader, test_loader)
    469         influence, harmful, helpful, _ = calc_influence_single(
    470             model, train_loader, test_loader, test_id_num=i, gpu=config['gpu'],
--> 471             recursion_depth=config['recursion_depth'], r=config['r_averaging'])
    472         end_time = time.time()
    473 

[/usr/local/lib/python3.7/dist-packages/pytorch_influence_functions/calc_influence_function.py](https://localhost:8080/#) in calc_influence_single(model, train_loader, test_loader, test_id_num, gpu, recursion_depth, r, s_test_vec, time_logging)
    344         display_progress("Calc. influence function: ", i, train_dataset_size)
    345 
--> 346     harmful = np.argsort(influences)
    347     helpful = harmful[::-1]
    348 

<__array_function__ internals> in argsort(*args, **kwargs)

[/usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py](https://localhost:8080/#) in argsort(a, axis, kind, order)
   1112 
   1113     """
-> 1114     return _wrapfunc(a, 'argsort', axis=axis, kind=kind, order=order)
   1115 
   1116 

[/usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py](https://localhost:8080/#) in _wrapfunc(obj, method, *args, **kwds)
     52     bound = getattr(obj, method, None)
     53     if bound is None:
---> 54         return _wrapit(obj, method, *args, **kwds)
     55 
     56     try:

[/usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py](https://localhost:8080/#) in _wrapit(obj, method, *args, **kwds)
     41     except AttributeError:
     42         wrap = None
---> 43     result = getattr(asarray(obj), method)(*args, **kwds)
     44     if wrap:
     45         if not isinstance(result, mu.ndarray):

[/usr/local/lib/python3.7/dist-packages/torch/_tensor.py](https://localhost:8080/#) in __array__(self, dtype)
    731         if dtype is None:
    732             return self.numpy()
--> 733         else:
    734             return self.numpy().astype(dtype, copy=False)
    735 

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

I can't figure out why it is not working, as I assume it worked for you and I haven't changed anything.
Does anyone else have this issue or can tell me how to solve it?

@janellelines
Copy link

I also am having this issue and am wondering how to solve it.

PaulCCCCCCH added a commit to PaulCCCCCCH/robustar-influence-function that referenced this issue Nov 6, 2022
@alycia-noel
Copy link

In this file '/usr/local/lib/python3.7/dist-packages/pytorch_influence_functions/calc_influence_function.py' change line 346 to be harmful = np.argsort(influences.cpu()).

@1yanzeyu
Copy link

insert “influences = [i.cpu() for i in influences]” before the wrong sentence

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

4 participants