npy文件读取与fine tune

npy文件是numpy专用的二进制文件,npy内容由一个字典组成,字典中的每一个键对应一层网络模型参数(包括权重w和偏置b)。
对于某些网络模型参数保存为.npy形式的文件,我们需要读取这些文件的参数,并fine tune到当前网络相应的变量。

def load_initial_weights(session):
  # Load the weights into memory
    weights_dict = np.load('C:\\Downloads\\bvlc_alexnet.npy', encoding = 'bytes').item()
  # Loop over all layer names stored in the weights dict
    for op_name in weights_dict:
    # Check if the layer is one of the layers that should be reinitialized
        if op_name not in ['fc6','fc7','fc8']:
            with tf.variable_scope('shared/'+op_name,reuse = True):
       # Loop over list of weights/biases and assign them to their corresponding tf variable
                for data in weights_dict[op_name]:
                  # Biases
                    if len(data.shape) == 1:
                        var = tf.get_variable('biases', trainable = True)
                        print(var.name)
                        session.run(var.assign(data))
                      # Weights
                    else:
                        var = tf.get_variable('weights', trainable = True)
                        session.run(var.assign(data))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还是少年呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值