【问题标题】:<NameError: name 'categorical_crossentropy' is not defined> when trying to load a model<NameError: name 'categorical_crossentropy' is not defined> 尝试加载模型时
【发布时间】:2019-11-01 16:25:11
【问题描述】:

我已经构建了一个自定义 keras 模型:

def create_model(input_dim, 
                     filters, 
                     kernel_size, 
                     strides,
                     padding, 
                     rnn_units=256, 
                     output_dim=30, 
                     dropout_rate=0.5, 
                     cell=GRU, 
                     activation='tanh'):
""" 
Creates simple Conv-Bi-RNN model used for word classification approach.

:params:
    input_dim - Integer, size of inputs (Example: 161 if using spectrogram, 13 for mfcc)
    filters - Integer, number of filters for the Conv1D layer
    kernel_size - Integer, size of kernel for Conv layer
    strides - Integer, stride size for the Conv layer
    padding - String, padding version for the Conv layer ('valid' or 'same')
    rnn_units - Integer, number of units/neurons for the RNN layer(s)
    output_dim - Integer, number of output neurons/units at the output layer
                          NOTE: For speech_to_text approach, this number will be number of characters that may occur
    dropout_rate - Float, percentage of dropout regularization at each RNN layer, between 0 and 1
    cell - Keras function, for a type of RNN layer * Valid solutions: LSTM, GRU, BasicRNN
    activation - String, activation type at the RNN layer

:returns:
    model - Keras Model object

"""

keras.losses.custom_loss = 'categorical_crossentropy'

#Defines Input layer for the model
input_data = Input(name='inputs', shape=input_dim)

#Defines 1D Conv block (Conv layer +  batch norm)
conv_1d = Conv1D(filters, 
                 kernel_size, 
                 strides=strides, 
                 padding=padding,
                 activation='relu',
                 name='layer_1_conv',
                 dilation_rate=1)(input_data)
conv_bn = BatchNormalization(name='conv_batch_norm')(conv_1d)

#Defines Bi-Directional RNN block (Bi-RNN layer + batch norm)
layer = cell(rnn_units, activation=activation,
            return_sequences=True, implementation=2, name='rnn_1', dropout=dropout_rate)(conv_bn)
layer = BatchNormalization(name='bt_rnn_1')(layer)

#Defines Bi-Directional RNN block (Bi-RNN layer + batch norm)
layer = cell(rnn_units, activation=activation,
            return_sequences=True, implementation=2, name='final_layer_of_rnn')(layer)
layer = BatchNormalization(name='bt_rnn_final')(layer)

layer = Flatten()(layer)

#squish RNN features to match number of classes
time_dense = Dense(output_dim)(layer)

#Define model predictions with softmax activation
y_pred = Activation('softmax', name='softmax')(time_dense)

#Defines Model itself, and use lambda function to define output length based on inputs
model = Model(inputs=input_data, outputs=y_pred)

model.output_length = lambda x: cnn_output_length(x, kernel_size, padding, strides)

#Adds categorical crossentropy loss for the classification model

model = add_categorical_loss(model , output_dim)

#compile the model with choosen loss and optimizer

model.compile(loss={'categorical_crossentropy': lambda y_true, y_pred: y_pred}, 
optimizer=keras.optimizers.RMSprop(), metrics=['accuracy'])
print("\r\ncompile the model with choosen loss and optimizer\r\n")

print(model.summary())
return model

训练后的模型:

checkpointer = ModelCheckpoint(filepath=save_path+'tst_model.hdf5')
#Train the choosen model with the data generator

hist = model.fit_generator(generator=generator.next_train(),            #Calls generators next_train function which generates new batch of training data
                            steps_per_epoch=steps_per_epoch,            #Defines how many training steps are there 
                            epochs=epochs,                              #Defines how many epochs does a training process takes
                            validation_data=generator.next_valid(),     #Calls generators next_valid function which generates new batch of validation data
                            validation_steps=validation_steps,          #Defines how many validation steps are theere
                            callbacks=[checkpointer],                   #Defines all callbacks (In this case we only have molde checkpointer that saves the model)
                            verbose=verbose) 

我正在尝试加载最新的检查点模型,如下所示:

从 keras.models 导入 load_model model = load_model(filepath=save_path+'tst_model.hdf5')

然后得到:

NameError: name 'categorical_crossentropy' is not defined

我做错了什么?

使用: Ubuntu 18.04 Python 3.6.8 TensorFlow 2.0 TensorFlow 后端 2.3.1

【问题讨论】:

    标签: tensorflow tf.keras


    【解决方案1】:

    您必须导入库。

    from tensorflow.keras.losses import categorical_crossentropy
    

    【讨论】:

      【解决方案2】:

      当你加载你的模型时,tensorflow 会自动尝试编译它(参见 tf.keras.load_model 的编译参数)。有两种方法可以消除此警告:

      • 如果您为模型提供了自定义损失,则必须将其包含在 tf.keras.load_model() 函数中(请参阅 custom_objects论点;它是一个 dict 对象)。
      • compile 参数设置为 False。

      【讨论】:

        猜你喜欢
        • 2021-02-17
        • 2022-12-02
        • 2013-04-09
        • 2010-10-31
        • 1970-01-01
        • 1970-01-01
        • 2020-11-07
        • 2017-01-09
        相关资源
        最近更新 更多