如何获得Conv2D图层滤镜权重

时间:2017-09-14 06:08:56

标签: keras

如何在每个纪元后获得Keras中Conv2D图层的所有滤镜(如32,64等)的权重?我提到这一点,因为初始权重是随机的,但在优化之后它们会改变。

我查了this answer但不明白。请帮助我找到一个解决方案,获取所有过滤器的重量和每个时代后。

还有一个问题是,在Keras文档中,Conv2D图层的输入形状是(样本,通道,行,列)。 samples究竟是什么意思?它是我们拥有的输入总数(如MNIST数据集中的60.000个训练图像)还是批量大小(如128或其他)?

1 个答案:

答案 0 :(得分:3)

样品=批量大小=批次中的图像数量

Keras通常会将None用于此维度,这意味着它可能会有所不同,您无需进行设置。

虽然此维度确实存在,但在创建图层时,如果没有它,则会传递input_shape

Conv2D(64,(3,3), input_shape=(channels,rows,cols))
#the standard it (rows,cols,channels), depending on your data_format

要在每个纪元(或批处理)之后完成操作,您可以使用LambdaCallback,传递on_epoch_end功能:

#the function to call back
def get_weights(epoch,logs):
    wsAndBs = model.layers[indexOfTheConvLayer].get_weights()
    #or model.get_layer("layerName").get_weights()

    weights = wsAndBs[0]
    biases = wsAndBs[1]
    #do what you need to do with them
    #you can see the epoch and the logs too: 
    print("end of epoch: " + str(epoch)) for instance

#the callback
from keras.callbacks import LambdaCallback
myCallback = LambdaCallback(on_epoch_end=get_weights)

将此回调传递给训练功能:

model.fit(...,...,... , callbacks=[myCallback])