我有一个生成培训数据的系统,我想要一起添加损失函数以获得批量大小。我想做(full code at commit in question),
for epoch in range(100):
with mx.autograd.record():
loss = 0.0
for k in range(40):
(i, x), (j, y) = random.choice(data), random.choice(data)
# Just compute loss on last output
if i == j:
loss = loss - l2loss(net(mx.nd.array(x)), net(mx.nd.array(y)))
else:
loss = loss + l2loss(net(mx.nd.array(x)), net(mx.nd.array(y)))
loss.backward()
trainer.step(BATCH_SIZE)
但是我收到了一个错误,
---------------------------------------------------------------------------
MXNetError Traceback (most recent call last)
<ipython-input-39-14981406278a> in <module>()
21 else:
22 loss = loss + l2loss(net(mx.nd.array(x)), net(mx.nd.array(y)))
---> 23 loss.backward()
24 trainer.step(BATCH_SIZE)
25 avg_loss += mx.nd.mean(loss).asscalar()
... More trace ...
MXNetError: [16:52:49] src/pass/gradient.cc:187: Operator _copyto is non-differentiable because it didn't register FGradient attribute.
如何逐步添加丢失函数,就像我正在尝试的那样?
答案 0 :(得分:0)
您使用的是哪个版本的MXNet?我无法使用最新的代码库重现这一点。您可以尝试使用GitHub主分支或0.12版本。