尝试在Google Colab中为mnist数据集在GPU上编译〜16K参数的CNN模型。 标准损失为'categorical_crossentropy',它可以正常工作。但是,使用custom_loss会产生错误。
lamda=0.01
m = X_train.shape[0]
def reg_loss(lamda):
model_layers = custom_model.layers # type list where each el is Conv2D obj etc.
reg_wts = 0
for idx, layer in enumerate(model_layers):
layer_wts = model_layers[idx].get_weights() # type list
if len(layer_wts) > 0: # activation, dropout layers do not have any weights
layer_wts = model_layers[idx].get_weights()[0] #ndarray, 3,3,1,16 : layer1 output
s = np.sum(layer_wts**2)
reg_wts += s
print(idx, "reg_wts", reg_wts)
return (lamda/(2*m))* reg_wts
reg_loss(lamda)
def custom_loss(y_true, y_pred):
K.categorical_crossentropy(y_true, y_pred) + reg_loss(lamda)
custom_model.compile(loss=custom_loss, optimizer='adam', metrics=['accuracy'])
reg_loss返回28个reg_wts 224.11805880069733 1.8676504900058112e-05
答案 0 :(得分:0)
custom_loss函数没有return语句。这是一个愚蠢的错误,但是该错误却极具误导性。因此花费了很多时间。