使用自定义损失函数在keras顺序模型上编译错误 [英] Compile error on keras sequential model with custom loss function

查看:66
本文介绍了使用自定义损失函数在keras顺序模型上编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Google Colab中为mnist数据集在GPU上编译〜16K参数的CNN模型.使用标准损失"categorical_crossentropy",它可以正常工作.但是,使用custom_loss会产生错误.

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返回28个reg_wts 224.118058800697331.8676504900058112e-05

reg_loss returns 28 reg_wts 224.11805880069733 1.8676504900058112e-05

在编译时,给出错误 AttributeError:'NoneType'对象没有属性'get_shape'

On compile, gives error AttributeError: 'NoneType' object has no attribute 'get_shape'

推荐答案

custom_loss函数没有return语句.这是一个愚蠢的错误,但该错误却极具误导性.因此,花费了很多时间.

custom_loss function did not have return statement. A silly mistake, but the error was quite misleading. Hence it took so much time.

这篇关于使用自定义损失函数在keras顺序模型上编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆