本文整理汇总了Python中network.Network.forward方法的典型用法代码示例。如果您正苦于以下问题:Python Network.forward方法的具体用法?Python Network.forward怎么用?Python Network.forward使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类network.Network
的用法示例。
在下文中一共展示了Network.forward方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import forward [as 别名]
def run():
parser = argparse.ArgumentParser(
description='Neural network damage experiment. ',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--proto', required=True, type=str,
help='Network prototxt.')
parser.add_argument('--model', required=True, type=str,
help='Network caffemodel.')
parser.add_argument('--meanfile', required=True, type=str,
help='Data mean file.')
parser.add_argument('--data', required=True, type=str,
help='Data.')
parser.add_argument('--layer', required=True, type=str,
help='Layer to apply damage to.')
parser.add_argument('--prefix', required=True, type=str,
help='Uniqe model name.')
parser.add_argument('--damage', default=1.0, type=damage_range,
help='Applied damage range.')
parser.add_argument('--step-num', default=10, type=int,
help='Number of steps in damage interval.')
parser.add_argument('--iterations', default=100, type=int,
help='Number of iterations to run.')
parser.add_argument('--repeat', default=0, type=int,
help='Number of repeated experiments to run.')
args = parser.parse_args()
# Object file
obj = 'experiment_damage_' + str(args.layer) + '_' + str(args.damage)
obj += '_step_' + str(args.step_num) + '_iter_' + str(args.iterations)
obj += '_prefix_' + str(args.prefix)
loss_list = list()
for std in np.linspace(0.0, args.damage, args.step_num):
_loss = list()
for t in range(args.repeat + 1):
# Fetch a network structure and apply damage
net = Network(args.proto, args.model, args.meanfile, args.data)
net.__add_damage__(args.layer, std)
# Forward to get loss
top_1, top_5, acc, loss = net.forward(maxrun=args.iterations)
_loss.append(loss)
loss_list.append(_loss)
# Store result
if path.exists(obj+'.pkl'):
read_loss = load_obj(obj)
combined = np.concatenate((read_loss, np.array(loss_list)), 1)
save_obj(combined, obj)
else:
save_obj(np.array(loss_list), obj)
示例2: run
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import forward [as 别名]
def run():
parser = argparse.ArgumentParser(
description='Neural network damage experiment. ',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--proto', required=True, type=str,
help='Network prototxt.')
parser.add_argument('--model', required=True, type=str,
help='Network caffemodel.')
parser.add_argument('--meanfile', required=True, type=str,
help='Data mean file.')
parser.add_argument('--data', required=True, type=str,
help='Data.')
parser.add_argument('--layer', required=True, type=str,
help='Layer to apply damage to.')
parser.add_argument('--prefix', required=True, type=str,
help='Uniqe model name.')
parser.add_argument('--damage', default=1.0, type=damage_interval,
help='Applied damage range.')
parser.add_argument('--step-num', default=10, type=int,
help='Number of steps in damage interval.')
parser.add_argument('--iterations', default=100, type=int,
help='Number of iterations to run.')
parser.add_argument('--repeat', default=0, type=int,
help='Number of repeated damage experiments.')
args = parser.parse_args()
# Object file
obj = 'experiment_damage_' + str(args.layer) + '_' + str(args.damage)
obj += '_step_' + str(args.step_num) + '_iter_' + str(args.iterations)
obj += '_prefix_' + str(args.prefix)
# If file not exists
if not path.isfile(obj + '.pkl'):
# Calculate damage
loss_list = list()
for std in np.linspace(0.0, args.damage, args.step_num):
_loss = list()
for repeat in range(args.repeat + 1):
# Fetch a network structure and apply damage
net = Network(args.proto, args.model, args.meanfile, args.data)
net.__add_damage__(args.layer, std)
# Forward to get loss
top_1, top_5, acc, loss = net.forward(maxrun=args.iterations)
_loss.append(loss)
loss_list.append(_loss)
# Save data
save_obj(np.array(loss_list), obj)
# If file exists
else:
# Load data
loss_list = load_obj(obj)
# Calculate hessian
net = Network(args.proto, args.model, args.meanfile, args.data)
# Get the layer std. and damage interval
net_std = net.__est_std__(args.layer)
damage_range = np.linspace(0.0, args.damage, args.step_num)
damage_span = net_std*damage_range
# Get the Hessian and calculate the expected loss
trace = net.get_hessian(args.layer, maxrun=np.min((args.iterations, 1e5)))
expected_loss = 0.5*(damage_span**2)*np.sum(trace)+np.mean(loss_list, 1)[0]
# Make plot?
plt.rcParams['figure.figsize'] = (6, 6)
plt.figure(1)
plt.plot(damage_range, np.mean(loss_list, 1), 'k-', label='Measured loss')
plt.plot(damage_range, expected_loss, 'k--', label='Est. loss')
plt.legend(loc='upper left')
plt.grid()
plt.xlabel('Std. damage')
plt.ylabel('Loss')
plt.title('Estimated loss on layer ' + str(args.layer))
plt.savefig(obj + '.pdf', format='pdf')