gpt4 book ai didi

python - 编译keras模型后如何防止反向传播?

转载 作者:行者123 更新时间:2023-11-30 22:22:23 24 4
gpt4 key购买 nike

我有一个像这样的多输出模型

       input
|
hidden
|
/ \
/ \
output1 output2

我可以通过 model.train_on_batch(input=input,output=[output1,output2]) 来训练这个模型,但在训练的某个特定阶段,我只想训练一个分支 (output2)该模型并防止输出 1 的反向传播。我最初尝试在模型中传递一个 Nonemodel.train_on_batch(input=input,output=[None,output2]) 但它显示

AttributeError: 'NoneType' object has no attribute 'shape'

然后我尝试传递一个输出1形状的NaN数组model.train_on_batch(input=input,output=[Nan_array,output2])然后损失变成NaN。如何在多输出 keras 模型中仅训练一个分支并防止另一个分支中的反向传播?

编辑

我试图找到这个问题的解决方案,并遇到了 K.stop_gradient 函数。我试图在这样的单输出模型中停止反向传播

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout
import keras.backend as K

def loss(y_true, y_pred):
return K.stop_gradient(y_pred)

# Generate dummy data
x_train = np.random.random((10, 20))
y_train = np.random.randint(2, size=(10, 1))
x_test = np.random.random((10, 20))
y_test = np.random.randint(2, size=(10, 1))

model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss=loss,
optimizer='rmsprop',
metrics=['accuracy'])

model.fit(x_train, y_train,
epochs=1,
batch_size=128)

score = model.evaluate(x_test, y_test, batch_size=128)

但出现此错误

ValueError: Tried to convert 'x' to a tensor and failed. Error: None values not supported.

最佳答案

您可以创建两个共享权重的Model对象。第一个模型在 output=[output1, output2] 上进行优化,而第二个模型仅包含 output2 的分支。如果您在第二个模型上调用 train_on_batch,分支 1 中的权重将不会更新。

例如,

x = Input(shape=(32,))
hidden = Dense(32)(x)
output1 = Dense(1)(hidden)
output2 = Dense(1)(hidden)

model = Model(x, [output1, output2])
model.compile(loss='mse', optimizer='adam')

model_only2 = Model(x, output2)
model_only2.compile(loss='mse', optimizer='adam')

X = np.random.rand(2, 32)
y1 = np.random.rand(2)
y2 = np.random.rand(2)

# verify: all the weights will change if we train on `model`
w0 = model.get_weights()
model.train_on_batch(X, [y1, y2])
w1 = model.get_weights()
print([np.allclose(x, y) for x, y in zip(w0, w1)])
# => [False, False, False, False, False, False]

# verify: branch 1 will not change if we train on `model_only2`
model_only2.train_on_batch(X, y2)
w2 = model.get_weights()
print([np.allclose(x, y) for x, y in zip(w1, w2)])
# => [False, False, True, True, False, False]

关于python - 编译keras模型后如何防止反向传播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48335212/

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