在机器学习算法的编写中,有时候我们需要在不同的函数中使用相同的参数,比如神经网络训练出来的网络参数。但由于参数过大,直接搬用会显得函数参数体积过大。因此可以将该参数打包成params结构(其实是将所有参数变成一列然后再并接起来),使用的时候可以解压成stack结构(其实是字典形式)。
def stack2params(stack):
"""
Converts a "stack" structure into a flattened parameter vector and also
stores the network configuration. This is useful when working with
optimization toolboxes such as minFunc.
[params, netconfig] = stack2params(stack)
stack - the stack structure, where stack{1}.w = weights of first layer
stack{1}.b = weights of first layer
stack{2}.w = weights of second layer
stack{2}.b = weights of second layer
... etc.
:param stack: the stack structure
:return: params: flattened parameter vector
:return: net_config: aux. variable with network structure
"""
params = []
for s in stack:
params.append(s['w'].flatten())
params.append(s['b'].flatten())
params = np.concatenate(params)
net_config = {}
if len(stack) == 0:
net_config['input_size'] = 0
net_config['layer_sizes'] = []
else:
net_config['input_size'] = stack[0]['w'].shape[1]
net_config['layer_sizes'] = []
for s in stack:
net_config['layer_sizes'].append(s['w'].shape[0])
return params, net_config
def params2stack(params, net_config):
"""
Converts a flattened parameter vector into a nice "stack" structure
for us to work with. This is useful when you're building multilayer
networks.
stack = params2stack(params, netconfig)
:param params: flattened parameter vector
:param net_config: aux. variable containing network config.
:return: stack structure (see above)
"""
# Map the params (a vector into a stack of weights)
depth = len(net_config['layer_sizes'])
stack = [dict() for i in range(depth)]
prev_layer_size = net_config['input_size']
current_pos = 0
for i in range(depth):
# Extract weights
wlen = prev_layer_size * net_config['layer_sizes'][i]
stack[i]['w'] = params[current_pos:current_pos + wlen].reshape(net_config['layer_sizes'][i], prev_layer_size)
current_pos = current_pos + wlen
# Extract bias
blen = net_config['layer_sizes'][i]
stack[i]['b'] = params[current_pos:current_pos + blen]
current_pos = current_pos + blen
# Set previous layer size
prev_layer_size = net_config['layer_sizes'][i]
return stack