本文介绍了如何获得Conv2D图层过滤器权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每个时期之后,如何获得Keras中Conv2D层的所有过滤器(如32、64等)的权重?我提到这一点是因为初始权重是随机的,但经过优化后它们会改变.

How do I get the weights of all filters (like 32 ,64, etc.) of a Conv2D layer in Keras after each epoch? I mention that, because initial weights are random but after optimization they will change.

我检查了此答案,但是不清楚,不知道,不懂.请帮助我找到一种在每个时期之后获取所有过滤器权重的解决方案.

I checked this answer but did not understand. Please help me find a solution of getting the weights of all the filter and after every epoch.

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

And one more question is that in Keras documentation for the Conv2D layer input shape is (samples, channels, rows, cols). What exactly does samples mean? Is it the total number of inputs we have (like in MNIST data set it is 60.000 training images) or the batch size (like 128 or other)?

推荐答案

样本=批次大小=批次中的图像数

Samples = batch size = number of images in a batch

Keras通常将None用于此尺寸,这意味着它可以变化,您不必设置.

Keras will often use None for this dimension, meaning it can vary and you don't have to set it.

尽管此尺寸实际上存在,但是在创建图层时,您传递的是input_shape而没有它:

Although this dimension actually exists, when you create a layer, you pass input_shape without it:

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


要在每个时期(或批次)之后执行操作,可以使用 LambdaCallback ,通过on_epoch_end函数:


To have actions done after each epoch (or batch), you can use a LambdaCallback, passing the on_epoch_end function:

#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)

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

Pass this callback to the training function:

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

这篇关于如何获得Conv2D图层过滤器权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:35
查看更多