尝试加载模型时,<NameError:名称'categorical_crossentropy'未定义>

时间:2019-11-01 16:25:11

标签: tensorflow tf.keras

我有一个自定义的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 型号= 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

2 个答案:

答案 0 :(得分:0)

您必须导入库。

build:
  stage: build
  script:
     -cd App/Server/SubService
    - mvn package

答案 1 :(得分:0)

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

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