zjuwss / gnnwr

A PyTorch implementation of the Geographically Neural Network Weighted Regression (GNNWR)
GNU General Public License v3.0
51 stars 6 forks source link

使用CPU执行predict方法报错 #1

Closed sdqdsjf1999 closed 8 months ago

sdqdsjf1999 commented 9 months ago

我事先使用GPU跑出来模型的结果保存为模型文件 再在只有CPU的设备上使用load_model加载路径上的模型文件并预测模型结果 在执行predict()方法之后,报错有变量在cuda而不是cpu上 以下是我的代码

import pandas as pd
from gnnwr import models,datasets
import torch
data=pd.read_csv('./Data/industrial_cpart.csv')
pred_data = pd.read_csv("./Data/test-ind.csv")

train_dataset,val_dataset,test_dataset = datasets.init_dataset(data=data,
                                                               test_ratio=0.3,valid_ratio=0.3,
                                                               spatial_column=['LNG','LAT'],
                                                               y_column=['PRICE'],
                                                               temp_column=['DATE'],
                                                               id_column=['id'],
                                                               # temp_column=['DATE'],
                                                               x_column=['BUSINESS','AREA','VOLUMNE_RATIIO'])
pred_dataset = datasets.init_predict_dataset(data=pred_data,train_dataset=train_dataset,x_column=['BUSINESS','AREA','VOLUMNE_RATIIO'],spatial_column=['LNG','LAT'],temp_column=['DATE'])
GTNNWR_ind=models.GTNNWR(train_dataset,val_dataset,test_dataset)
GTNNWR_ind.load_model(path='./gtnnwr_models/GTNNWR_industrial_date.pkl',use_dict=False)

res = GTNNWR_ind.predict(pred_dataset)

报错信息如下: /home/induschain/anaconda3/lib/python3.9/site-packages/gnnwr/utils.py:26: FutureWarning: Series.getitem treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos] intercept = self.__fit.params[0] Traceback (most recent call last): File "/home/induschain/zju/sjf/GNNWR/predict_test.py", line 23, in res = GTNNWR_ind.predict(pred_dataset) File "/home/induschain/anaconda3/lib/python3.9/site-packages/gnnwr/models.py", line 398, in predict output = self._out(self._model(data).mul(coef.to(torch.float32))) File "/home/induschain/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl return self._call_impl(*args, *kwargs) File "/home/induschain/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl return forward_call(args, **kwargs) File "/home/induschain/anaconda3/lib/python3.9/site-packages/torch/nn/parallel/data_parallel.py", line 171, in forward raise RuntimeError("module must have its parameters and buffers " RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cpu

yorktownting commented 9 months ago

我们在0.1.3版本中加入了对CPU二进制模型的支持,可以尝试使用以下方法来载入使用GPU完成训练的模型,并使用CPU进行预测:

gnnwr = models.GNNWR(train_dataset, val_dataset, test_dataset, [256, 64, 16])
gnnwr.gpumodel_to_cpu('../gnnwr-models/GNNWR_20231027-000000.pkl', '../gnnwr-models/GNNWR_CPU.pkl')
gnnwr.load_model('../gnnwr_models/GNNWR_CPU.pkl', map_location='cpu', use_dict=True)
gnnwr.result('../gnnwr_models/GNNWR_CPU.pkl', map_location='cpu', use dict=True)