dqn pytorch代码
时间: 2024-02-24 12:51:58 浏览: 134
DQN(Deep Q-Network)是一种基于深度学习的强化学习算法,用于解决离散动作空间的问题。下面是一个简单的DQN PyTorch代码的介绍:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
# 定义DQN网络
class DQN(nn.Module):
def __init__(self, input_dim, output_dim):
super(DQN, self).__init__()
self.fc1 = nn.Linear(input_dim, 64)
self.fc2 = nn.Linear(64, 64)
self.fc3 = nn.Linear(64, output_dim)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
# 定义经验回放缓存
class ReplayBuffer():
def __init__(self, capacity):
self.capacity = capacity
self.buffer = []
self.position = 0
def push(self, state, action, reward, next_state, done):
if len(self.buffer) < self.capacity:
self.buffer.append(None)
self.buffer[self.position] = (state, action, reward, next_state, done)
self.position = (self.position + 1) % self.capacity
def sample(self, batch_size):
batch = np.random.choice(self.buffer, batch_size)
states, actions, rewards, next_states, dones = zip(*batch)
return np.array(states), np.array(actions), np.array(rewards), np.array(next_states), np.array(dones)
def __len__(self):
return len(self.buffer)
# 定义DQN Agent
class DQNAgent():
def __init__(self, input_dim, output_dim, lr, gamma, epsilon):
self.input_dim = input_dim
self.output_dim = output_dim
self.lr = lr
self.gamma = gamma
self.epsilon = epsilon
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = DQN(input_dim, output_dim).to(self.device)
self.target_model = DQN(input_dim, output_dim).to(self.device)
self.target_model.load_state_dict(self.model.state_dict())
self.target_model.eval()
self.optimizer = optim.Adam(self.model.parameters(), lr=self.lr)
self.loss_fn = nn.MSELoss()
self.replay_buffer = ReplayBuffer(capacity=10000)
def select_action(self, state):
if np.random.rand() < self.epsilon:
return np.random.randint(self.output_dim)
state = torch.tensor(state, dtype=torch.float32).unsqueeze(0).to(self.device)
q_values = self.model(state)
return torch.argmax(q_values).item()
def train(self, batch_size):
if len(self.replay_buffer) < batch_size:
return
states, actions, rewards, next_states, dones = self.replay_buffer.sample(batch_size)
states = torch.tensor(states, dtype=torch.float32).to(self.device)
actions = torch.tensor(actions, dtype=torch.long).unsqueeze(1).to(self.device)
rewards = torch.tensor(rewards, dtype=torch.float32).unsqueeze(1).to(self.device)
next_states = torch.tensor(next_states, dtype=torch.float32).to(self.device)
dones = torch.tensor(dones, dtype=torch.float32).unsqueeze(1).to(self.device)
q_values = self.model(states).gather(1, actions)
next_q_values = self.target_model(next_states).max(1)[0].unsqueeze(1)
target_q_values = rewards + self.gamma * next_q_values * (1 - dones)
loss = self.loss_fn(q_values, target_q_values.detach())
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
def update_target_model(self):
self.target_model.load_state_dict(self.model.state_dict())
def store_experience(self, state, action, reward, next_state, done):
self.replay_buffer.push(state, action, reward, next_state, done)
# 创建DQN Agent实例
input_dim = 4 # 输入维度
output_dim = 2 # 输出维度
lr = 0.001 # 学习率
gamma = 0.99 # 折扣因子
epsilon = 0.1 # 探索率
agent = DQNAgent(input_dim, output_dim, lr, gamma, epsilon)
# 训练DQN Agent
num_episodes = 1000
batch_size = 32
for episode in range(num_episodes):
state = env.reset()
done = False
total_reward = 0
while not done:
action = agent.select_action(state)
next_state, reward, done, _ = env.step(action)
agent.store_experience(state, action, reward, next_state, done)
agent.train(batch_size)
agent.update_target_model()
state = next_state
total_reward += reward
print(f"Episode: {episode+1}, Total Reward: {total_reward}")
# 使用训练好的DQN Agent进行预测
state = env.reset()
done = False
total_reward = 0
while not done:
action = agent.select_action(state)
next_state, reward, done, _ = env.step(action)
state = next_state
total_reward += reward
print(f"Total Reward: {total_reward}")
```
这段代码实现了一个简单的DQN Agent,包括DQN网络的定义、经验回放缓存的实现、Agent的训练和预测过程。你可以根据自己的需求进行修改和扩展。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)