0

I have this piece of code, part of a Spikingjelly neural network training functionality, which gets some errors on this line after running. I'm not quite experienced in Python, and I don't understand what class initialization type this is.

The class definition and the __init__ looks like this:

class DVS128Gesture(sjds.NeuromorphicDatasetFolder):
    def __init__(
            self,
            root: str,
            train: bool = None,
            data_type: str = 'event',
            frames_number: int = None,
            split_by: str = None,
            duration: int = None,
            custom_integrate_function: Callable = None,
            custom_integrated_frames_dir_name: str = None,
            transform: Optional[Callable] = None,
            target_transform: Optional[Callable] = None,
    ) -> None:

And the intialization:

train_data_loader = torch.utils.data.DataLoader(
    dataset=DVS128Gesture(dataset_dir, train=True, use_frame=True, frames_num=T,
                            split_by=split_by, normalization=normalization),
    batch_size=batch_size,
    shuffle=True,
    num_workers=4,
    drop_last=True,
    pin_memory=True)
test_data_loader = torch.utils.data.DataLoader(
    dataset=DVS128Gesture(dataset_dir, train=False, use_frame=True, frames_num=T,
                            split_by=split_by, normalization=normalization),
    batch_size=batch_size,
    shuffle=False,
    num_workers=4,
    drop_last=False,
    pin_memory=True)

For example, class DVS128Gesture expects:

  • A string (root) as the address of the dataset

  • A bool (train) variable (which is not our interest right now)

  • A string

  • An integer value allocated to frames_number

The problem: However, the initialization (at the bottom of the picture) is not according to that sequence; for example, useframe=True is used as the 3rd parameter while the class's 3rd parameter was datatype: str = 'event' .

Questions:

  1. Is the problem I mentioned above, really an error by the author? or my misunderstanding of something?

  2. What's the meaning of a variable assignment during a class initialization? For example, the 4th parameter of class initialization is written framesnum=T. Why not write 'T' directly? and why it was used framesnum instead of the original frames_number written in class definition?

I traced the mentioned variables the their declaration and classes they were used in and as much as I can understand, there is no other variables with the same name.

2
  • 1
    they are not positional arguments but keyword arguments (order not important), also the function must have a **kwargs argument to catch all the non specified keyword arguments Commented Oct 4, 2023 at 9:33
  • 1
    A keyword argument of a function call consists of the name of a function parameter, equal sign and value (an expression, to be precise). It can have a different order as in function definition because the parameter name tells where it belongs to. The frames_num versus frames_number seems an error to me. Commented Oct 4, 2023 at 9:34

1 Answer 1

0
  1. As others mentioned in the comments, this is a keyword argument (kwarg), for which order does not matter. You don't /have/ to use keywords when calling a function, but if you don't, then all of your arguments must be in the correct order and position.

  2. This seems to be an error. You might use frames_number=T rather than just T so that it doesn't matter if you pass arguments in the correct order, but you're right that there's no framesnum kwarg in the original class declaration. There is also no use_frame or normalization kwarg, so I'm skeptical of what's going on here. It is possible to pass on keyword arguments for a parent class without specifically defining them in the child class's __init__, by adding **kwargs to the list of arguments and passing that to super.init(), but that isn't being done here, nor does the parent class actually have any of these keyword arguments in the first place.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.