DeepPSP / torch_ecg

Deep learning ECG models implemented using PyTorch
MIT License
161 stars 20 forks source link

TypeError: forward() takes 2 positional arguments but 3 were given #4

Closed chengstark closed 1 year ago

chengstark commented 1 year ago

Hello, I encountered this error:

config = CFG(
    random=False
)
ppm = PreprocManager.from_config(config)
sig = torch.rand(12,80000).numpy()
sig, fs = ppm(sig, 200)

TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_1437211/3652760291.py in <module>
      4 ppm = PreprocManager.from_config(config)
      5 sig = torch.rand(12,80000).numpy()
----> 6 sig, fs = ppm(sig, 200)

/labs/hulab/stark_conda/envs/base_pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []

TypeError: forward() takes 2 positional arguments but 3 were given
wenh06 commented 1 year ago

Are you using the latest version (master branch or the latest PyPI version, which currently is 0.0.25)? image You were using a dummy (empty) preprocessor. Now I'd like to raise a warning for such use cases when calling from_config.

chengstark commented 1 year ago

I installed the package with pip just now, python is 3.7.13, torch-ecg is 0.0.25. I ran the package with a real signal, it still produced this error.

image

1665426611756
wenh06 commented 1 year ago

I see. There are actually 2 types of PreProcessors. One is from torch_ecg._preprocessors, whose signature is (sig:numpy.ndarray, fs:int) -> Tuple[numpy.ndarray, int]; the other is from torch_ecg.preprocessors with signature (sig: torch.Tensor) -> torch.Tensor.

chengstark commented 1 year ago

Thank you for replying, I am currently using from torch_ecg.preprocessors import PreprocManager, should I use the other one?

wenh06 commented 1 year ago

Thank you for replying, I am currently using from torch_ecg.preprocessors import PreprocManager, should I use the other one?

It depends on whether you do the preprocessings before or after transforming the data from numpy arrays to torch tensors. Usually, I would do it in some Dataset class before this transformation. But I am not sure whether my data processing pipeline could be further optimized.

chengstark commented 1 year ago

I see, please correct me if I misunderstood. If I use the preprocessing manager (torch_ecg.preprocessors import PreprocManager) in the Dataset class it won't generate this " forward() takes 2 positional arguments but 3 were given " error?

Which should I use if I just want to test it out on a single signal (numpy array) beforehand?

wenh06 commented 1 year ago

If you just want to try something on numpy arrays, please use torch_ecg._preprocessors.PreprocManager.

torch_ecg._preprocessors.PreprocManager takes numpy arrays as input, with an additional fs parameter.

torch_ecg.preprocessors.PreprocManager takes only one torch tensor as input.

chengstark commented 1 year ago

Problem solved. Thank you so much!