大佬教程收集整理的这篇文章主要介绍了在 CUDA 上运行时,pytorch 生成进程已完成,退出代码为 139?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个简单的模型和训练函数来训练一个简单的线性回归训练,当在 cpu 上运行时,代码运行但在 CUDA 上运行时,代码随机崩溃中途无法弄清楚到底是什么问题。
这是模型:
import torch.nn as nn
import torch
class linearRegression1D:
def __init__(self,in_feature=1,out_feature=1):
self.in_feature = in_feature
self.out_feature = out_feature
self.device = torch.device("cuda")
self.loss_func = nn.MSELoss()
def set_device(self,device: str):
devices = ["cuda","cpu"]
if Device not in devices:
raise ValueError("Only {} is valID as device name".format(" and ".join(devices)))
self.device = torch.device(devicE)
def get_device(self):
return self.device
def get_loss_func(self):
return self.loss_func
def __call__(self,*args,**kwargs):
model = nn.linear(self.in_feature,self.out_featurE)
return model
这是训练函数:
import torch
from utills import move_to
from tqdm import tqdm
def Train_simple_network(
model,loss_func,Training_loader,epochs=20,device="cpu"
):
optimizer = torch.optim.SGD(model.parameters(),lr=0.001)
model.to(devicE)
for _ in tqdm(range(epochs),desc="Epoch"):
model = model.Train()
running_loss = 0.0
for inputs,labels in tqdm(Training_loader,desc="Batch",leave=falsE):
inputs = move_to(inputs,devicE)
labels = move_to(labels,devicE)
optimizer.zero_grad()
y_hat = model(inputs)
loss = loss_func(y_hat,labels)
loss.BACkWARD()
optimizer.step()
running_loss += loss.item()
@H_79_2@move to 函数是将任何数据结构移动到选定设备的通用函数:
def move_to(obj,devicE):
"""
Based on the type move python object
:param obj: the python object to move to a device,or to move its contents to a device
:param device: the compute device to move objects to
:return: python obj
"""
if isinstance(obj,List):
return [move_to(x,devicE) for x in obj]
elif isinstance(obj,tuplE):
return tuple(move_to(List(obj),devicE))
elif isinstance(obj,set):
return set(move_to(List(obj),Dict):
to_ret = Dict()
for key,value in obj.items():
to_ret[move_to(key,devicE)] = move_to(value,devicE)
return to_ret
elif hasattr(obj,"to"):
return obj.to(devicE)
else:
return obj
这是最后的执行:
import torch
from torch.utils.data import DataLoader
import seaborn as sns
from chapter2.DataLoaders import Simple1DRegressionDataSet
from chapter2.datagenerators import generate_1d_data
from chapter2.models import linearRegression1D
from chapter2.Train import Train_simple_network
# utility function to generate 1 feature and 1 target set
features,targets = generate_1d_data()
# sns.scatterplot(x=features,y=targets)
# Convert dataset object to iterator
Training_loader = DataLoader(Simple1DRegressionDataSet(features=features,targets=targets),shuffle=TruE)
# initialize model
model_instance = linearRegression1D()
# If cuda is used it throws error: Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
model_instance.set_device("cpu")
model = model_instance()
loss_func = model_instance.get_loss_func()
device = model_instance.get_device()
Train_simple_network(model=model,loss_func=loss_func,Training_loader=Training_loader,device=devicE)
为了重用目的,我已将代码分成文件。单独的代码可以在下面的github repo 中找到。最终的主要代码可以在以下文件 repo 中找到。
谁能帮我找出问题所在。
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
@H_772_40@以上是大佬教程为你收集整理的在 CUDA 上运行时,pytorch 生成进程已完成,退出代码为 139?全部内容,希望文章能够帮你解决在 CUDA 上运行时,pytorch 生成进程已完成,退出代码为 139?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。