import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import numpy as np
import gym
# Hyper Parameters
BATCH_SIZE = 32
LR = 0.01 # learning rate
EPSILON = 0.9 # greedy policy
GAMMA = 0.9 # reward discount
TARGET_REPLACE_ITER = 100 # target update frequency
MEMORY_CAPACITY = 2000
env = gym.make('CartPole-v0')
env = env.unwrapped
N_ACTIONS = env.action_space.n
N_STATES = env.observation_space.shape[0]
class Net(nn.Module):
def __init__(self,):
super(Net,self).__init__()
self.fc1 = nn.Linear(N_STATES,10)
self.fc1.weight.data.normal_(0,0.1) # initialization
self.out = nn.Linear(10,N_STATES)
self.out.weight.data.normal_(0,0.1) # initialization
def forward(self,x):
x = self.fc1(x)
x = F.relu(x)
actions_value = self.out(x)
return actions_value
class DQN(object):
def __init__(self):
self.eval_net = Net()
self.target_net = Net()
self.learn_step_counter = 0 # for target updating
self.memory_counter = 0 # for storing memory
self.memory = np.zeros((MEMORY_CAPACITY,N_STATES*2+2)) # initailize memory
self.optimizer = torch.optim.Adam(self.eval_net.parameters(),lr=LR)
self.loss_func = nn.MSELoss()
def choose_action(self,x):
x = Variable(torch.unsqueeze(torch.FloatTensor(x),0))
if np.random.uniform() < EPSILON: # greedy
actions_value = self.eval_net.forward(x)
action = torch.max(actions_value,1)[1].data.numpy()[0,0]
else:
action = np.random.randint(0,N_ACTIONS)
return action
def store_transition(self,s,a,r,s_):
transition = np.hstack((s,[a,r],s_))
# replace the old memory with new memory
index = self.memory_counter % MEMORY_CAPACITY
self.memory[index,:] = transition
self.memory_counter += 1
def learn(self):
# target net update
if self.learn_step_counter % TARGET_REPLACE_ITER == 0:
self.target_net.load_state_dict(self.eval_net.state_dict())
sample_index = np.random.choice(MEMORY_CAPACITY,BATCH_SIZE)
b_memory = self.memory[sample_index,:]
b_s = Variable(torch.FloatTensor(b_memory[:,:N_STATES]))
b_a = Variable(torch.LongTensor(b_memory[:,N_STATES:N_STATES+1].astype(int)))
b_r = Variable(torch.FloatTensor(b_memory[:,N_STATES+1:N_STATES+2]))
b_s_ = Variable(torch.FloatTensor(b_memory[:,-N_STATES:]))
q_eval = self.eval_net(b_s).gather(1,b_a)
q_next = self.target_net(b_s_).detach()
q_target = b_r + GAMMA * q_next.max(1)[0]
loss = self.loss_func(q_eval,q_target)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
dqn = DQN()
print('\nCollecting experience...')
for i_episode in range(400):
s = env.reset()
while True:
env.render()
a = dqn.choose_action(s)
# take action
s_,r,done,info = env.step(a)
# modify the reward
x,x_dot,theta,theta_dot = s_
r1 = (env.x_threshold - abs(x)) / env.x_threshold - 0.8
r2 = (env.theta_threshold_radians - abs(theta)) / env.theta_threshold_radians - 0.5
r = r1 + r2
dqn.store_transition(s,a,r,s_)
if dqn.memory_counter > MEMORY_CAPACITY:
dqn.learn()
if done:
break
s = s_
pytorch DQN强化学习
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Preface 这里开始强化学习的理论内容。虽然比较简单,但是强化学习的入门基础。在有监督学习里面,我们有明确的目...