机器学习---权重参数打包和解包

在机器学习算法的编写中,有时候我们需要在不同的函数中使用相同的参数,比如神经网络训练出来的网络参数。但由于参数过大,直接搬用会显得函数参数体积过大。因此可以将该参数打包成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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值