我试图在连续状态(昏暗= 20)和离散动作(3种可能的动作)的环境中找到最优策略.并且有一个特定的时刻:对于最优政策,一个动作(称之为"动作0")应该比其他两个更频繁地选择(大约100倍以上;这两个动作更有风险).
我尝试过使用NN值函数逼近的Q学习.结果相当糟糕:NN学会总是选择"动作0".我认为政策梯度方法(关于NN权重)可能有所帮助,但不了解如何在离散行动中使用它们.
你能给一些建议尝试一下吗?(也许算法,论文要读).当状态空间是连续的并且动作空间是离散的时,最先进的RL算法是什么?
谢谢.
我正在开发一个简单的Q-Learning实现而不是一个简单的应用程序,但有些东西让我感到困惑.
让我们考虑Q-Learning的标准表述
Q(S, A) = Q(S, A) + alpha * [R + MaxQ(S', A') - Q(S, A)]
Run Code Online (Sandbox Code Playgroud)
让我们假设这个状态K有两种可能的行为,既可以奖励我们的代理奖励R也可以R'通过A和A'.
如果我们遵循几乎完全贪婪的方法(假设我们假设0.1 epsilon),我将首先随机选择其中一个动作A.下一次,我可能(90%的时间)再次选择A,这将导致Q(K,A)不断增长和增长,即使偶然我尝试A',也可能是它的奖励是真实的情况在与A的相同程度上,我们将进入这样一种情况:在其余学习过程中,几乎不可能从我们的第一次猜测中"恢复".
我想这肯定不是这样,否则代理人基本上不会学习 - 它只是遵循一个简单的方法:像你第一次做的那样做.
我错过了什么吗?我知道我可以调整alpha值(通常,随着时间的推移减少它),但这绝不会改善我们的情况.
artificial-intelligence machine-learning reinforcement-learning q-learning
我想使用这个 q-learning(强化学习)代码。看起来代码是正确的,但我收到错误,我不知道为什么:
function q=ReinforcementLearning
clc;
format short;
format compact;
int state=0;
R= [-inf,-inf,-inf,-inf, 0,-inf;
-inf,-inf,-inf, 0,-inf, 100;
-inf,-inf,-inf, 0,-inf,-inf;
-inf, 0, 0,-inf, 0,-inf;
0,-inf,-inf, 0,-inf, 100;
-inf, 0,-inf,-inf, 0, 100];
gamma=0.8;
q=zero(size(R));
q1=one(size(R))*inf;
count=0;
for episode = 0:20000;
y=randperm(size(R,1));
state=y(1);
x=find(R(state,:)>=0);
if size(x,1)>0,
x1=RandomPermutation(x);
x1=x1(1);
end
qMax=max(q,[],2);
q(state,x1)=R(state,x1)+ gamma* qMax(x1);
int state=x1;
if sum(sum(abs(q1-q)))<0.0001 && sum(sum(q>0))
if count > 1000;
break
else
count=count+1;
end
else
q1=q;
count=0;
end
end
Run Code Online (Sandbox Code Playgroud)
但我收到以下警告和错误:
enter code here
Warning: The method char/int will be removed …Run Code Online (Sandbox Code Playgroud) 我试图建立一个利用强化学习的神经网络.我选择了scikit-neuralnetwork作为库(因为它很简单).看起来,两次装配会使Theano崩溃.
这是导致崩溃的最简单的代码(注意,它与哪些层无关,学习率或n_iter也不重要):
import numpy as np
from sknn.mlp import Classifier, Layer
clf = Classifier(
layers=[
Layer("Softmax")
],
learning_rate=0.001,
n_iter=1)
clf.fit(np.array([[0.]]), np.array([[0.]])) # Initialize the network for learning
X = np.array([[-1.], [1.]])
Y = np.array([[1.], [0.]])
clf.fit(X, Y) # crash
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
ValueError: Input dimension mis-match. (input[0].shape[1] = 2, input[1].shape[1] = 1)
Apply node that caused the error: Elemwise{Mul}[(0, 1)](y, LogSoftmax.0)
Toposort index: 12
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]
Inputs shapes: [(1L, 2L), (1L, 1L)]
Inputs strides: [(16L, 8L), (8L, 8L)] …Run Code Online (Sandbox Code Playgroud) 我想在CartPole-v0不打开渲染窗口的情况下访问 OpenAI 健身房环境中的原始像素。我该怎么做呢?
示例代码:
import gym
env = gym.make("CartPole-v0")
env.reset()
img = env.render(mode='rgb_array', close=True) # Returns None
print(img)
img = env.render(mode='rgb_array', close=False)
# Opens annoying window, but gives me the array that I want
print(img.shape)
Run Code Online (Sandbox Code Playgroud)
附注。我很难为 OpenAI 健身房找到好的文档。它只是我,还是根本不存在?
编辑:我不需要打开渲染视频。
我正在尝试实现一个 Q 学习代理来学习在井字游戏中与随机代理对战的最佳策略。
我制定了一个我相信会奏效的计划。只有一个部分我无法理解。这是因为环境中有两个玩家。
现在,Q Learning 代理应该对当前状态s采取行动,给定一些策略采取a的行动,给定动作的连续状态s',以及从该连续状态收到的任何奖励,r。
让我们把它放到一个元组中 (s, a, r, s')
现在通常一个代理会对它在给定动作中遇到的每个状态采取行动,并使用 Q 学习方程来更新前一个状态的值。
然而,由于 Tic Tac Toe 有两个玩家,我们可以将状态集分成两个。一组状态可以是学习代理开始采取行动的状态。另一组状态可以是对手开始行动的地方。
那么,我们是否需要将状态一分为二?或者学习代理是否需要更新游戏中访问的每个状态?
I feel as though it should probably be the latter, as this might affect updating Q Values for when the opponent wins the game.
Any help with this would be great, as there does not seem to be anything online that helps with my predicament.
[注意我正在使用xvfb-run -s "-screen 0 1400x900x24" jupyter notebook]
我尝试在OpenAI Gym中运行一组基本命令
import gym
env = gym.make("CartPole-v0")
obs = env.reset()
env.render()
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
...
~/Downloads/yes/lib/python3.7/site-packages/pyglet/gl/__init__.py in <module>()
225 else:
226 from .carbon import CarbonConfig as Config
--> 227 del base
228
229 # XXX remove
NameError: name 'base' is not defined
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决此问题?
我正在尝试使用 tf_agents 库通过批量学习来训练 DDPG 代理。但是,我需要定义一个 Observation_spec 和 action_spec,它们说明代理将接收到的张量的形状。我设法创建了可以提供数据的轨迹,但是这些轨迹和代理本身的形状不匹配
我尝试使用代理定义更改观察和操作规范。这是我的代理定义:
observation_spec = TensorSpec(shape = (1,),dtype = tf.float32)
time_step_spec = time_step.time_step_spec(observation_spec)
action_spec = BoundedTensorSpec([1],tf.float32,minimum = -100, maximum = 100)
actor_network = ActorNetwork(
input_tensor_spec=observation_spec,
output_tensor_spec=action_spec,
fc_layer_params=(100,200,100),
name="ddpg_ActorNetwork"
)
critic_net_input_specs = (observation_spec, action_spec)
critic_network = CriticNetwork(
input_tensor_spec=critic_net_input_specs,
observation_fc_layer_params=(200,100),
joint_fc_layer_params=(100,200),
action_fc_layer_params=None,
name="ddpg_CriticNetwork"
)
agent = ddpg_agent.DdpgAgent(
time_step_spec=time_step_spec,
action_spec=action_spec,
actor_network=actor_network,
critic_network=critic_network,
actor_optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
critic_optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
)
Run Code Online (Sandbox Code Playgroud)
这就是轨迹的样子
Trajectory(step_type=<tf.Variable 'Variable:0' shape=(1, 2) dtype=int32, numpy=array([[0, 1]], dtype=int32)>, observation=<tf.Variable 'Variable:0' shape=(1, 2) dtype=int32, numpy=array([[280, 280]], dtype=int32)>, action=<tf.Variable 'Variable:0' shape=(1, …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试从具有连续动作空间的 openAi 健身房环境中解决 Pendulum-v0。因此,我需要使用正态分布来对我的行为进行采样。我不明白的是 log_prob 使用时的维度:
import torch
from torch.distributions import Normal
means = torch.tensor([[0.0538],
[0.0651]])
stds = torch.tensor([[0.7865],
[0.7792]])
dist = Normal(means, stds)
a = torch.tensor([1.2,3.4])
d = dist.log_prob(a)
print(d.size())
Run Code Online (Sandbox Code Playgroud)
我期待一个大小为 2 的张量(每个动作一个 log_prob),但它输出一个大小为(2,2)的张量。
但是,当对离散环境使用分类分布时,log_prob 具有预期大小:
logits = torch.tensor([[-0.0657, -0.0949],
[-0.0586, -0.1007]])
dist = Categorical(logits = logits)
a = torch.tensor([1, 1])
print(dist.log_prob(a).size())
Run Code Online (Sandbox Code Playgroud)
给我一个大小为(2)的张量。
为什么正态分布的 log_prob 大小不同?
我已经为 Cartpole-VO 环境实现了 PPO。然而,它在游戏的某些迭代中并不收敛。有时它会陷入局部最优。我已经使用 TD-0 优势实现了该算法,即
A(s_t) = R(t+1) + \gamma V(S_{t+1}) - V(S_t)
这是我的代码:
def running_average(x, n):
N = n
kernel = np.ones(N)
conv_len = x.shape[0]-N
y = np.zeros(conv_len)
for i in range(conv_len):
y[i] = kernel @ x[i:i+N] # matrix multiplication operator: np.mul
y[i] /= N
return y
class ActorNetwork(nn.Module):
def __init__(self, state_dim, n_actions, learning_rate=0.0003, epsilon_clipping=0.3, update_epochs=10):
super().__init__()
self.n_actions = n_actions
self.model = nn.Sequential(
nn.Linear(state_dim, 64),
nn.ReLU(),
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, n_actions),
nn.Softmax(dim=-1)
).float()
self.optimizer = optim.Adam(self.model.parameters(), lr=learning_rate)
self.epsilon_clipping …Run Code Online (Sandbox Code Playgroud) python machine-learning reinforcement-learning pytorch policy-gradient-descent
python ×4
openai-gym ×2
pytorch ×2
q-learning ×2
matlab ×1
scikit-learn ×1
tensor ×1
tensorflow ×1
tic-tac-toe ×1