报错内容:An attempt has been made to start a new process before the current process has finished
该报错一般发生在Windows系统中使用多进程。比如在Pycharm中执行如下代码:
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)
## 训练集X转化为张量,训练集y转化为张量
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)
## 将训练集转化为张量后,使用TensorDataset将X和Y整理到一起
train_data = Data.TensorDataset(train_xt, train_yt)
## 定义一个数据加载器,将训练数据集进行批量处理
train_loader = Data.DataLoader(
dataset=train_data, ## 使用的数据集
batch_size=10, # 批处理样本大小
shuffle=True, # 每次迭代前打乱数据
num_workers=2, # 【注意:这里使用2个进程】
)
## 检查训练数据集的一个batch的样本的维度是否正确
for step, (b_x, b_y) in enumerate(train_loader):
if step > 0:
break
## 输出训练图像的尺寸和标签的尺寸,和数据类型
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)
## ——————————正确结果如下——————————
# 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
则会报如下的错。(在同样环境下的Jupyter Notebook中运行不会报错,目前不知道为何。。。)
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.
关于这里的freeze_support(),讲解可见:
讲解python中的 freeze_support()_欢迎关注,共同进步!-CSDN博客
其他例子见自己电脑 Pytorch\SYL_YBG\Test 路径下:
2_5_2_图像数据预处理.py
2_5_1_高维数组预处理.py
解决方法一:
把设置多进程的语句去掉,该例子中即把下面这行注释掉、删掉或者num_workers设为0。该语句表示调用 1+num_workers 个进程。Linux系统,该参数正常,不需担心。
num_workers=2, # 【注意:这里使用2个进程】
解决方法二:
将当前位于脚本顶层的所有代码放在一个
函数中,然后在【if __name__ == '__main__':】语句下调用。
import torch
import torch.utils.data as Data
import numpy as np
from sklearn.datasets import load_iris
def Func():
iris_x, irisy = load_iris(return_X_y=True)
print("iris_x.dtype:", iris_x.dtype)
print("irisy:", irisy.dtype)
## 训练集X转化为张量,训练集y转化为张量
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)
## 将训练集转化为张量后,使用TensorDataset将X和Y整理到一起
train_data = Data.TensorDataset(train_xt, train_yt)
## 定义一个数据加载器,将训练数据集进行批量处理
train_loader = Data.DataLoader(
dataset=train_data, ## 使用的数据集
batch_size=10, # 批处理样本大小
shuffle=True, # 每次迭代前打乱数据
num_workers=2, # 【注意:这里使用2个进程】
)
## 检查训练数据集的一个batch的样本的维度是否正确
for step, (b_x, b_y) in enumerate(train_loader):
if step > 0:
break
## 输出训练图像的尺寸和标签的尺寸,和数据类型
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)
if __name__ == '__main__': # 把上面所有内容都写到一个函数里,在该语句下调用函数,则只执行1遍!
Func()
## ——————————Pycharm中运行结果如下——————————
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
——————————————
【注意】:如果只 把调用多进程的代码部分,移到【if __name__ == '__main__':】下。
if __name__ == '__main__':
## 检查训练数据集的一个batch的样本的维度是否正确
for step, (b_x, b_y) in enumerate(train_loader):
if step > 0:
break
## 输出训练图像的尺寸和标签的尺寸,和数据类型
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)
在Pycharm中,会把【for step, (b_x, b_y) in enumerate(train_loader):】这行代码之前的部分执行3遍。
## ——————————Pycharm中运行结果如下——————————
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
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