我在Tensorflow上有一些经验,但是在mxnet上只有大约一周的时间。当我在下面的函数中遇到断点时,我试图了解某些代码的行为:
def train_and_eval(lr, end_date_str, pred):
model.collect_params().initialize(mx.init.Xavier(), ctx=ctx, force_reinit=True)
mgr = ProcessMgr(2, end_date_str)
for epoch in range(args_epochs):
for i in range(2):
if i == TRAIN_MODE:
mgr.switch_to_train()
elif epoch == args_epochs - 1 and i == VALIDATE_MODE:
mgr.switch_to_validate()
else:
break
while True:
try:
data, target, eval_target, date_str = mgr.get_batch()
data = gluon.utils.split_and_load(data, ctx)
target = gluon.utils.split_and_load(target, ctx)
eval_target = gluon.utils.split_and_load(eval_target, ctx)
data = [mx.nd.swapaxes(d, 0, 1) for d in data]
with autograd.record():
losses = [loss(model(X)[-args_batch_size:], Y) for X, Y in zip(data, target)]
null_loss_vals = sum([Y.square().sum().asscalar() for Y in target])
model_loss_vals = sum([sum(l).asscalar() for l in losses])
null_loss[i] += null_loss_vals
model_loss[i] += model_loss_vals
**pdb.set_trace() ## BREAK POINT IS HERE**
if i == TRAIN_MODE:
for l in losses:
l.backward()
x = 18
grads = [i.grad(ctx) for i in model.collect_params().values() if i._grad is not None]
gluon.utils.clip_global_norm(grads, args_clip)
trainer.step(GPU_COUNT * args_batch_size)
except:
print("completed an epoch")
break
我正在为所计算的损失获得一些意外值,因此我设置了一个转折点以查看发生了什么。问题是,当我通过模型运行相同的数据时,每次都会得到不同的输出。在下面,我粘贴了到达pdb
断点并尝试通过model
运行数据时得到的一些输出。
<NDArray 38400x1 @gpu(0)>
(Pdb) model(data[0])
[[ 2.9265028e-01]
[ 9.3701184e-03]
[ 4.3234527e-02]
...
[-5.0668776e-09]
[-2.7628975e-08]
[-1.9340845e-08]]
<NDArray 38400x1 @gpu(0)>
(Pdb) model(data[0])
[[ 1.5275864e-01]
[ 2.0615126e-01]
[ 4.6957955e-02]
...
[-2.6077061e-08]
[-9.2040580e-09]
[-3.2883932e-08]]
<NDArray 38400x1 @gpu(0)>
(Pdb) data[0]
[[[ 0. -4.]
[ 0. -4.]
[ 0. -4.]
...
[ 0. -4.]
[ 0. -4.]
[ 0. -4.]]
[[ 0. -4.]
[ 0. -4.]
[ 0. -4.]
...
[ 0. -4.]
[ 0. -4.]
[ 0. -4.]]
[[ 0. -4.]
[ 0. -4.]
[ 0. -4.]
...
[ 0. -4.]
[ 0. -4.]
[ 0. -4.]]
...
[[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
...
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]]
[[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
...
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]]
[[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
...
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]]]
<NDArray 128x300x2 @gpu(0)>
(Pdb) data[0]
[[[ 0. -4.]
[ 0. -4.]
[ 0. -4.]
...
[ 0. -4.]
[ 0. -4.]
[ 0. -4.]]
[[ 0. -4.]
[ 0. -4.]
[ 0. -4.]
...
[ 0. -4.]
[ 0. -4.]
[ 0. -4.]]
[[ 0. -4.]
[ 0. -4.]
[ 0. -4.]
...
[ 0. -4.]
[ 0. -4.]
[ 0. -4.]]
...
[[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
...
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]]
[[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
...
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]]
[[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
...
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]]]
<NDArray 128x300x2 @gpu(0)>
(Pdb)
我对这里发生的事情感到困惑。我确实意识到我的代码可能并不完全正确,因为我没有在预测或推理模型中运行任何东西(打算稍后再检查/解决),但是我不明白模型本身似乎在发生什么变化即使我没有运行backward()
或trainer.step()
,每次我对模型运行输入。任何见解都会受到赞赏。为什么会这样呢?
我唯一的猜测是,两次运行之间可能保留了隐藏状态。但是我以为我没有对它进行编码(我看到了一个示例,在此示例中已完成,并且必须显式保存隐藏状态并将其反馈回RNN)。特别是,我尚未为自己的begin_state
实现gluon.Block
方法。我不确定如何验证或反驳这种猜测。
这是我的胶粘剂。在相关情况下执行该块:
class RNNModel(gluon.Block):
def __init__(self, mode, num_inputs, num_embed, num_hidden,
num_layers, dropout=0.5, tie_weights=False, **kwargs):
super(RNNModel, self).__init__(**kwargs)
with self.name_scope():
self.drop = nn.Dropout(dropout)
self.rnn = rnn.GRU(num_hidden, num_layers, dropout=dropout,
input_size=num_inputs)
self.decoder = nn.Dense(1, in_units = num_hidden)
self.num_hidden = num_hidden
def forward(self, inputs):
output = self.rnn(inputs)
output = self.drop(output)
decoded = self.decoder(output.reshape((-1, self.num_hidden)))
return decoded
答案 0 :(得分:0)
我确定在autograd.record()
上下文中,隐藏状态必须不断发展,因为我没有在此上下文之外看到此行为。因为我的模型没有提供暴露隐藏状态的变量,所以我无法明确验证这一点,但这是最有意义的。我还能够确认(通过trainer._params
公开的权重没有变化,因此它必须是隐藏状态。