DQN算法打印迭代奖励值代码
时间: 2023-07-26 20:33:36 浏览: 117
好的,以下是使用DQN算法打印迭代奖励值的代码示例:
```python
import gym
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
class DQNAgent:
def __init__(self, state_size, action_size):
self.state_size = state_size
self.action_size = action_size
self.memory = []
self.gamma = 0.95
self.epsilon = 1.0
self.epsilon_decay = 0.995
self.epsilon_min = 0.01
self.learning_rate = 0.001
self.model = self._build_model()
def _build_model(self):
model = Sequential()
model.add(Dense(24, input_dim=self.state_size, activation='relu'))
model.add(Dense(24, activation='relu'))
model.add(Dense(self.action_size, activation='linear'))
model.compile(loss='mse', optimizer=Adam(lr=self.learning_rate))
return model
def remember(self, state, action, reward, next_state, done):
self.memory.append((state, action, reward, next_state, done))
def act(self, state):
if np.random.rand() <= self.epsilon:
return np.random.choice(self.action_size)
else:
return np.argmax(self.model.predict(state)[0])
def replay(self, batch_size):
minibatch = np.random.choice(len(self.memory), batch_size, replace=False)
for state, action, reward, next_state, done in minibatch:
target = reward
if not done:
target = reward + self.gamma * np.amax(self.model.predict(next_state)[0])
target_f = self.model.predict(state)
target_f[0][action] = target
self.model.fit(state, target_f, epochs=1, verbose=0)
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
def train(agent, env, episodes, batch_size):
rewards = []
for episode in range(episodes):
state = env.reset()
state = np.reshape(state, [1, agent.state_size])
done = False
total_reward = 0
while not done:
action = agent.act(state)
next_state, reward, done, _ = env.step(action)
next_state = np.reshape(next_state, [1, agent.state_size])
agent.remember(state, action, reward, next_state, done)
state = next_state
total_reward += reward
if len(agent.memory) > batch_size:
agent.replay(batch_size)
rewards.append(total_reward)
print("Episode: {}/{}, Reward: {}".format(episode+1, episodes, total_reward))
return rewards
if __name__ == "__main__":
env = gym.make('CartPole-v0')
state_size = env.observation_space.shape[0]
action_size = env.action_space.n
agent = DQNAgent(state_size, action_size)
episodes = 100
batch_size = 32
rewards = train(agent, env, episodes, batch_size)
print("Average reward over {} episodes: {}".format(episodes, sum(rewards)/episodes))
```
在训练过程中,每个 episode 结束后会打印出这个 episode 的迭代奖励值。最后会输出平均奖励值。
阅读全文