在Keras的Alexnet模型中使用预先训练的权重

时间:2018-12-22 16:06:11

标签: python machine-learning keras

我正在尝试使用来自bvlc_alexnet.npy的预训练权重来实现AlexNet:

#load the weight data
weights_dic = numpy.load('bvlc_alexnet.npy', encoding='bytes').item()
conv1W = weights_dic["conv1"][0] # <class 'numpy.ndarray'> (11, 11, 3, 96)
conv1b = weights_dic["conv1"][1] # <class 'numpy.ndarray'> (96,)

model = Sequential()
model.add(Conv2D(96, kernel_size=[11, 11], kernel_initializer = <???>, 
             bias_initializer = <???>, dtype=np.ndarray), activation='relu', strides=4, padding="same")

在这里,我陷入了如何为conv1Wconv1b属性分配这些权重(kernel_initializerbias_initializer)的问题。

1 个答案:

答案 0 :(得分:2)

首先构造模型,而无需设置任何初始化程序。然后按照图层在模型中出现的顺序将所有权重放入列表中(例如conv1_weights,conv1_biases,conv2_weights,conv2_biases等),然后调用模型的set_weights方法:

model.set_weights(weights)

或者,您可以分别设置每个图层的权重:

model.layers[layer_index].set_weights([layer_weights, layer_biases])

# or using layer's name if you have specified names for them
model.get_layer(layer_name).set_weights([layer_weights, layer_biases])