简体   繁体   中英

Keras An operation has None for gradient when train_on_batch

Google Colab to reproduce the error None_for_gradient.ipynb

I need a custom loss function where the value is calculated according to the model inputs, these inputs are not the default values (y_true, y_pred) . The predict method works for the generated architecture, but when I try to use the train_on_batch , the following error appears.

ValueError: An operation has None for gradient. Please make sure that all of your ops have a gradient defined (ie are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.

My custom function of loss (below) was based on this example image_ocr.py#L475 , in the Colab link has another example based on this solution Custom loss function y_true y_pred shape mismatch #4781 , it also generates the same error:

from keras import backend as K
from keras import losses
import keras
from keras.models import TimeDistributed, Dense, Dropout, LSTM

def my_loss(args):
    input_y, input_y_pred, y_pred = args
    return keras.losses.binary_crossentropy(input_y, input_y_pred)

def generator2():
    input_noise = keras.Input(name='input_noise', shape=(40, 38), dtype='float32')
    input_y = keras.Input(name='input_y', shape=(1,), dtype='float32')
    input_y_pred = keras.Input(name='input_y_pred', shape=(1,), dtype='float32')
    lstm1 = LSTM(256, return_sequences=True)(input_noise)
    drop = Dropout(0.2)(lstm1)
    lstm2 = LSTM(256, return_sequences=True)(drop)
    y_pred = TimeDistributed(Dense(38, activation='softmax'))(lstm2)

    loss_out = keras.layers.Lambda(my_loss, output_shape=(1,), name='my_loss')([input_y, input_y_pred, y_pred])

    model = keras.models.Model(inputs=[input_noise, input_y, input_y_pred], outputs=[y_pred, loss_out])
    model.compile(loss={'my_loss': lambda y_true, y_pred: y_pred}, optimizer='adam')

    return model

g2 = generator2()
noise = np.random.uniform(0,1,size=[10,40,38])
g2.train_on_batch([noise, np.ones(10), np.zeros(10)], noise)

I need help to verify which operation is generating this error, because as far as I know the keras.losses.binary_crossentropy is differentiable.

我认为原因是input_y和input_y_pred都是keras输入,你的损失函数用这两个张量计算,它们没有与模型参数绑定,所以损失函数没有为你的模型提供梯度

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM