topn op 没有优化时,没有正确反传梯度
Created by: lgone2000
当分支条件为1时(使用topk),打印查找fc.w_0@GRAD 返回空,没有正确生成反向梯度 当分钟条件为0是(不适用topk),可以正常工作
def test_topn_grad():
input = fluid.layers.data(name='input', shape=[1], dtype='float32')
fc = fluid.layers.fc(name='fc', input=input, size=10, bias_attr=False)
if 1:
top_fc, top_index = fluid.layers.topk(fc, k=10)
loss = fluid.layers.reduce_sum(input=top_fc)
else:
loss = fluid.layers.reduce_sum(input=fc)
optimizer = fluid.optimizer.SGD(learning_rate=1.0)
opts = optimizer.minimize(loss)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
def find_var(varname):
return [x for x in fluid.default_main_program().list_vars() if x.name == varname]
grad = find_var('fc.w_0@GRAD')
print(grad)