问题
Trying to compile CNN model of ~16K parameters on GPU in google colab for mnist dataset. With standard loss 'categorical_crossentropy', it is working fine. But with custom_loss it is giving error.
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 returns 28 reg_wts 224.11805880069733 1.8676504900058112e-05
On compile, gives error AttributeError: 'NoneType' object has no attribute 'get_shape'
回答1:
custom_loss function did not have return statement. A silly mistake, but the error was quite misleading. Hence it took so much time.
来源:https://stackoverflow.com/questions/57613079/compile-error-on-keras-sequential-model-with-custom-loss-function