2

我正在尝试实现 SGD 功能以在 caffe python 中手动更新 python 中的权重,而不是使用solver.step()函数。solver.step()目标是通过手动更新权重来匹配完成后的权重更新。

设置如下: 使用 MNIST 数据。将solver.prototxt中的随机种子设置为random_seed: 52:确保momentum: 0.0base_lr: 0.01,,lr_policy: "fixed"。上面已经完成,我可以简单地实现 SGD 更新方程(没有动量、正则化等)。公式很简单:W_t+1 = W_t - mu * W_t_diff

以下是两个测试:

Test1: 使用caffe的forward()和backward()计算前向传播和后向传播。对于包含权重的每一层,我都会这样做:

    for k in weight_layer_idx:
        solver.net.layers[k].blobs[0].diff[...] *= lr # weights
        solver.net.layers[k].blobs[1].diff[...] *= lr # biases

接下来,将权重/偏差更新为:

        solver.net.layers[k].blobs[0].data[...] -= solver.net.layers[k].blobs[0].diff
        solver.net.layers[k].blobs[1].data[...] -= solver.net.layers[k].blobs[1].diff

我运行了 5 次迭代。

Test2:运行 caffe 的solver.step(5).

现在,我期望这两个测试在两次迭代后应该产生完全相同的权重。

我在上述每个测试之后保存权重值,并通过两个测试计算权重向量之间的范数差,我发现它们并不精确。有人能发现我可能遗漏的东西吗?

以下是完整的代码供参考:

import caffe
caffe.set_device(0)
caffe.set_mode_gpu()
import numpy as np

niter = 5
solver = None
solver = caffe.SGDSolver('solver.prototxt')

# Automatic SGD: TEST2
solver.step(niter)
# save the weights to compare later
w_solver_step = copy(solver.net.layers[1].blobs[0].data.astype('float64'))
b_solver_step = copy(solver.net.layers[1].blobs[1].data.astype('float64'))

# Manual SGD: TEST1
solver = None
solver = caffe.SGDSolver('solver.prototxt')
lr = 0.01
momentum = 0.

# Get layer types
layer_types = []
for ll in solver.net.layers:
    layer_types.append(ll.type)

# Get the indices of layers that have weights in them
weight_layer_idx = [idx for idx,l in enumerate(layer_types) if 'Convolution' in l or 'InnerProduct' in l]

for it in range(1, niter+1):
    solver.net.forward()  # fprop
    solver.net.backward()  # bprop
    for k in weight_layer_idx:
        solver.net.layers[k].blobs[0].diff[...] *= lr
        solver.net.layers[k].blobs[1].diff[...] *= lr
        solver.net.layers[k].blobs[0].data[...] -= solver.net.layers[k].blobs[0].diff
        solver.net.layers[k].blobs[1].data[...] -= solver.net.layers[k].blobs[1].diff

# save the weights to compare later
w_fwdbwd_update = copy(solver.net.layers[1].blobs[0].data.astype('float64'))
b_fwdbwd_update = copy(solver.net.layers[1].blobs[1].data.astype('float64'))

# Compare
print "after iter", niter, ": weight diff: ", np.linalg.norm(w_solver_step - w_fwdbwd_update), "and bias diff:", np.linalg.norm(b_solver_step - b_fwdbwd_update)

将权重与两个测试进行比较的最后一行产生:

after iter 5 : weight diff: 0.000203027766144 and bias diff: 1.78390789051e-05 正如我所期望的那样,这个差异是 0.0

有任何想法吗?

4

1 回答 1

4

你几乎是对的,你只需在每次更新后将差异设置为零。Caffe 不会自动执行此操作以让您有机会实现批量累积(累积多个批次的梯度以进行一次权重更新,如果您的内存不足以满足您所需的批量大小,这可能会很有帮助)。

另一个可能的问题是 cudnn 的使用,它的卷积实现是不确定的(或者准确地说,它是如何在 caffe 中使用的)。一般来说,这应该没问题,但在你的情况下,每次都会导致略有不同的结果,因此权重也不同。如果您使用 cudnn 编译 caffe,您可以简单地将模式设置为 cpu 以防止在测试时发生这种情况。

此外,求解器参数对权重更新有影响。正如您所指出的,您应该注意:

  • lr_policy:“固定”
  • 动量:0
  • 重量衰减:0
  • random_seed: 52 # 或任何其他常数

在网络中,一定不要使用学习率乘数,通常偏差的学习速度是权重的两倍,但这不是您实现的行为。因此,您需要确保在图层定义中将它们设置为一个:

param {
    lr_mult: 1 # weight lr multiplier
  }
param {
    lr_mult: 1 # bias lr multiplier
  }

最后但同样重要的是,这里有一个示例,您的代码在动量、权重衰减和 lr_mult 下的外观。在 CPU 模式下,这会产生预期的输出(无差异):

import caffe
caffe.set_device(0)
caffe.set_mode_cpu()
import numpy as np

niter = 5
solver = None
solver = caffe.SGDSolver('solver.prototxt')

# Automatic SGD: TEST2
solver.step(niter)
# save the weights to compare later
w_solver_step = solver.net.layers[1].blobs[0].data.copy()
b_solver_step = solver.net.layers[1].blobs[1].data.copy()

# Manual SGD: TEST1
solver = None
solver = caffe.SGDSolver('solver.prototxt')
base_lr = 0.01
momentum = 0.9
weight_decay = 0.0005
lr_w_mult = 1
lr_b_mult = 2

momentum_hist = {}
for layer in solver.net.params:
    m_w = np.zeros_like(solver.net.params[layer][0].data)
    m_b = np.zeros_like(solver.net.params[layer][1].data)
    momentum_hist[layer] = [m_w, m_b]

for i in range(niter):
    solver.net.forward()
    solver.net.backward()
    for layer in solver.net.params:
        momentum_hist[layer][0] = momentum_hist[layer][0] * momentum + (solver.net.params[layer][0].diff + weight_decay *
                                                       solver.net.params[layer][0].data) * base_lr * lr_w_mult
        momentum_hist[layer][1] = momentum_hist[layer][1] * momentum + (solver.net.params[layer][1].diff + weight_decay *
                                                       solver.net.params[layer][1].data) * base_lr * lr_b_mult
        solver.net.params[layer][0].data[...] -= momentum_hist[layer][0]
        solver.net.params[layer][1].data[...] -= momentum_hist[layer][1]
        solver.net.params[layer][0].diff[...] *= 0
        solver.net.params[layer][1].diff[...] *= 0

# save the weights to compare later
w_fwdbwd_update = solver.net.layers[1].blobs[0].data.copy()
b_fwdbwd_update = solver.net.layers[1].blobs[1].data.copy()

# Compare
print "after iter", niter, ": weight diff: ", np.linalg.norm(w_solver_step - w_fwdbwd_update), "and bias diff:", np.linalg.norm(b_solver_step - b_fwdbwd_update)
于 2016-11-20T20:17:08.007 回答