0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

7个流行的强化学习算法及代码实现

Dbwd_Imgtec 来源:未知 2023-02-03 20:15 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

作者:Siddhartha Pramanik来源:DeepHub IMBA

目前流行的强化学习算法包括 Q-learning、SARSA、DDPG、A2C、PPO、DQN 和 TRPO。这些算法已被用于在游戏、机器人和决策制定等各种应用中,并且这些流行的算法还在不断发展和改进,本文我们将对其做一个简单的介绍。

f5048b68-a3bb-11ed-bfe3-dac502259ad0.png
1、Q-learningQ-learning:Q-learning 是一种无模型、非策略的强化学习算法。它使用 Bellman 方程估计最佳动作值函数,该方程迭代地更新给定状态动作对的估计值。Q-learning 以其简单性和处理大型连续状态空间的能力而闻名。下面是一个使用 Python 实现 Q-learning 的简单示例:
 import numpy as np
 
 # Define the Q-table and the learning rate
 Q = np.zeros((state_space_size, action_space_size))
 alpha = 0.1
 
 # Define the exploration rate and discount factor
 epsilon = 0.1
 gamma = 0.99
 
 for episode in range(num_episodes):
     current_state = initial_state
     while not done:
         # Choose an action using an epsilon-greedy policy
         if np.random.uniform(0, 1) < epsilon:
             action = np.random.randint(0, action_space_size)
         else:
             action = np.argmax(Q[current_state])
 
         # Take the action and observe the next state and reward
         next_state, reward, done = take_action(current_state, action)
 
         # Update the Q-table using the Bellman equation
         Q[current_state, action] = Q[current_state, action] + alpha * (reward + gamma * np.max(Q[next_state]) - Q[current_state, action])
 
         current_state = next_state

上面的示例中,state_space_size 和 action_space_size 分别是环境中的状态数和动作数。num_episodes 是要为运行算法的轮次数。initial_state 是环境的起始状态。take_action(current_state, action) 是一个函数,它将当前状态和一个动作作为输入,并返回下一个状态、奖励和一个指示轮次是否完成的布尔值。

在 while 循环中,使用 epsilon-greedy 策略根据当前状态选择一个动作。使用概率 epsilon选择一个随机动作,使用概率 1-epsilon选择对当前状态具有最高 Q 值的动作。采取行动后,观察下一个状态和奖励,使用Bellman方程更新q。并将当前状态更新为下一个状态。这只是 Q-learning 的一个简单示例,并未考虑 Q-table 的初始化和要解决的问题的具体细节。
2、SARSASARSA:SARSA 是一种无模型、基于策略的强化学习算法。它也使用Bellman方程来估计动作价值函数,但它是基于下一个动作的期望值,而不是像 Q-learning 中的最优动作。SARSA 以其处理随机动力学问题的能力而闻名。

	
 import numpy as np
 
 # Define the Q-table and the learning rate
 Q = np.zeros((state_space_size, action_space_size))
 alpha = 0.1
 
 # Define the exploration rate and discount factor
 epsilon = 0.1
 gamma = 0.99
 
 for episode in range(num_episodes):
     current_state = initial_state
     action = epsilon_greedy_policy(epsilon, Q, current_state)
     while not done:
         # Take the action and observe the next state and reward
         next_state, reward, done = take_action(current_state, action)
         # Choose next action using epsilon-greedy policy
         next_action = epsilon_greedy_policy(epsilon, Q, next_state)
         # Update the Q-table using the Bellman equation
         Q[current_state, action] = Q[current_state, action] + alpha * (reward + gamma * Q[next_state, next_action] - Q[current_state, action])
         current_state = next_state
         action = next_action

state_space_size和action_space_size分别是环境中的状态和操作的数量。num_episodes是您想要运行SARSA算法的轮次数。Initial_state是环境的初始状态。take_action(current_state, action)是一个将当前状态和作为操作输入的函数,并返回下一个状态、奖励和一个指示情节是否完成的布尔值。

在while循环中,使用在单独的函数epsilon_greedy_policy(epsilon, Q, current_state)中定义的epsilon-greedy策略来根据当前状态选择操作。使用概率 epsilon选择一个随机动作,使用概率 1-epsilon对当前状态具有最高 Q 值的动作。上面与Q-learning相同,但是采取了一个行动后,在观察下一个状态和奖励时它然后使用贪心策略选择下一个行动。并使用Bellman方程更新q表。
3、DDPGDDPG 是一种用于连续动作空间的无模型、非策略算法。它是一种actor-critic算法,其中actor网络用于选择动作,而critic网络用于评估动作。DDPG 对于机器人控制和其他连续控制任务特别有用。

	
 import numpy as np
 from keras.models import Model, Sequential
 from keras.layers import Dense, Input
 from keras.optimizers import Adam
 
 # Define the actor and critic models
 actor = Sequential()
 actor.add(Dense(32, input_dim=state_space_size, activation='relu'))
 actor.add(Dense(32, activation='relu'))
 actor.add(Dense(action_space_size, activation='tanh'))
 actor.compile(loss='mse', optimizer=Adam(lr=0.001))
 
 critic = Sequential()
 critic.add(Dense(32, input_dim=state_space_size, activation='relu'))
 critic.add(Dense(32, activation='relu'))
 critic.add(Dense(1, activation='linear'))
 critic.compile(loss='mse', optimizer=Adam(lr=0.001))
 
 # Define the replay buffer
 replay_buffer = []
 
 # Define the exploration noise
 exploration_noise = OrnsteinUhlenbeckProcess(size=action_space_size, theta=0.15, mu=0, sigma=0.2)
 
 for episode in range(num_episodes):
     current_state = initial_state
     while not done:
         # Select an action using the actor model and add exploration noise
         action = actor.predict(current_state)[0] + exploration_noise.sample()
         action = np.clip(action, -1, 1)
 
         # Take the action and observe the next state and reward
         next_state, reward, done = take_action(current_state, action)
 
         # Add the experience to the replay buffer
         replay_buffer.append((current_state, action, reward, next_state, done))
 
         # Sample a batch of experiences from the replay buffer
         batch = sample(replay_buffer, batch_size)
 
         # Update the critic model
         states = np.array([x[0] for x in batch])
         actions = np.array([x[1] for x in batch])
         rewards = np.array([x[2] for x in batch])
         next_states = np.array([x[3] for x in batch])
 
         target_q_values = rewards + gamma * critic.predict(next_states)
         critic.train_on_batch(states, target_q_values)
 
         # Update the actor model
         action_gradients = np.array(critic.get_gradients(states, actions))
         actor.train_on_batch(states, action_gradients)
 
         current_state = next_state
在本例中,state_space_size和action_space_size分别是环境中的状态和操作的数量。num_episodes是轮次数。Initial_state是环境的初始状态。Take_action (current_state, action)是一个函数,它接受当前状态和操作作为输入,并返回下一个操作。
4、A2CA2C(Advantage Actor-Critic)是一种有策略的actor-critic算法,它使用Advantage函数来更新策略。该算法实现简单,可以处理离散和连续的动作空间。

	
 import numpy as np
 from keras.models import Model, Sequential
 from keras.layers import Dense, Input
 from keras.optimizers import Adam
 from keras.utils import to_categorical
 
 # Define the actor and critic models
 state_input = Input(shape=(state_space_size,))
 actor = Dense(32, activation='relu')(state_input)
 actor = Dense(32, activation='relu')(actor)
 actor = Dense(action_space_size, activation='softmax')(actor)
 actor_model = Model(inputs=state_input, outputs=actor)
 actor_model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001))
 
 state_input = Input(shape=(state_space_size,))
 critic = Dense(32, activation='relu')(state_input)
 critic = Dense(32, activation='relu')(critic)
 critic = Dense(1, activation='linear')(critic)
 critic_model = Model(inputs=state_input, outputs=critic)
 critic_model.compile(loss='mse', optimizer=Adam(lr=0.001))
 
 for episode in range(num_episodes):
     current_state = initial_state
     done = False
     while not done:
         # Select an action using the actor model and add exploration noise
         action_probs = actor_model.predict(np.array([current_state]))[0]
         action = np.random.choice(range(action_space_size), p=action_probs)
 
         # Take the action and observe the next state and reward
         next_state, reward, done = take_action(current_state, action)
 
         # Calculate the advantage
         target_value = critic_model.predict(np.array([next_state]))[0][0]
         advantage = reward + gamma * target_value - critic_model.predict(np.array([current_state]))[0][0]
 
         # Update the actor model
         action_one_hot = to_categorical(action, action_space_size)
         actor_model.train_on_batch(np.array([current_state]), advantage * action_one_hot)
 
         # Update the critic model
         critic_model.train_on_batch(np.array([current_state]), reward + gamma * target_value)
 
         current_state = next_state
在这个例子中,actor模型是一个神经网络,它有2个隐藏层,每个隐藏层有32个神经元,具有relu激活函数,输出层具有softmax激活函数。critic模型也是一个神经网络,它有2个隐含层,每层32个神经元,具有relu激活函数,输出层具有线性激活函数。使用分类交叉熵损失函数训练actor模型,使用均方误差损失函数训练critic模型。动作是根据actor模型预测选择的,并添加了用于探索的噪声。
5、PPOPPO(Proximal Policy Optimization)是一种策略算法,它使用信任域优化的方法来更新策略。它在具有高维观察和连续动作空间的环境中特别有用。PPO 以其稳定性和高样品效率而著称。

	
 import numpy as np
 from keras.models import Model, Sequential
 from keras.layers import Dense, Input
 from keras.optimizers import Adam
 
 # Define the policy model
 state_input = Input(shape=(state_space_size,))
 policy = Dense(32, activation='relu')(state_input)
 policy = Dense(32, activation='relu')(policy)
 policy = Dense(action_space_size, activation='softmax')(policy)
 policy_model = Model(inputs=state_input, outputs=policy)
 
 # Define the value model
 value_model = Model(inputs=state_input, outputs=Dense(1, activation='linear')(policy))
 
 # Define the optimizer
 optimizer = Adam(lr=0.001)
 
 for episode in range(num_episodes):
     current_state = initial_state
     while not done:
         # Select an action using the policy model
         action_probs = policy_model.predict(np.array([current_state]))[0]
         action = np.random.choice(range(action_space_size), p=action_probs)
 
         # Take the action and observe the next state and reward
         next_state, reward, done = take_action(current_state, action)
 
         # Calculate the advantage
         target_value = value_model.predict(np.array([next_state]))[0][0]
         advantage = reward + gamma * target_value - value_model.predict(np.array([current_state]))[0][0]
 
         # Calculate the old and new policy probabilities
         old_policy_prob = action_probs[action]
         new_policy_prob = policy_model.predict(np.array([next_state]))[0][action]
 
         # Calculate the ratio and the surrogate loss
         ratio = new_policy_prob / old_policy_prob
         surrogate_loss = np.minimum(ratio * advantage, np.clip(ratio, 1 - epsilon, 1 + epsilon) * advantage)
 
         # Update the policy and value models
         policy_model.trainable_weights = value_model.trainable_weights
         policy_model.compile(optimizer=optimizer, loss=-surrogate_loss)
         policy_model.train_on_batch(np.array([current_state]), np.array([action_one_hot]))
         value_model.train_on_batch(np.array([current_state]), reward + gamma * target_value)
 
         current_state = next_state

6、DQNDQN(深度 Q 网络)是一种无模型、非策略算法,它使用神经网络来逼近 Q 函数。DQN 特别适用于 Atari 游戏和其他类似问题,其中状态空间是高维的,并使用神经网络近似 Q 函数。
 import numpy as np
 from keras.models import Sequential
 from keras.layers import Dense, Input
 from keras.optimizers import Adam
 from collections import deque
 
 # Define the Q-network model
 model = Sequential()
 model.add(Dense(32, input_dim=state_space_size, activation='relu'))
 model.add(Dense(32, activation='relu'))
 model.add(Dense(action_space_size, activation='linear'))
 model.compile(loss='mse', optimizer=Adam(lr=0.001))
 
 # Define the replay buffer
 replay_buffer = deque(maxlen=replay_buffer_size)
 
 for episode in range(num_episodes):
     current_state = initial_state
     while not done:
         # Select an action using an epsilon-greedy policy
         if np.random.rand() < epsilon:
             action = np.random.randint(0, action_space_size)
         else:
             action = np.argmax(model.predict(np.array([current_state]))[0])
 
         # Take the action and observe the next state and reward
         next_state, reward, done = take_action(current_state, action)
 
         # Add the experience to the replay buffer
         replay_buffer.append((current_state, action, reward, next_state, done))
 
         # Sample a batch of experiences from the replay buffer
         batch = random.sample(replay_buffer, batch_size)
 
         # Prepare the inputs and targets for the Q-network
         inputs = np.array([x[0] for x in batch])
         targets = model.predict(inputs)
         for i, (state, action, reward, next_state, done) in enumerate(batch):
             if done:
                 targets[i, action] = reward
             else:
                 targets[i, action] = reward + gamma * np.max(model.predict(np.array([next_state]))[0])
 
         # Update the Q-network
         model.train_on_batch(inputs, targets)
 
         current_state = next_state
上面的代码,Q-network有2个隐藏层,每个隐藏层有32个神经元,使用relu激活函数。该网络使用均方误差损失函数和Adam优化器进行训练。
7、TRPOTRPO (Trust Region Policy Optimization)是一种无模型的策略算法,它使用信任域优化方法来更新策略。它在具有高维观察和连续动作空间的环境中特别有用。TRPO 是一个复杂的算法,需要多个步骤和组件来实现。TRPO不是用几行代码就能实现的简单算法。所以我们这里使用实现了TRPO的现有库,例如OpenAI Baselines,它提供了包括TRPO在内的各种预先实现的强化学习算法,。要在OpenAI Baselines中使用TRPO,我们需要安装:

	
 pip install baselines
然后可以使用baselines库中的trpo_mpi模块在你的环境中训练TRPO代理,这里有一个简单的例子:
 import gym
 from baselines.common.vec_env.dummy_vec_env import DummyVecEnv
 from baselines.trpo_mpi import trpo_mpi
 
 #Initialize the environment
 env = gym.make("CartPole-v1")
 env = DummyVecEnv([lambda: env])
 
 # Define the policy network
 policy_fn = mlp_policy
 
 #Train the TRPO model
 model = trpo_mpi.learn(env, policy_fn, max_iters=1000)
我们使用Gym库初始化环境。然后定义策略网络,并调用TRPO模块中的learn()函数来训练模型。还有许多其他库也提供了TRPO的实现,例如TensorFlow、PyTorch和RLLib。下面时一个使用TF 2.0实现的样例
 import tensorflow as tf
 import gym
 
 # Define the policy network
 class PolicyNetwork(tf.keras.Model):
     def __init__(self):
         super(PolicyNetwork, self).__init__()
         self.dense1 = tf.keras.layers.Dense(16, activation='relu')
         self.dense2 = tf.keras.layers.Dense(16, activation='relu')
         self.dense3 = tf.keras.layers.Dense(1, activation='sigmoid')
 
     def call(self, inputs):
         x = self.dense1(inputs)
         x = self.dense2(x)
         x = self.dense3(x)
         return x
 
 # Initialize the environment
 env = gym.make("CartPole-v1")
 
 # Initialize the policy network
 policy_network = PolicyNetwork()
 
 # Define the optimizer
 optimizer = tf.optimizers.Adam()
 
 # Define the loss function
 loss_fn = tf.losses.BinaryCrossentropy()
 
 # Set the maximum number of iterations
 max_iters = 1000
 
 # Start the training loop
 for i in range(max_iters):
     # Sample an action from the policy network
     action = tf.squeeze(tf.random.categorical(policy_network(observation), 1))
 
     # Take a step in the environment
     observation, reward, done, _ = env.step(action)
 
     with tf.GradientTape() as tape:
         # Compute the loss
         loss = loss_fn(reward, policy_network(observation))
 
     # Compute the gradients
     grads = tape.gradient(loss, policy_network.trainable_variables)
 
     # Perform the update step
     optimizer.apply_gradients(zip(grads, policy_network.trainable_variables))
 
     if done:
         # Reset the environment
         observation = env.reset()
在这个例子中,我们首先使用TensorFlow的Keras API定义一个策略网络。然后使用Gym库和策略网络初始化环境。然后定义用于训练策略网络的优化器和损失函数。在训练循环中,从策略网络中采样一个动作,在环境中前进一步,然后使用TensorFlow的GradientTape计算损失和梯度。然后我们使用优化器执行更新步骤。这是一个简单的例子,只展示了如何在TensorFlow 2.0中实现TRPO。TRPO是一个非常复杂的算法,这个例子没有涵盖所有的细节,但它是试验TRPO的一个很好的起点。
总结

以上就是我们总结的7个常用的强化学习算法,这些算法并不相互排斥,通常与其他技术(如值函数逼近、基于模型的方法和集成方法)结合使用,可以获得更好的结果。

END

欢迎加入Imagination GPU人工智能交流2群f5173222-a3bb-11ed-bfe3-dac502259ad0.jpg入群请加小编微信:eetrend89

(添加请备注公司名和职称)

推荐阅读 对话Imagination中国区董事长:以GPU为支点加强软硬件协同,助力数字化转型

手机芯片这个功能,有望改变市场格局!

Imagination Technologies是一家总部位于英国的公司,致力于研发芯片和软件知识产权(IP),基于Imagination IP的产品已在全球数十亿人的电话、汽车、家庭和工作 场所中使用。获取更多物联网、智能穿戴、通信汽车电子、图形图像开发等前沿技术信息,欢迎关注 Imagination Tech!


原文标题:7个流行的强化学习算法及代码实现

文章出处:【微信公众号:Imagination Tech】欢迎添加关注!文章转载请注明出处。


声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • imagination
    +关注

    关注

    1

    文章

    627

    浏览量

    63572

原文标题:7个流行的强化学习算法及代码实现

文章出处:【微信号:Imgtec,微信公众号:Imagination Tech】欢迎添加关注!文章转载请注明出处。

收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    在阿里云PAI平台的机器人感知强化学习规模化实践

    物理 AI 正在迅速从基础运动控制迈向更复杂的环境理解。传统机器人强化学习(RL)长期依赖本体感知(proprioception),包括关节角度、力矩反馈和内部状态,来训练灵巧的运动技能。
    的头像 发表于 05-18 10:34 432次阅读
    在阿里云PAI平台的机器人感知<b class='flag-5'>强化学习</b>规模化实践

    NVIDIA与Ineffable Intelligence合作打造强化学习基础设施

    我们正在共同构建能够解锁全新水平智能的强化学习基础设施。
    的头像 发表于 05-18 10:30 500次阅读

    Momenta R7强化学习世界模型实现量产首发

    等话题展开深度对话,正式宣布Momenta R7强化学习世界模型实现量产首发,标志着智能驾驶从“看见世界”到“理解世界”的全新跨越,物理AI正式从技术理念走向规模化量产落地。
    的头像 发表于 04-29 15:44 849次阅读

    Momenta R7强化学习世界模型助力上汽大众ID. ERA 9X正式上市

    2026年4月25日,上汽大众全新旗舰SUV ID. ERA 9X于2026北京国际汽车展览会期间正式上市,并将全球首发搭载Momenta R7强化学习世界模型。这意味着Momenta R7率先在全球
    的头像 发表于 04-29 15:42 768次阅读

    上汽奥迪E5 Sportback车型升级搭载全新Momenta强化学习大模型

    近日,上汽奥迪宣布旗下 E5 Sportback 车型升级搭载 全新Momenta 强化学习大模型。
    的头像 发表于 04-09 09:33 348次阅读

    上汽大众ID. ERA 9X全球首发搭载Momenta R7强化学习世界模型

    3月30日,Momenta R7强化学习世界模型全球首发搭载车型——上汽大众ID. ERA 9X正式开启预售。
    的头像 发表于 03-31 13:48 538次阅读

    蚂蚁集团全模态代码算法团队自研OpAgent技术框架

    为应对真实 Web 环境的非结构化复杂性、时序不稳定性与交互隐式逻辑等挑战,蚂蚁集团全模态代码算法团队提出了一套结合了多任务微调、在线强化学习与模块化协作的综合解决方案:OpAgent。
    的头像 发表于 03-18 17:13 1129次阅读
    蚂蚁集团全模态<b class='flag-5'>代码</b><b class='flag-5'>算法</b>团队自研OpAgent技术框架

    Momenta R7强化学习世界模型即将推出

    3月16日,上汽大众举办以“人本科技”为主题的ID. ERA技术发布会,首次揭晓了ID. ERA 系列包括智能辅助驾驶在内的诸多核心技术亮点。会上,Momenta CEO曹旭东正式宣布:Momenta R7强化学习世界模型即将推出,并将全球首发搭载于上汽大众全新旗舰SUV
    的头像 发表于 03-17 13:57 1396次阅读

    自动驾驶中常提的离线强化学习是什么?

    [首发于智驾最前沿微信公众号]在之前谈及自动驾驶模型学习时,详细聊过强化学习的作用,由于强化学习能让大模型通过交互学到策略,不需要固定的规则,从而给自动驾驶的落地创造了更多可能。 强化学习
    的头像 发表于 02-07 09:21 452次阅读
    自动驾驶中常提的离线<b class='flag-5'>强化学习</b>是什么?

    强化学习会让自动驾驶模型学习更快吗?

    [首发于智驾最前沿微信公众号]在谈及自动驾驶大模型训练时,有的技术方案会采用模仿学习,而有些会采用强化学习。同样作为大模型的训练方式,强化学习有何不同?又有什么特点呢? 什么是强化学习
    的头像 发表于 01-31 09:34 955次阅读
    <b class='flag-5'>强化学习</b>会让自动驾驶模型<b class='flag-5'>学习</b>更快吗?

    多智能体强化学习(MARL)核心概念与算法概览

    训练单个RL智能体的过程非常简单,那么我们现在换一场景,同时训练五智能体,而且每个都有自己的目标、只能看到部分信息,还能互相帮忙。这就是多智能体强化学习
    的头像 发表于 01-21 16:21 414次阅读
    多智能体<b class='flag-5'>强化学习</b>(MARL)核心概念与<b class='flag-5'>算法</b>概览

    上汽别克至境E7首发搭载Momenta R6强化学习大模型

    别克至境家族迎来新成员——大五座智能SUV别克至境E7首发。新车将搭载Momenta R6强化学习大模型,带来全场景的智能出行体验。
    的头像 发表于 01-12 16:23 609次阅读

    今日看点:智元推出真机强化学习;美国软件公司SAS退出中国市场

    智元推出真机强化学习,机器人训练周期从“数周”减至“数十分钟”   近日,智元机器人宣布其研发的真机强化学习技术,已在与龙旗科技合作的验证产线中成功落地。据介绍,此次落地的真机强化学习方案,机器人
    发表于 11-05 09:44 1205次阅读

    自动驾驶中常提的“强化学习”是啥?

    下,就是一智能体在环境里行动,它能观察到环境的一些信息,并做出一动作,然后环境会给出一反馈(奖励或惩罚),智能体的目标是把长期得到的奖励累积到最大。和监督学习不同,
    的头像 发表于 10-23 09:00 1036次阅读
    自动驾驶中常提的“<b class='flag-5'>强化学习</b>”是<b class='flag-5'>个</b>啥?

    NVIDIA Isaac Lab可用环境与强化学习脚本使用指南

    Lab 是一适用于机器人学习的开源模块化框架,其模块化高保真仿真适用于各种训练环境,Isaac Lab 同时支持模仿学习(模仿人类)和强化学习(在尝试和错误中进行
    的头像 发表于 07-14 15:29 2831次阅读
    NVIDIA Isaac Lab可用环境与<b class='flag-5'>强化学习</b>脚本使用指南