本文整理匯總了Python中net.Net.forward_backward方法的典型用法代碼示例。如果您正苦於以下問題:Python Net.forward_backward方法的具體用法?Python Net.forward_backward怎麽用?Python Net.forward_backward使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net.Net
的用法示例。
在下文中一共展示了Net.forward_backward方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Solver
# 需要導入模塊: from net import Net [as 別名]
# 或者: from net.Net import forward_backward [as 別名]
class Solver(object):
"""Docstring for Solver. """
def __init__(self, param):
"""TODO: to be defined1. """
self.param = param
self.init_train_net(param)
def init_train_net(self, param):
net_param = pb.NetParameter()
with open(param.net, "rb") as f:
text_format.Merge(f.read(), net_param)
net_state = pb.NetState()
net_state.phase = pb.TRAIN
# net_state.MergeFrom(net_param.state)
# net_state.MergeFrom(param.train_state)
net_param.state.CopyFrom(net_state)
self.train_net = Net(net_param)
def step(self, iters):
avg_loss = self.param.average_loss
losses = []
smoothed_loss = 0
for i in range(iters):
loss = self.train_net.forward_backward()
if len(losses) < avg_loss:
losses.append(loss)
size = len(losses)
smoothed_loss = (smoothed_loss * (size - 1) + loss) / size
else:
idx = (i - 0) % avg_loss
smoothed_loss += (loss - losses[idx]) / avg_loss
log.info("Iteration %d, loss %f", i, smoothed_loss)
self.compute_update_value(i)
# self.train_net.update()
def compute_update_value(self, i):
current_step = i / 100000.0
base_lr = 0.01
gamma = 0.1
rate = base_lr * pow(gamma, current_step)
weight_decay = 0.0005
momentum = 0.9
self.train_net.update_params(rate, weight_decay, momentum)