gpt4 book ai didi

python-3.x - Keras:初始化权重时模型未学习

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

使用 Keras 设置初始模型权重时,模型不会在后续训练调用中更新。

MNIST 数据示例:(示例中初始权重是随机的,但将在以后的迭代中传递,因此不能选择使用随机内核)

W1 = np.random.rand(784, n_nodes)
b1 = np.random.rand(n_nodes,)
W2 = np.random.rand(n_nodes, 10)
b2 = np.random.rand(10,)
model = Sequential()
model.add(Dense(n_nodes, input_dim=784, activation='relu', weights=[W1,b1]))
model.add(Dense(10, activation='softmax', weights = [W2, b2]))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

for batch in batch_sets:
model.train_on_batch(batch[0], batch[1])

我查了 b1参数通过 get_weights()在每次迭代中,可以看到没有从原始的随机更新 b1 .

我也试过使用 set_weights以及自定义内核(和偏差)初始值设定项,并且我遇到了同样的问题。当内核初始值设定项设置为 1 或 0 时,也会发生同样的无更新问题,但当使用随机内核初始值设定项时,行为符合预期(也称为适当更新)。

是否有其他方法可以设置初始权重,以便在后续模型训练调用中更新它们?

最佳答案

weights=参数正在替换通常放在 .weights 中的内容Layer 的属性(property),它们是 TensorFlow 变量。用 numpy 数组替换变量意味着它们作为常量嵌入到 TensorFlow 图中,这意味着优化器会忽略它们(它们是常量!)。

相反,您可以将 numpy 数组作为内核和偏差的初始值设定项传递:Dense(kernel_initializer=w1, bias_initializer=b1, ...

关于python-3.x - Keras:初始化权重时模型未学习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50769874/

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