Actor是一个神经网络
Critic也是一个神经网络,他们是不同的神经网络,Actor用于预测行为的概率,Critic是预测在这个状态下的价值。
结合了Policy Gradient(Actor)和Function Approximation(Critic)的方法,Actor基于概率选行为,Critic(可以用Q-learning或者Value-based)估计每一个状态的价值,用这个状态的价值减去下个状态的价值,(TD-error),Critic就告诉actor下一个动作要被加大更新的,TD-error如果是正的,下个动作就要加大更新,如果是负的,就减小actor的更新幅度,Criric基于Actor的行为评判行为的得分,Actor根据Critic的评分修改选行为的概率。
总结:
(1)演员(Actor)是指策略函数π θ(a∣s),即学习一个策略来得到尽量高的回报。
(2)评论家(Critic)是指值函数 V π(s),对当前策略的值函数进行估计,即评估演员的好坏。
(3)借助于值函数,演员-评论家算法可以进行单步更新参数,不需要等到回合结束才进行更新。
1.优点:Actor-Critic是由Policy Gradients和Value-Based组成的,critic通过学习环境和奖惩的关系能看到所处的状态潜在的奖励,所以来指点actor使actor每一步都在更新,如果使用Policy Gradients,actor只能等到一个回合才能更新。
因此:可以进行单步更新,比传统的Policy Gradient要快。
2.缺点:
(1)Actor— Critic涉及到两个神经网络,每次都在连续状态中更新参数,每次参数更新的前后都存在相关性,导致了神经网络只能片面的看待问题,甚至学不到东西,
(2)Actor的行为取决于Critic的Value,但是因为Critic本来就很难收敛,和Actor一起更新的话就更难收敛了(为了解决收敛问题,后又提出了DDPG)
1.Actor
Actor用于预测行为的概率。输入是现在的状态state,选择生成出各种行为的价值,(左60%,右40%),根据概率来选择行为(选择60%的),输出概率,然后优化行为(TD-error),
2.Critic
输入状态state,输出状态的值。
伪代码如下:
代码主要分为两部分,第一部分是Actor,第二部分是Critic。对于Actor部分,大家可以和上一篇策略梯度的代码对比,改动并不大,主要区别在于梯度更新部分,策略梯度使用是蒙特卡罗法计算出的价值v(t)v(t),则我们的actor使用的是TD误差。
在策略梯度部分,对应的位置如下:
self.loss = tf.reduce_mean(self.neg_log_prob * self.tf_vt) # reward guided loss
而我们的Actor对应的位置的代码是:
self.exp = tf.reduce_mean(self.neg_log_prob * self.td_error)
此处要注意的是,由于使用的是TD误差,而不是价值v(t)v(t),此处需要最大化self.exp,而不是最小化它,这点和策略梯度不同。对应的Actor代码为:
#这里需要最大化当前策略的价值,因此需要最大化self.exp,即最小化-self.exp
self.train_op = tf.train.AdamOptimizer(LEARNING_RATE).minimize(-self.exp)
对于Critic部分,我们使用了类似于DQN的三层神经网络。不过我们简化了这个网络的输出,只有一维输出值,而不是之前DQN使用的有多少个可选动作,就有多少维输出值。网络结构如下:
def create_Q_network(self):
# network weights
W1q = self.weight_variable([self.state_dim, 20])
b1q = self.bias_variable([20])
W2q = self.weight_variable([20, 1])
b2q = self.bias_variable([1])
self.state_input = tf.placeholder(tf.float32, [1, self.state_dim], "state")
# hidden layers
h_layerq = tf.nn.relu(tf.matmul(self.state_input, W1q) + b1q)
# Q Value layer
self.Q_value = tf.matmul(h_layerq, W2q) + b2q
对于算法中Actor和Critic交互的逻辑,在main函数中:
for step in range(STEP):
action = actor.choose_action(state) # e-greedy action for train
next_state,reward,done,_ = env.step(action)
td_error = critic.train_Q_network(state, reward, next_state) # gradient = grad[r + gamma * V(s_) - V(s)]
actor.learn(state, action, td_error) # true_gradient = grad[logPi(s,a) * td_error]
state = next_state
if done:
break
来自莫烦PYTHON
import numpy as np
import tensorflow as tf
import gym
np.random.seed(2)
tf.set_random_seed(2) # reproducible
# 超参数
OUTPUT_GRAPH = True
MAX_EPISODE = 3000
DISPLAY_REWARD_THRESHOLD = 200 # 刷新阈值
MAX_EP_STEPS = 1000 # 最大迭代次数
RENDER = False # 渲染开关
GAMMA = 0.9 # 衰变值
LR_A = 0.001 # Actor学习率
LR_C = 0.01 # Critic学习率
env = gym.make('CartPole-v0')
env.seed(1)
env = env.unwrapped # 为环境增加限制,对训练有利
N_F = env.observation_space.shape[0] # 状态空间
N_A = env.action_space.n # 动作空间
class Actor(object):
def __init__(self, sess, n_features, n_actions, lr=0.001):
self.sess = sess
self.s = tf.placeholder(tf.float32, [1, n_features], "state")
self.a = tf.placeholder(tf.int32, None, "act")
self.td_error = tf.placeholder(tf.float32, None, "td_error") # TD_error应该加大幅度或者减小幅度
with tf.variable_scope('Actor'): # 指定变量的作用域
l1 = tf.layers.dense( # 全连接层
inputs=self.s,
units=20, # number of hidden units
activation=tf.nn.relu,
kernel_initializer=tf.random_normal_initializer(0., .1), # 初始化weights,均值为0,方差为1
bias_initializer=tf.constant_initializer(0.1), # biases
name='l1'
)
self.acts_prob = tf.layers.dense(
inputs=l1,
units=n_actions, # output units
activation=tf.nn.softmax, # get action probabilities,输出概率
kernel_initializer=tf.random_normal_initializer(0., .1), # weights
bias_initializer=tf.constant_initializer(0.1), # biases
name='acts_prob'
)
with tf.variable_scope('exp_v'): # 预计的价值函数
log_prob = tf.log(self.acts_prob[0, self.a]) # 计算loss,将动作的可能性放大或是缩小,log动作概率
self.exp_v = tf.reduce_mean(log_prob * self.td_error) # advantage (TD_error) guided loss,,exp表示预计的价值,log概率*TD方向
with tf.variable_scope('train'):
# 不断增加exp_v(动作带来的额外价值)
self.train_op = tf.train.AdamOptimizer(lr).minimize(-self.exp_v) # minimize(-exp_v) = maximize(exp_v)最大化预计价值
def learn(self, s, a, td):
s = s[np.newaxis, :] # 新增维度
feed_dict = {self.s: s, self.a: a, self.td_error: td} # TF的一种输入方式
_, exp_v = self.sess.run([self.train_op, self.exp_v], feed_dict)
return exp_v
def choose_action(self, s):
s = s[np.newaxis, :]
probs = self.sess.run(self.acts_prob, {self.s: s}) # 获取所有操作的概率
return np.random.choice(np.arange(probs.shape[1]), p=probs.ravel()) # return a int,返回按照一定概率选择的动作
class Critic(object):
def __init__(self, sess, n_features, lr=0.01):
self.sess = sess
self.s = tf.placeholder(tf.float32, [1, n_features], "state")
self.v_ = tf.placeholder(tf.float32, [1, 1], "v_next")
self.r = tf.placeholder(tf.float32, None, 'r')
with tf.variable_scope('Critic'):
l1 = tf.layers.dense(
inputs=self.s,
units=20, # number of hidden units
activation=tf.nn.relu, # None
# have to be linear to make sure the convergence of actor.
# But linear approximator seems hardly learns the correct Q.
kernel_initializer=tf.random_normal_initializer(0., .1), # weights
bias_initializer=tf.constant_initializer(0.1), # biases
name='l1'
)
self.v = tf.layers.dense(
inputs=l1,
units=1, # output units
activation=None,
kernel_initializer=tf.random_normal_initializer(0., .1), # weights
bias_initializer=tf.constant_initializer(0.1), # biases
name='V'
)
# 就想Q-learning那样更新TD-error就可以
with tf.variable_scope('squared_TD_error'):
self.td_error = self.r + GAMMA * self.v_ - self.v # critic得出的评分
self.loss = tf.square(self.td_error) # TD_error = (r+gamma*V_next) - V_eval,TD-error=V现实-V估计
with tf.variable_scope('train'):
self.train_op = tf.train.AdamOptimizer(lr).minimize(self.loss) # 最小化损失函数
def learn(self, s, r, s_):
s, s_ = s[np.newaxis, :], s_[np.newaxis, :]
v_ = self.sess.run(self.v, {self.s: s_})
td_error, _ = self.sess.run([self.td_error, self.train_op],
{self.s: s, self.v_: v_, self.r: r})
return td_error
sess = tf.Session()
# 定义Actor,Critic
actor = Actor(sess, n_features=N_F, n_actions=N_A, lr=LR_A) # 初始化Actor
critic = Critic(sess, n_features=N_F, lr=LR_C) # 初始化Critic,critic学习的数据会为actor作指导,所以LR_C会大一些
sess.run(tf.global_variables_initializer()) # 初始化参数
if OUTPUT_GRAPH:
tf.summary.FileWriter("logs/", sess.graph) # 输出日志
# 开始迭代过程 对应伪代码部分
for i_episode in range(MAX_EPISODE):
s = env.reset() # 环境初始化
t = 0
track_r = [] # 每回合的所有奖励,置空
while True: # 回合中每一步
if RENDER: env.render()
a = actor.choose_action(s) # Actor选取动作
s_, r, done, info = env.step(a) # 环境反馈
if done: r = -20 # 回合结束的惩罚
track_r.append(r) # 记录回报值r
td_error = critic.learn(s, r, s_) # Critic 学习,返回TD-error
actor.learn(s, a, td_error) # Actor 学习
s = s_
t += 1
if done or t >= MAX_EP_STEPS:
# 回合结束, 打印回合累积奖励
ep_rs_sum = sum(track_r)
if 'running_reward' not in globals():
running_reward = ep_rs_sum
else:
running_reward = running_reward * 0.95 + ep_rs_sum * 0.05
if running_reward > DISPLAY_REWARD_THRESHOLD: RENDER = True # rendering
print("episode:", i_episode, " reward:", int(running_reward))
break
参考链接;
1.actor-critic
https://github.com/datawhalechina/leedeeprl-notes
https://blog.csdn.net/qq_30615903/article/details/80774384?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522161033115816780265418113%252522%25252C%252522scm%252522%25253A%25252220140713.130102334…%252522%25257D&request_id=161033115816780265418113&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allbaidu_landing_v2~default-1-80774384.pc_search_result_no_baidu_js&utm_term=actor%20critic%E6%9B%B4%E6%96%B0%E5%85%AC%E5%BC%8F
https://blog.csdn.net/rufanchen_/article/details/95093958?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522161032889916780262561436%252522%25252C%252522scm%252522%25253A%25252220140713.130102334…%252522%25257D&request_id=161032889916780262561436&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_click~default-1-95093958.pc_search_result_no_baidu_js&utm_term=actor-critic