gpt4 book ai didi

keras - 如何获得 Conv2D 层过滤器权重

转载 作者:行者123 更新时间:2023-12-04 02:02:27 25 4
gpt4 key购买 nike

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

我检查了this answer但不明白。请帮我找到一个解决方案,在每个时期之后获取所有过滤器的权重。

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

最佳答案

样本 = 批量大小 = 一批中的图像数量

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

关于keras - 如何获得 Conv2D 层过滤器权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46211730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com