吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 811|回复: 13
收起左侧

[求助] 如何将下面的py代码改为无gpu的环境中运行呢?

[复制链接]
jtwc 发表于 2023-9-8 14:05
本帖最后由 jtwc 于 2023-9-8 14:10 编辑

各位老师,下面代码出错,此错误是由于下载的torch没有cuda,在运行时就会出错,如何将下面的py代码改为无gpu的环境中运行呢?
time_idx= 2018-05-09
Traceback (most recent call last):
  File "E:/main_test.py", line 149, in <module>
    rdpg = RDPG(demo_env, test_env, args)
  File "E:\rdpg.py", line 56, in __init__
    self.agent = Agent(args)
  File "E:\agent.py", line 18, in __init__
    self.rnn = RNN(args)
  File "E:\
[Python] 纯文本查看 复制代码
import numpy as npimport argparse
from copy import deepcopy
import random
import torch
from timeit import default_timer as timer

from evaluator import Evaluator
from rdpg import RDPG
from util import *
from environment import environment

torch.cuda.empty_cache()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='PyTorch on Financial trading--iRDPG algorithm')
    
    ##### Model Setting #####
    # parser.add_argument('--rnn_mode', default='lstm', type=str, help='RNN mode: LSTM/GRU')
    parser.add_argument('--rnn_mode', default='gru', type=str, help='RNN mode: LSTM/GRU')
    parser.add_argument('--input_size', default=14, type=int, help='num of features for input state')
    parser.add_argument('--seq_len', default=15, type=int, help='sequence length of input state')
    parser.add_argument('--num_rnn_layer', default=2, type=int, help='num of rnn layer')
    parser.add_argument('--hidden_rnn', default=128, type=int, help='hidden num of lstm layer')
    parser.add_argument('--hidden_fc1', default=256, type=int, help='hidden num of 1st-fc layer')
    parser.add_argument('--hidden_fc2', default=64, type=int, help='hidden num of 2nd-fc layer')
    parser.add_argument('--hidden_fc3', default=32, type=int, help='hidden num of 3rd-fc layer')
    parser.add_argument('--init_w', default=0.005, type=float, help='initialize model weights') 
    
    ##### Learning Setting #####
    parser.add_argument('--r_rate', default=0.0001, type=float, help='gru layer learning rate')  
    parser.add_argument('--c_rate', default=0.0001, type=float, help='critic net learning rate') 
    parser.add_argument('--a_rate', default=0.0001, type=float, help='policy net learning rate (only for DDPG)')
    parser.add_argument('--beta1', default=0.3, type=float, help='mometum beta1 for Adam optimizer')
    parser.add_argument('--beta2', default=0.9, type=float, help='mometum beta2 for Adam optimizer')
    parser.add_argument('--sch_step_size', default=16*150, type=float, help='LR_scheduler: step_size')
    parser.add_argument('--sch_gamma', default=0.5, type=float, help='LR_scheduler: gamma')
    parser.add_argument('--bsize', default=100, type=int, help='minibatch size')
    
    ##### RL Setting #####
    parser.add_argument('--warmup', default=100, type=int, help='only filling the replay memory without training')
    parser.add_argument('--discount', default=0.95, type=float, help='future rewards discount rate')
    parser.add_argument('--a_update_freq', default=3, type=int, help='actor update frequecy (per N steps)')
    parser.add_argument('--Reward_max_clip', default=15., type=float, help='max DSR reward for clipping')
    parser.add_argument('--tau', default=0.002, type=float, help='moving average for target network')
    ##### original Replay Buffer Setting #####
    parser.add_argument('--rmsize', default=12000, type=int, help='memory size')
    parser.add_argument('--window_length', default=1, type=int, help='')  
    ##### Exploration Setting #####
    parser.add_argument('--ou_theta', default=0.18, type=float, help='noise theta of Ornstein Uhlenbeck Process')
    parser.add_argument('--ou_sigma', default=0.3, type=float, help='noise sigma of Ornstein Uhlenbeck Process') 
    parser.add_argument('--ou_mu', default=0.0, type=float, help='noise mu of Ornstein Uhlenbeck Process') 
    parser.add_argument('--epsilon_decay', default=100000, type=int, help='linear decay of exploration policy')
    
    ##### Training Trajectory Setting #####
    parser.add_argument('--exp_traj_len', default=16, type=int, help='segmented experiece trajectory length')  
    parser.add_argument('--train_num_episodes', default=2000, type=int, help='train iters each episode')  
    ### Also use in Test (Evaluator) Setting ###
    parser.add_argument('--max_episode_length', default=240, type=int, help='the max episode length is 240 minites in one day')  
    parser.add_argument('--test_episodes', default=243, type=int, help='how many episode to perform during testing periods')
    
    ##### PER Demostration Buffer #####
    parser.add_argument('--is_PER_replay', default=True, help='conduct PER momery or not')
    parser.add_argument('--is_pretrain', default=True, action='store_true', help='conduct pretrain or not')
    parser.add_argument('--Pretrain_itrs', default=10, type=int, help='number of pretrain iterations')
    parser.add_argument('--is_demo_warmup', default=True, action='store_true', help='Execute demonstration buffer')
    parser.add_argument('--PER_size', default=40000, type=int, help='memory size for PER')
    parser.add_argument('--p_alpha', default=0.3, type=int, help='the power of priority for each experience')
    parser.add_argument('--lambda_balance', default=50, type=int, help='priority coeffient for weighting the gradient term')
    parser.add_argument('--priority_const', default=0.1, type=int, help='priority constant for demonstration experiences')
    parser.add_argument('--small_const', default=0.001, type=int, help='priority constant for agent experiences')
    
    ##### Behavior Cloning #####
    parser.add_argument('--is_BClone', default=True, action='store_true', help='conduct behavior cloning or not')
    parser.add_argument('--is_Qfilt', default=False, action='store_true', help='conduct Q-filter or not')
    parser.add_argument('--use_Qfilt', default=100, type=int, help='set the episode after warmup to use Q-filter')
    parser.add_argument('--lambda_Policy', default=0.7, type=int, help='The weight for actor loss')
    # parser.add_argument('--lambda_BC', default=0.5, type=int, help='The weight for BC loss after Q-filter, default is equal to (1-lambda_Policy)')
    
    ##### Other Setting #####
    parser.add_argument('--seed', default=627, type=int, help='seed number')
    parser.add_argument('--date', default=629, type=int, help='date for output file name')
    parser.add_argument('--save_threshold', default=20, type=int, help='lack margin stop ratio')
    parser.add_argument('--lackM_ratio', default=0.7, type=int, help='lack margin stop ratio')
    parser.add_argument('--debug', default=True, dest='debug', action='store_true')
    parser.add_argument('--checkpoint', default="checkpoints", type=str, help='Checkpoint path')
    parser.add_argument('--logdir', default='log')
    parser.add_argument('--mode', default='test', type=str, help='support option: train/test')
    # parser.add_argument('--mode', default='train', type=str, help='support option: train/test')
    
    
    args = parser.parse_args()
    #######################################################################################################

    ####################################################################################################
    '''##### Run Task #####'''
    if args.seed > 0:
        np.random.seed(args.seed)
        random.seed(args.seed)

    is_lack_margin = True
    # is_lack_margin = False
    
    ##### Demonstration Setting #####
    if args.is_demo_warmup:
        data_fn = "data_preprocess/IF_tech_oriDT.csv"
        demo_env = environment(data_fn=data_fn, data_mode='random', duration='train', is_demo=True, 
                               is_intraday=True, is_lack_margin=is_lack_margin, args=args)
    else:
        demo_env = None
        
        
    ##### Run Training #####
    start_time = timer()
    if args.mode == 'train':
        print('##### Run Training #####')
        ### train_env setting ###
        data_mode = 'random'  # random select a day for a trading episode (240 minutes)
        duration = 'train'  # training period from 2016/1/1 to 2018/5/8
        
        data_fn = "data_preprocess/IF_prophetic.csv"
        train_env = environment(data_fn=data_fn, data_mode=data_mode, duration=duration, is_demo=False, 
                                is_intraday=True, is_lack_margin=is_lack_margin, args=args)
        
        ### Run training ###
        rdpg = RDPG(demo_env, train_env, args)
        rdpg.train(args.train_num_episodes, args.checkpoint, args.debug)
        
        end_time = timer()
        minutes, seconds = (end_time - start_time)//60, (end_time - start_time)%60
        print(f"\nTraining time taken: {minutes} minutes {seconds:.1f} seconds")
    
    ##### Run Testing #####
    elif args.mode == 'test':
        torch.cuda.empty_cache()
        print('##### Run Testing #####')
        ### test_env setting ###
        # is_demo = True
        is_demo = False  
        data_mode = 'time_order'  
        duration = 'test'  # testing period from 2018/5/9 to 2019/5/8
        is_lack_margin = True
        
        # data_fn = "data_preprocess/IF_prophetic.csv"
        data_fn = "data_preprocess/IC_prophetic.csv"
        test_env = environment(data_fn=data_fn,  data_mode=data_mode, duration=duration, is_demo=is_demo, 
                                is_intraday=True, is_lack_margin=is_lack_margin, args=args)
        rdpg = RDPG(demo_env, test_env, args)
        

        description = 'iRDPG_agent' 
        model_fn = description +'.pkl'
        rdpg.test(args.checkpoint, model_fn, description, lackM=is_lack_margin, debug=args.debug)
                
            
        end_time = timer()
        minutes, seconds = (end_time - start_time)//60, (end_time - start_time)%60
        print(f"\nTesting time taken: {minutes} minutes {seconds:.1f} seconds")
        
    else:
        raise RuntimeError('undefined mode {}'.format(args.mode))


model.py", line 32, in __init__
    self.cx = Variable(torch.zeros(self.num_layer, 1, self.hidden_rnn)).type(FLOAT).cuda()
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\cuda\__init__.py", line 221, in _lazy_init
    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

eKing 发表于 2023-9-8 14:59
把19行不注释20注释掉试试
15126819695 发表于 2023-9-8 14:59
项目包发出来吧 model.py都没用  你这个报错在model.py里面
 楼主| jtwc 发表于 2023-9-8 15:05
plauger 发表于 2023-9-8 15:13
本帖最后由 plauger 于 2023-9-8 15:15 编辑

这应该跟你python环境安装的pytorch 版本有关系,不用GPU,那么应该安装pytorch的cpu版本,然后把20注释,19行不注释,
再把所有以下行都注释掉:
torch.cuda.empty_cache()

~零度 发表于 2023-9-8 15:15
本帖最后由 ~零度 于 2023-9-8 15:16 编辑

你把和cuda有关的去掉就行了
[Shell] 纯文本查看 复制代码
model.py", line 32, in __init__
    self.cx = Variable(torch.zeros(self.num_layer, 1, self.hidden_rnn)).type(FLOAT).cuda()
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\cuda\__init__.py", line 221, in _lazy_init
    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled


model.py第32行:
[Python] 纯文本查看 复制代码
self.cx = Variable(torch.zeros(self.num_layer, 1, self.hidden_rnn)).type(FLOAT).cuda()

最后那个cuda()表示把变量移动到GPU上,其他地方如果也有cuda相关的,也需要改一下

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
jtwc + 1 + 1 我很赞同!
lizy169 + 1 + 1 谢谢@Thanks!

查看全部评分

 楼主| jtwc 发表于 2023-9-8 15:16
plauger 发表于 2023-9-8 15:13
这应该跟你python环境安装的pytorch 版本有关系,不用GPU,那么应该安装pytorch的cpu版本,然后把20注释,1 ...

老师,不行,还是一样
plauger 发表于 2023-9-8 15:18
jtwc 发表于 2023-9-8 15:16
老师,不行,还是一样

所有与cuda有关的都要注释掉
 楼主| jtwc 发表于 2023-9-8 15:19
15126819695 发表于 2023-9-8 14:59
项目包发出来吧 model.py都没用  你这个报错在model.py里面

老师,我看看
 楼主| jtwc 发表于 2023-9-8 15:26
~零度 发表于 2023-9-8 15:15
你把和cuda有关的去掉就行了
[mw_shl_code=shell,true]model.py", line 32, in __init__
    self.cx = V ...

谢谢老师
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-28 13:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表