强化学习 | 强化学习基础知识(图解)

news/2024/5/18 22:27:25 标签: 人工智能, 强化学习, python

强化学习是机器学习的一个领域。它是关于在特定情况下采取适当的行动来最大化奖励。它被各种软件和机器用来找到在特定情况下应该采取的最佳行为或路径。强化学习与监督学习的不同之处在于,在监督学习中,训练数据具有答案键,因此模型本身使用正确答案进行训练,而在强化学习中,没有答案,但强化代理决定如何执行给定的任务。在没有训练数据集的情况下,它必然会从它的经验中学习。

强化学习(RL)是一门决策科学。它是关于学习环境中的最佳行为以获得最大的奖励。在 RL 中,数据是从使用试错法的机器学习系统中累积的。数据不是在有监督或无监督机器学习中找到的输入的一部分。

强化学习使用从结果中学习并决定下一步要采取的操作的算法。在每个操作之后,算法都会收到反馈,帮助它确定它所做的选择是正确的、中立的还是不正确的。对于必须在没有人工指导的情况下做出大量小决策的自动化系统来说,这是一个很好的技术。

强化学习是一种自主的自学系统,本质上是通过反复试验来学习的。它执行旨在最大化奖励的行动,或者换句话说,它是边做边学,以达到最佳结果。

1.理论基础

强化学习(Reinforcement Learning)的基本概念从马尔科夫决策过程(MDP)出发。MDP 是指在状态传播过程中遵循马可夫属性的过程。

2.强化学习算法:使用 Q 学习实现 Python

环境:jupyter lab

步骤 1:导入所需的库

python">#步骤 1:导入所需的库
import numpy as np 
import pylab as pl 
import networkx as nx 

步骤 2:定义和可视化图形

python"># 步骤 2:定义和可视化图形
edges = [(0, 1), (1, 5), (5, 6), (5, 4), (1, 2), 
		(1, 3), (9, 10), (2, 4), (0, 6), (6, 7), 
		(8, 9), (7, 8), (1, 7), (3, 9)] 

# 
goal = 10
G = nx.Graph() 
G.add_edges_from(edges) 
pos = nx.spring_layout(G) 
nx.draw_networkx_nodes(G, pos) 
nx.draw_networkx_edges(G, pos) 
nx.draw_networkx_labels(G, pos) 
pl.show() 

#上面的图表在代码的复制上可能看起来不一样,因为python中的networkx库从给定的边缘生成一个随机图。

步骤 3:为机器人定义系统的奖励

python"># 步骤 3:为机器人定义系统的奖励
MATRIX_SIZE = 11
M = np.matrix(np.ones(shape =(MATRIX_SIZE, MATRIX_SIZE))) 
M *= -1

for point in edges: 
	print(point) 
	if point[1] == goal: 
		M[point] = 100
	else: 
		M[point] = 0

	if point[0] == goal: 
		M[point[::-1]] = 100
	else: 
		M[point[::-1]]= 0
		# reverse of point 

M[goal, goal]= 100
print(M) 
# add goal point round trip 

步骤 4:定义一些要在训练中使用的实用程序函数

python"># 步骤 4:定义一些要在训练中使用的实用程序函数
Q = np.matrix(np.zeros([MATRIX_SIZE, MATRIX_SIZE])) 

gamma = 0.75
# learning parameter 
initial_state = 1

# Determines the available actions for a given state 
def available_actions(state): 
	current_state_row = M[state, ] 
	available_action = np.where(current_state_row >= 0)[1] 
	return available_action 

available_action = available_actions(initial_state) 

# Chooses one of the available actions at random 
def sample_next_action(available_actions_range): 
	next_action = int(np.random.choice(available_action, 1)) 
	return next_action 


action = sample_next_action(available_action) 

def update(current_state, action, gamma): 

    max_index = np.where(Q[action, ] == np.max(Q[action, ]))[1] 
    if max_index.shape[0] > 1: 
	    max_index = int(np.random.choice(max_index, size = 1)) 
    else: 
	    max_index = int(max_index) 
    max_value = Q[action, max_index] 
    Q[current_state, action] = M[current_state, action] + gamma * max_value 
    if (np.max(Q) > 0): 
	    return(np.sum(Q / np.max(Q)*100)) 
    else: 
	    return (0) 
# Updates the Q-Matrix according to the path chosen 

update(initial_state, action, gamma) 

步骤 5:使用 Q 矩阵训练和评估机器人

python"># 第 5 步:使用 Q 矩阵训练和评估机器人
scores = [] 
for i in range(1000): 
	current_state = np.random.randint(0, int(Q.shape[0])) 
	available_action = available_actions(current_state) 
	action = sample_next_action(available_action) 
	score = update(current_state, action, gamma) 
	scores.append(score) 

# print("Trained Q matrix:") 
# print(Q / np.max(Q)*100) 
# You can uncomment the above two lines to view the trained Q matrix 

# Testing 
current_state = 0
steps = [current_state] 

while current_state != 10: 

	next_step_index = np.where(Q[current_state, ] == np.max(Q[current_state, ]))[1] 
	if next_step_index.shape[0] > 1: 
		next_step_index = int(np.random.choice(next_step_index, size = 1)) 
	else: 
		next_step_index = int(next_step_index) 
	steps.append(next_step_index) 
	current_state = next_step_index 

print("Most efficient path:") 
print(steps) 

pl.plot(scores) 
pl.xlabel('No of iterations') 
pl.ylabel('Reward gained') 
pl.show() 

步骤 6 :使用环境线索定义和可视化新图形

python"># 第 6 步:使用环境线索定义和可视化新图形
# Defining the locations of the police and the drug traces 
police = [2, 4, 5] 
drug_traces = [3, 8, 9] 

G = nx.Graph() 
G.add_edges_from(edges) 
mapping = {0:'0 - Detective', 1:'1', 2:'2 - Police', 3:'3 - Drug traces', 
		4:'4 - Police', 5:'5 - Police', 6:'6', 7:'7', 8:'Drug traces', 
		9:'9 - Drug traces', 10:'10 - Drug racket location'} 

H = nx.relabel_nodes(G, mapping) 
pos = nx.spring_layout(H) 
#nx.draw_networkx_nodes(H, pos, node_size =[200, 200, 200, 200, 200, 200, 200, 200]) 
nx.draw_networkx_nodes(H, pos)
nx.draw_networkx_edges(H, pos) 
nx.draw_networkx_labels(H, pos) 
pl.show() 

上图可能看起来与上一张图略有不同,但实际上它们是相同的图表。这是由于networkx库随机放置节点。

步骤 7:为训练过程定义一些实用程序函数

python"># 步骤 7:为训练过程定义一些实用程序函数
Q = np.matrix(np.zeros([MATRIX_SIZE, MATRIX_SIZE])) 
env_police = np.matrix(np.zeros([MATRIX_SIZE, MATRIX_SIZE])) 
env_drugs = np.matrix(np.zeros([MATRIX_SIZE, MATRIX_SIZE])) 
initial_state = 1

# Same as above 
def available_actions(state): 
	current_state_row = M[state, ] 
	av_action = np.where(current_state_row >= 0)[1] 
	return av_action 

# Same as above 
def sample_next_action(available_actions_range): 
	next_action = int(np.random.choice(available_action, 1)) 
	return next_action 

# Exploring the environment 
def collect_environmental_data(action): 
	found = [] 
	if action in police: 
		found.append('p') 
	if action in drug_traces: 
		found.append('d') 
	return (found) 


available_action = available_actions(initial_state) 
action = sample_next_action(available_action) 

def update(current_state, action, gamma): 
    max_index = np.where(Q[action, ] == np.max(Q[action, ]))[1] 
    if max_index.shape[0] > 1: 
    	max_index = int(np.random.choice(max_index, size = 1)) 
    else: 
    	max_index = int(max_index) 
    max_value = Q[action, max_index] 
    Q[current_state, action] = M[current_state, action] + gamma * max_value 
    environment = collect_environmental_data(action) 
    if 'p' in environment: 
    	env_police[current_state, action] += 1
    if 'd' in environment: 
    	env_drugs[current_state, action] += 1
    if (np.max(Q) > 0): 
    	return(np.sum(Q / np.max(Q)*100)) 
    else: 
    	return (0) 
# Same as above 
update(initial_state, action, gamma) 

def available_actions_with_env_help(state): 
	current_state_row = M[state, ] 
	av_action = np.where(current_state_row >= 0)[1] 

	# if there are multiple routes, dis-favor anything negative 
	env_pos_row = env_matrix_snap[state, av_action] 

	if (np.sum(env_pos_row < 0)): 
		# can we remove the negative directions from av_act? 
		temp_av_action = av_action[np.array(env_pos_row)[0]>= 0] 
		if len(temp_av_action) > 0: 
			av_action = temp_av_action 
	return av_action 
# Determines the available actions according to the environment 

步骤 8:可视化环境矩阵

python"># 步骤 8:可视化环境矩阵
scores = [] 
for i in range(1000): 
	current_state = np.random.randint(0, int(Q.shape[0])) 
	available_action = available_actions(current_state) 
	action = sample_next_action(available_action) 
	score = update(current_state, action, gamma) 

# print environmental matrices 
print('Police Found') 
print(env_police) 
print('') 
print('Drug traces Found') 
print(env_drugs) 

步骤 9:训练和评估模型

python">scores = [] 
for i in range(1000): 
	current_state = np.random.randint(0, int(Q.shape[0])) 
	available_action = available_actions_with_env_help(current_state) 
	action = sample_next_action(available_action) 
	score = update(current_state, action, gamma) 
	scores.append(score) 

pl.plot(scores) 
pl.xlabel('Number of iterations') 
pl.ylabel('Reward gained') 
pl.show() 

参考文献

【1】Part 1: Key Concepts in RL — Spinning Up documentation (openai.com)

【2】 BartoSutton.pdf (cmu.edu)

【3】 Reinforcement learning - GeeksforGeeks

【4】ML | Reinforcement Learning Algorithm : Python Implementation using Q-learning - GeeksforGeeks 


http://www.niftyadmin.cn/n/5099206.html

相关文章

电机PID控制算法

电机的PID控制算法是一种广泛应用于工业控制中的经典控制算法。PID全称为比例-积分-微分控制算法&#xff0c;通过不断调节输出信号来使得被控对象&#xff08;电机&#xff09;的实际值接近期望值。 PID控制算法的原理如下&#xff1a; 比例&#xff08;Proportional&#x…

SNAP报错:No sigmaHHBand[0]

问题描述 使用snap想要直接反演哨兵1号的soil moisture时&#xff0c;提示缺少HH波段&#xff0c;而我去网上下载发现S1也没有这个波段数据提供。 原因 这需要Quad Pol Radarsat-2 SLC数据。双端口S1数据仅具有VV和VH。 Sentinel-1数据仅在双极化条件下采集。目前&#xff…

第三章 交换技术及应用

目录 3.1 port-vlan技术 3.1.1 VLAN概述 3.1.2 VLAN划分方法——Port-VLAN 3.1.3 Port-VLAN工作原理 3.1.3 Port-VLAN配置 3.2 port-vlan仿真演示 3.2.1 实验背景 3.2.2 实验目的 3.2.3 实验设备 3.2.4 实验步骤思维导图 3.3 tag-vlan技术 3.3.1 问题分析 3.3.2 T…

解决报错:RuntimeError: “LayerNormKernelImpl“ not implemented for ‘Half‘

文章目录 1. 为什么报错&#xff1f;2. 解决办法2.1 方法12.2 方法22.3 其他 1. 为什么报错&#xff1f; 一般发生在模型推理过程中&#xff0c;由于精度导致的报错&#xff0c;一些硬件和框架对于半精度操作的支持可能有限&#xff0c;导致无法执行特定的操作。 2. 解决办法…

14.10 Socket 套接字选择通信

对于网络通信中的服务端来说&#xff0c;显然不可能是一对一的&#xff0c;我们所希望的是服务端启用一份则可以选择性的与特定一个客户端通信&#xff0c;而当不需要与客户端通信时&#xff0c;则只需要将该套接字挂到链表中存储并等待后续操作&#xff0c;套接字服务端通过多…

手机应用app打开游戏显示连接服务器失败是什么原因?排查解决方案?

亲爱的同学们&#xff0c;有时候我们在使用手机设备时&#xff0c;可能会遇到一个很头疼的问题——连接服务器失败。这个问题不仅让我们感到困扰&#xff0c;还影响到了我们的用户体验。那么&#xff0c;我们究竟能如何解决这个问题呢&#xff1f;今天&#xff0c;笔者就和大家…

Python中的字典(Dictionary)学习

一、字典(Dictionary) 1、什么是 dict(字典) 上一章节,我们学习了列表(List) 和 元组(tuple) 来表示有序集合。 而我们在讲列表(list)的时候,我们用了列表(list) 来存储用户的姓名。 name = [一点水, 两点水, 三点水, 四点水, 五点水]那么如果我们为了方便联系…

服务器中了locked勒索病毒怎么办,勒索病毒解密,数据恢复

最近一段时间内&#xff0c;相信很多使用金蝶或用友的办公软件的企业&#xff0c;有很多都经历了locked勒索病毒的攻击&#xff0c;导致企业服务器被加密无法正常使用&#xff0c;严重影响了企业的正常工作。通过云天数据恢复中心的解密恢复发现&#xff0c;在今年locked勒索病…