4

所以我一直在阅读关于 Q 学习和神经网络的文章。我相信我对此有正确的想法,但是我想对我的 NN 代码和 Q 值更新有第二意见。

我已经创建了山地车问题的 MatLab 实现和我的神经网络,我正在为 NN 部分使用神经网络工具箱。

它是一个包含 2 个输入、5-20 个隐藏(用于实验)和 3 个输出(对应于山地车中的动作)的网络

隐藏单元设置为 tansig,输出为 purelin,训练函数为 traingdm

这是正确的步骤吗?

  1. 获得初始状态 s -> [-0.5; 0.0]
  2. 使用 Qs=net(s) 运行网络……这给了我一个 1x3 Q 值矩阵,对应于初始状态 s 中的每个动作。
  3. 使用 e-greedy selection 选择一个动作
  4. 模拟山地车,得到s'(执行动作a的新状态)
  5. 使用 Qs_prime=net(s') 运行网络以获得 s' 的 Q 值的另一个矩阵

现在我不确定这是否正确,因为我必须弄清楚如何正确更新 NN 的权重。

  1. 计算 QTarget,即 =reward+gamma* 来自 s' 的最大 Q 值?
  2. 使用初始 s 中的 Q 值创建一个目标矩阵 (1x3),并将执行的动作 a 的相应 Q 值更改为 QTarget
  3. 使用 net=Train(net,s,Targets) 更新 NN 中的权重
  4. s=s'
  5. 对新的 s 重复上述所有操作

例子:

       actions
        1       2      3
Qs  = 1.3346 -1.9000 0.2371

selected action 3(corresponding to move  mountain car forward)

Qs' = 1.3328 -1.8997 0.2463

QTarget=reward+gamma*max(Qs') = -1+1.0*1.3328 = 0.3328

s= [-5.0; 0.0] and Targets = 1.3346 -1.9000 0.3328

Or I have this wrong and the Targets are 0 0 0.3328 

since we don't know how good the other actions are..

这是我的 Matlab 代码(我使用 R2011 和神经网络工具箱)

%create a neural network
num_hidden=5
num_actions=3
net= newff([-1.2 0.6; -0.07 0.07;], [num_hidden,num_actions], {'tansig', 'purelin'},'traingdm');

%network weight and bias initalization
net= init(net);

%turn off the training window
net.trainParam.showWindow = false;

%neural network training parameters
net.trainParam.lr=0.01;
net.trainParam.mc=0.1;
net.trainParam.epochs=100

%parameters for q learning
epsilon=0.9;
gamma=1.0;


%parameters for Mountain car task
maxEpisodes =10;
maxSteps=5000;
reset=false;
inital_pos=-0.5;
inital_vel=0.0;

%construct the inital state
s=[inital_pos;inital_vel];
Qs=zeros(1,3);
Qs_prime=zeros(1,3);

%training for maxEpisodes
for i=1:maxEpisodes
 %each episode is maxSteps long
 for j = 1:maxSteps

    %run the network and get Q values for current state Qs-> vector of
    %current Q values for state s at time t Q(s_t)
    Qs=net(s);


    %select an action
    if (rand() <= epsilon)
        %returns max Q value over all actions
        [Qs_value a]=max(Qs);
    else
        %return a random number between 1 and 3(inclusive)
        a = randint(1,1,3)+1;
    end

    %simulate a step of Mountain Car
    [s_prime, action, reward, reset] = SimulateMC(s,a);

    %get new Q values for S_prime -> Q(s_t+1)
    Qs_prime=net(s_prime);

    %Compute Qtarget for weight updates given by r+y*max Q(s_t+1) over all
    %actions
    Q_target = reward+gamma*max(Qs_prime);

    %Create a Targets matrix with the orginal state s q-values 
    Targets=Qs;

    %change q-value of the original action to the QTarget
    Targets(a)=Q_target;


    % update the network for input state s and targets
    [net TR]=train(net,s,Targets);
    %update the state for next step
    s=s_prime;
    %display exactly where the car is to user the NN learns if this output reaches -0.45
    disp(s(1))

    if reset==true
        bestSteps=j
        break
    end
 end
 %reset for new episode
 reset=false;
 s=[inital_pos;inital_vel];
end

%test the network
%reset state
 s=[inital_pos;inital_vel];
 for i=1:maxEpisodes
    for j=1:maxSteps
        %run the network and get Q values for current state
        Qs=net(s);

        %select the max  action always
         [Qs_value a]=max(Qs);

        %simulate a step of Mountain Car
        [s_prime, action, reward, reset] = SimulateMC(s,a);

        s=s_prime;
        disp(s(1))
    end
     s=[inital_pos;inital_vel];
 end

谢谢

4

1 回答 1

0

问题表示

使用神经网络来表示价值行动函数是一个好主意。已经表明,这对于许多应用程序都很有效。然而,Q 函数更自然的表示是一个网络,它接收组合的状态-动作向量作为输入并具有标量输出。但只要动作的数量是有限且小的,就应该可以像你那样做。请记住,严格来说,您学习的不是 Q(s,a),而是多个值函数 V(s)(每个动作一个),它们共享相同的权重,除了最后一层。

测试

这是对 Q 函数的直接贪婪利用。应该是正确的。

学习

这里有几个陷阱,你将不得不考虑。第一个是缩放。对于神经网络学习,您确实需要将输入缩放到相同的范围。如果您在输出层中使用 sigmoidal 激活函数,您可能还必须缩放目标值。

数据效率是另一件需要考虑的事情。您可以使用每个转换样本对网络进行多次更新。学习会更快,但您必须将每个转换样本存储在内存中。

在线与批量:如果您存储样本,您可以进行批量学习,并避免最近的样本破坏问题中已经学习的部分的问题。

文学

你应该看看

于 2013-08-30T08:52:18.537 回答