如何通过keras中的numpy数组初始化图层
问题描述:
我想将预先训练好的caffe模型转换为keras,然后我需要逐层初始化图层。 我将重量和偏差保存在mat文件中,并将它们加载到python工作区。 我知道“权重”参数获得numpy数组,但不是如何? 谢谢如何通过keras中的numpy数组初始化图层
答
您可以在Keras Layers Documentation中获得更多关于如何设置模型重量的信息。基本上使用:
layer.set_weights(weights)
:从numpy的阵列的列表设置该层的权重(与相同的形状的get_weights
的输出)。
或者您可以在创建图层时直接初始化它们。每个图层都有一个参数weights
,您可以使用numpy数组进行设置。阅读each layer's documentation以提供正确的权重格式。例如,Dense()
层接受这种格式参数weights
:
numpy的阵列列表以设定为初始权重。该列表应该有两个元素,分别为权重和偏差的形状(input_dim,output_dim)和(output_dim)。 source
卷积层检查http://stackoverflow.com/questions/42211619/how-to-set-weights-for-convolution2d/42212349#42212349 – maz
@maz,这是有用 –