通过pycaffe在Caffe中更改求解器参数

6

我如何通过pycaffe更改Caffe中的求解器参数?

例如,在调用solver = caffe.get_solver(solver_prototxt_filename)之后,我想更改求解器的参数(学习率、步长、gamma、动量、base_lr、power等),而不必更改solver_prototxt_filename

1个回答

4
也许你可以创建一个临时文件。
首先,使用以下命令加载您的求解器参数:
from caffe.proto import caffe_pb2
from google.protobuf import text_format
solver_config = caffe_pb2.SolverParameter()
with open('/your/solver/path') as f:
    text_format.Merge(str(f.read()), solver_config)

您可以通过在 solver_config 中设置所需的值来修改任何求解器参数(例如:solver_config.test_interval = 15)。然后,只需创建一个临时文件并从中加载求解器即可:

new_solver_config = text_format.MessageToString(solver_config)
with open('temp.prototxt', 'w') as f:
    f.write(new_solver_config) 
solver = caffe.get_solver('temp.prototxt')
solver.step(1)

我该如何修改solver_config中重复的参数,例如_stepvalue_?当我尝试赋值solver_config.stepvalue = 1000时,会出现**AttributeError: Assignment not allowed to repeated field "stepvalue" in protocol message object.**的错误提示。 - Tu Bui
@TuBui 因为你没有使用“multistep”策略。 - Microos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接