“Could not interpret optimizer identifier” error in Keras

后端 未结 14 1524
别跟我提以往
别跟我提以往 2021-02-03 19:47

I got this error when I tried to modify the learning rate parameter of SGD optimizer in Keras. Did I miss something in my codes or my Keras was not installed properly?

14条回答
  •  走了就别回头了
    2021-02-03 20:00

    I am bit late here, Your issue is you have mixed Tensorflow keras and keras API in your code. The optimizer and the model should come from same layer definition. Use Keras API for everything as below:

    from keras.models import Sequential
    from keras.layers import Dense, Dropout, LSTM, BatchNormalization
    from keras.callbacks import TensorBoard
    from keras.callbacks import ModelCheckpoint
    from keras.optimizers import adam
    
    # Set Model
    model = Sequential()
    model.add(LSTM(128, input_shape=(train_x.shape[1:]), return_sequences=True))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())
    
    # Set Optimizer
    opt = adam(lr=0.001, decay=1e-6)
    
    # Compile model
    model.compile(
        loss='sparse_categorical_crossentropy',
        optimizer=opt,
        metrics=['accuracy']
    )
    

    I have used adam in this example. Please use your relevant optimizer as per above code.

    Hope this helps.

提交回复
热议问题