我想用TensorFlow实现Momentum算法。但是我的算法在mnist上是如此之慢。每次迭代都比最后一次慢。我有一个Nvidia TitanX pascal,在tensorflow官方网站上训练演示时非常快。所以,这不是GPU的问题。
这是我的代码(动量和修改动量):
def momentum(loss, t, parameter_list):
opt = GradientDescentOptimizer(1e-3)
grads_and_vars = opt.compute_gradients(loss, parameter_list)
capped_grads_and_vars = []
for i in range(len(grads_and_vars)):
gradient = grads_and_vars[i][0]
variable = grads_and_vars[i][1]
if t != 0:
momentum = gradient + mu * last_momentum0[i]
capped_grads_and_vars.append((momentum, variable))
else:
momentum = gradient
capped_grads_and_vars.append((momentum, variable))
if t != 0:
for i in range(len(grads_and_vars)):
last_momentum0[i] = capped_grads_and_vars[i][0]
else:
for i in range(len(grads_and_vars)):
last_momentum0.append(capped_grads_and_vars[i][0])
return opt.apply_gradients(capped_grads_and_vars)