This error usually occurs in Windows systems using multiple processes. For example, execute the following code in pychar:
import torch
import torch.utils.data as Data
import numpy as np
from sklearn.datasets import load_iris
iris_x, irisy = load_iris(return_X_y=True)
print("iris_x.dtype:", iris_x.dtype)
print("irisy:", irisy.dtype)
## transform the training set x into a tensor, and the training set y into a tensor
train_xt = torch.from_numpy(iris_x.astype(np.float32))
train_yt = torch.from_numpy(irisy.astype(np.int64))
print("train_xt.dtype:", train_xt.dtype)
print("train_yt.dtype:", train_yt.dtype)
## After converting the training set into a tensor, use TensorDataset to collate X and Y together
train_data = Data.TensorDataset(train_xt, train_yt)
## Define a data loader to batch the training dataset
train_loader = Data.DataLoader(
dataset=train_data, ## the dataset to use
batch_size=10, # # Batch sample size
shuffle=True, # Break up the data before each iteration
num_workers=2, # [Note: 2 processes are used here]
)
## Check if the dimensionality of the samples of a batch of the training dataset is correct
for step, (b_x, b_y) in enumerate(train_loader):
if step > 0:
break
## Output the dimensions of the training image and the labels, and the data type
print("b_x.shape:", b_x.shape)
print("b_y.shape:", b_y.shape)
print("b_x.dtype:", b_x.dtype)
print("b_y.dtype:", b_y.dtype)
## --------- -The correct result is as follows -------- --
# iris_x.dtype: float64
# irisy: int32
# train_xt.dtype: torch.float32
# train_yt.dtype: torch.int64
# b_x.shape: torch.Size([10, 4])
# b_y.shape: torch.Size([10])
# b_x.dtype: torch.float32
# b_y.dtype: torch.int64
The following errors will be reported. (no error will be reported when running in jupyter notebook under the same environment. I don’t know why…)
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
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()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
Solution 1:
Remove the statement setting up multiple processes. In this example, comment or delete the following line.
num_workers=2, # [Note: 2 processes are used here]
Solution 2:
Move the code part of calling multiple processes to [if _name_ = = ‘_main_’:].
if __name__ == '__main__':
## Check if the dimensionality of the samples of a batch of the training dataset is correct
for step, (b_x, b_y) in enumerate(train_loader):
if step > 0:
break
## Output the dimensions of the training image and the dimensions of the labels, and the data type
print("b_x.shape:", b_x.shape)
print("b_y.shape:", b_y.shape)
print("b_x.dtype:", b_x.dtype)
print("b_y.dtype:", b_y.dtype)
However, in pychart, the part before [for step, (b_x, b_y) in enumerate (train_loader):] will be executed twice.
## ——————————The result of running in Pycharm is as follows——————————
iris_x.dtype: float64
irisy: int32
train_xt.dtype: torch.float32
train_yt.dtype: torch.int64
iris_x.dtype: float64
irisy: int32
train_xt.dtype: torch.float32
train_yt.dtype: torch.int64
b_x.shape: torch.Size([10, 4])
b_y.shape: torch.Size([10])
b_x.dtype: torch.float32
b_y.dtype: torch.int64
Read More:
- [Solved] RuntimeError An attempt has been made to start a new process
- [Solved] mindinsight modelart Error: RuntimeError: An attempt has been made to start a new process before…
- Python Error: Process finished with exit code -1073740791 (0xC0000409)
- [Solved] Error starting proxy server: oserror (10013), “an attempt was made to access the socket in a way that the access permission is not allowed.”, None, 10013, None)
- [Solved] AttributeError: module ‘thread‘ has no attribute ‘start_new_thread‘
- [Solved] PyQt: RuntimeError: wrapped C/C++ object has been deleted & has no attribute of flush in python
- [Solved] Jupyter Notebook Start Error: Fatal error in launcher: Unable to create process using
- Python3 Fatal error in launcher: Unable to create process using ‘”‘
- Python PIP Fatal error in launcher: Unable to create process using ‘“e:\program files\programdata
- Python writes DICOM file (attributeerror: ‘filemetadataset’ object has no attribute ‘transfersyntax uid’ solution)
- [Solved] Python Keras Error: AttributeError: ‘Sequential‘ object has no attribute ‘predict_classes‘
- When sending an email, an error was reported: AttributeError:’list’ object has no attribute’encode’
- How to Solve attributeerror: ‘list’ object has no attribute ‘shape‘
- [Solved] PyTorch Caught RuntimeError in DataLoader worker process 0和invalid argument 0: Sizes of tensors mus
- How to Solve Python AttributeError: ‘dict’ object has no attribute ‘item’
- [Solved] Pytorch-transformers Error: AttributeError: ‘str‘ object has no attribute ‘shape‘
- A summary of a demo development process for Python using the QT5 development interface
- Python 3 urllib has no URLEncode attribute
- Python asynchronous co process crawler error: [aiohttp. Client]_ Exceptions: serverdisconnected error: Server Disconnected]
- How to Solve Python AttributeError: ‘module’ object has no attribute ‘xxx’