Batch training error got an unexpected keyword...

Synopsis

       Recently, when I was learning the batch training of Neural Networks, I encountered a problem, that is, when creating the dataset Times __init__() got an unexpected keyword argument 'data_tensor', I searched for a long time without effective results. But I tried it by accident and the result was successful. The debugging process is listed below

Original situation

Original code

x = torch.linspace(1, 10, 10)       # x data (torch tensor)
y = torch.linspace(10, 1, 10)       # y data (torch tensor)

# 先转换成 torch 能识别的 Dataset
torch_dataset = Data.TensorDataset(data_tensor=x, target_tensor=y)

Report an error
Report an error

Initial commissioning

       After some searching, there is a way on the Internet to remove data_tensor and target_tensor, namely

x = torch.linspace(1, 10, 10)       # x data (torch tensor)
y = torch.linspace(10, 1, 10)       # y data (torch tensor)

# 先转换成 torch 能识别的 Dataset
torch_dataset = Data.TensorDataset(x, y)

       But this time the error is more exaggerated, see the picture below
Report an error

Continue debugging

       The above picture is only a small part of the error report that I intercepted. Although the error report is very exaggerated, we caught a sentence while browsing the error information.
This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module:

if __name__ == '__main__':
    freeze_support()
    ...

       This program opens dual threads, and the above paragraph tells us that there is no way to open a new thread before the current process ends. why? The following tells us that there is no start entrance. In other words, we have to add a start entry to the program.
       Then continue to run

if __name__ == '__main__':
    x = torch.linspace(1, 10, 10)
    y = torch.linspace(10, 1, 10)

    # 先转换成 torch 能识别的 Dataset
    torch_dataset = data.TensorDataset(x, y)

result
result

Final reflection

       Okay, we successfully ran the results. But we still have to think about the debugging process this time, and see if the problem is really just because it was not added? Then, we might as well change the code back.

if __name__ == '__main__':
    x = torch.linspace(1, 10, 10)
    y = torch.linspace(10, 1, 10)

    # 先转换成 torch 能识别的 Dataset
    torch_dataset = data.TensorDataset(data_tensor=x, target_tensor=y)

       Let's run it and have a try
Report an error
       . The error is still reported. It seems that to solve this problem, we need to add the entry and remove the data_tensor and target_tensor.
       So, there is one last question: Why did you report __init__() got an unexpected keyword argument 'data_tensor'it? This is something I still don't understand. If there are big guys passing by, please leave your opinion.

If you find it helpful, please like it

Guess you like

Origin blog.csdn.net/ThunderF/article/details/94733747