我试图理解Karpathy在Python中解释的pong代码:karpathy pong
# forward the policy network and sample an action from the returned probability
#########action 2 is up and 3 is down
aprob, h = policy_forward(x)
print("aprob\n {}\n h\n {}\n".format(aprob, h))
#2 is up, 3 is down
action = 2 if np.random.uniform() < aprob else 3 # roll the dice!
print("action\n {}\n".format(action))
# record various intermediates (needed later for backprop)
xs.append(x) # observation, ie. the difference frame?
#print("xs {}".format(xs))
hs.append(h) # hidden state obtained from forward pass
#print("hs {}".format(hs))
#if action is up, y = 1, else 0
y = 1 if action == 2 else 0 # a "fake label"
print("y \n{}\n".format(y))
dlogps.append(y - aprob) # grad that encourages the action that was taken to be taken (see http://cs231n.github.io/neural-networks-2/#losses if confused)
print("dlogps\n {}\n".format(dlogps))
# step the environment and get new measurements
observation, reward, done, info = env.step(action)
print("observation\n {}\n reward\n {}\n done\n {}\n ".format(observation, reward, done))
reward_sum += reward
print("reward_sum\n {}\n".format(reward_sum))
drs.append(reward) # record reward (has to be done after we call step() to get reward for previous action)
print("drs\n {}\n".format(drs))
if done: # an episode finished
episode_number += 1
在上面的片段中,我不太明白为什么需要假标签以及这意味着什么:dlogps.append(y - aprob)# grad that encourages the action that was taken to be taken (see http://cs231n.github.io/neural-networks-2/#losses if confused)
为什么是假标签y
减去aprob
?在
我的理解是,网络输出一个“对数概率”上升,但解释似乎表明,标签实际上应该是采取该行动所获得的奖励,然后鼓励一集内的所有行动,如果是成功的。因此,我不明白假标签1或0是如何起作用的。在
同样在前向传递函数中,没有日志操作,所以它是一个日志概率?在
^{pr2}$编辑:
我使用print语句来查看引擎盖下发生了什么,并发现既然y=0
表示action down,(y - aprob)
对于action down将是负的。他用优势来调节梯度的公式,最终仍然表明向下移动是好的,即负数还是坏数,即正数。
而对于向上运动,则相反。也就是说,epdlogp *= discounted_epr
的正数意味着行动是好的,负的意味着行动是坏的。
因此,这似乎是一种相当简洁的实现方法,但我仍然不明白从前向传递返回的aprob
是一个日志概率,因为到控制台的输出如下所示:
aprob
0.5
action
3
aprob
0.5010495775824385
action
2
aprob
0.5023498477623756
action
2
aprob
0.5051575154468827
action
2
看起来概率在0和1之间。那么,使用y - aprob
作为一种“对数概率”仅仅是一种伴随着经过数月和数年实践而形成的直觉的黑客行为吗?如果是这样,这些黑客是通过反复试验发现的吗?在
编辑:多亏了汤米的精彩解释,我知道在我的Udacity深度学习课程的视频中,我可以从哪里寻找关于对数概率和交叉熵的复习:https://www.youtube.com/watch?time_continue=94&v=iREoPUrpXvE
另外,这个备忘单也有帮助:https://ml-cheatsheet.readthedocs.io/en/latest/loss_functions.html
我对他如何到达的解释:
当他向前通过他的网络时,最后一步是对最后一个神经元的输出应用sigmoids(x)。在
以及它的梯度
^{pr2}$为了增加/减少你行动的可能性,你必须计算你的“标签”的概率日志
为了反向传播,你必须计算你的似然L的梯度
因为在输出上应用了sigmoid函数p=S(y),所以实际上是在计算
这实际上只不过是对数损失/交叉熵。 更一般的公式是:
由于Andrej在他的例子中没有使用像Tensorflow或PyTorch这样的框架,所以他在那里做了一些反向传播。在
一开始我也很困惑,我花了一些时间才弄清楚那里到底有什么魔法。也许他本可以说得更清楚一些,并给出一些提示。在
至少这是我对他的准则的拙劣理解:)
相关问题 更多 >
编程相关推荐