倒立摆_DQN算法_边做边学深度强化学习:PyTorch程序设计实践(5)
0、相关系列文章
迷宫_随机实验_边做边学深度强化学习:PyTorch程序设计实践(1)
迷宫_Sarsa算法_边做边学深度强化学习:PyTorch程序设计实践(2)
迷宫_Q-Learning算法_边做边学深度强化学习:PyTorch程序设计实践(3)
倒立摆_Q-Learning算法_边做边学深度强化学习:PyTorch程序设计实践(4)
1、Agent.py
from select import select
import numpy as np
import Brain
# 倒立摆小推车对象
class Agent:
def __init__(self, num_states, num_actions):
# 为智能体创建大脑以作出决策
self.brain = Brain.Brain(num_states, num_actions)
# 更新Q函数
def update_Q_function(self):
self.brain.replay()
# 确定下一个动作
def get_action(self, state, episode):
action = self.brain.decide_action(state, episode)
return action
# 将state\action\state_next和reward的内容保存在经验池中
def memorize(self, state, action, state_next, reward):
self.brain.memory.push(state, action, state_next, reward)
2、Brain.py
from os import rename
import numpy as np
from ReplayMemory import ReplayMemory, Transition
import Val
import random
import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
BATCH_SIZE = 32
CAPACITY = 10000
lr = 0.0001
# 使用Q表来实现Q学习
class Brain:
# 为智能体创建大脑以作出决策
def __init__(self, num_states, num_actions):
# 获取动作
self.num_actions = num_actions
# 创建存储经验的对象
self.memory = ReplayMemory(CAPACITY)
# 构建一个神经网络
self.model = nn.Sequential()
self.model.add_module('fc1',nn.Linear(num_states,32))
self.model.add_module('relu1',nn.ReLU())
self.model.add_module('fc2',nn.Linear(32,32))
self.model.add_module('relu2',nn.ReLU())
self.model.add_module('fc3',nn.Linear(32,num_actions))
# 输出网络的形状
print(self.model)
# 最优化方法的设定
self.optimizer = optim.Adam(self.model.parameters(),lr=0.0001)
# 通过Experience Replay 学习网络的连接参数
def replay(self)