亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Keras AttributeError: 'NoneType' 對象在 load_model

Keras AttributeError: 'NoneType' 對象在 load_model

慕桂英3389331 2023-02-15 17:21:49
我正在處理課程作業,我必須在 keras 中保存和加載模型。我創建模型、訓練模型和保存模型的代碼是def get_new_model(input_shape):    """    This function should build a Sequential model according to the above specification. Ensure the     weights are initialised by providing the input_shape argument in the first layer, given by the    function argument.    Your function should also compile the model with the Adam optimiser, sparse categorical cross    entropy loss function, and a single accuracy metric.    """        model = Sequential([        Conv2D(16, kernel_size=(3,3),activation='relu',padding='Same', name='conv_1', input_shape=input_shape),        Conv2D(8, kernel_size=(3,3), activation='relu', padding='Same', name='conv_2'),        MaxPooling2D(pool_size=(8,8), name='pool_1'),        tf.keras.layers.Flatten(name='flatten'),        Dense(32, activation='relu', name='dense_1'),        Dense(10, activation='softmax', name='dense_2')    ])    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])    return modelmodel = get_new_model(x_train[0].shape)def get_checkpoint_every_epoch():    """    This function should return a ModelCheckpoint object that:    - saves the weights only at the end of every epoch    - saves into a directory called 'checkpoints_every_epoch' inside the current working directory    - generates filenames in that directory like 'checkpoint_XXX' where      XXX is the epoch number formatted to have three digits, e.g. 001, 002, 003, etc.    """    path = 'checkpoints_every_epoch/checkpoint_{epoch:02d}'    checkpoint = ModelCheckpoint(filepath = path, save_weights_only=True, save_freq= 'epoch')    return checkpoint
查看完整描述

1 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

我得到了它。文件路徑名中有錯誤。我花了很多時間來弄清楚。所以正確的功能是


def get_model_last_epoch(model):

    """

    This function should create a new instance of the CNN you created earlier,

    load on the weights from the last training epoch, and return this model.

    """

    model.load_weights(tf.train.latest_checkpoint('checkpoints_every_epoch'))

    return model

    

    

def get_model_best_epoch(model):

    """

    This function should create a new instance of the CNN you created earlier, load 

    on the weights leading to the highest validation accuracy, and return this model.

    """

    #filepath = tf.train.latest_checkpoint('checkpoints_best_only')

    model.load_weights(tf.train.latest_checkpoint('checkpoints_best_only'))

    return model

    

它不會給出錯誤,因為文件名tf.train.latest_checkpoint是正確的


查看完整回答
反對 回復 2023-02-15
  • 1 回答
  • 0 關注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號