加载已保存的检查点并预测不会产生与训练相同的结果

Rom*_*cea 17 python deep-learning keras tensorflow

我正在根据我在互联网上找到的示例代码进行培训.测试的准确率为92%,检查点保存在目录中.并行(培训现在运行3天)我想创建我的预测代码,这样我就可以学到更多,而不仅仅是等待.

这是我深度学习的第三天,所以我可能不知道自己在做什么.以下是我试图预测的方式:

  • 使用与训练中相同的代码实例化模型
  • 加载最后一个检查点
  • 试着预测

代码有效,但结果不到90%.

以下是我创建模型的方法:

INPUT_LAYERS = 2
OUTPUT_LAYERS = 2
AMOUNT_OF_DROPOUT = 0.3
HIDDEN_SIZE = 700
INITIALIZATION = "he_normal"  # : Gaussian initialization scaled by fan_in (He et al., 2014)
CHARS = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .")

def generate_model(output_len, chars=None):
    """Generate the model"""
    print('Build model...')
    chars = chars or CHARS
    model = Sequential()
    # "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE
    # note: in a situation where your input sequences have a variable length,
    # use input_shape=(None, nb_feature).
    for layer_number in range(INPUT_LAYERS):
        model.add(recurrent.LSTM(HIDDEN_SIZE, input_shape=(None, len(chars)), init=INITIALIZATION,
                         return_sequences=layer_number + 1 < INPUT_LAYERS))
        model.add(Dropout(AMOUNT_OF_DROPOUT))
    # For the decoder's input, we repeat the encoded input for each time step
    model.add(RepeatVector(output_len))
    # The decoder RNN could be multiple layers stacked or a single layer
    for _ in range(OUTPUT_LAYERS):
        model.add(recurrent.LSTM(HIDDEN_SIZE, return_sequences=True, init=INITIALIZATION))
        model.add(Dropout(AMOUNT_OF_DROPOUT))

    # For each of step of the output sequence, decide which character should be chosen
    model.add(TimeDistributed(Dense(len(chars), init=INITIALIZATION)))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model
Run Code Online (Sandbox Code Playgroud)

在一个单独的文件中,predict.py我导入此方法来创建我的模型并尝试预测:

...import code
model = generate_model(len(question), dataset['chars'])
model.load_weights('models/weights.204-0.20.hdf5')

def decode(pred):
    return character_table.decode(pred, calc_argmax=False)


x = np.zeros((1, len(question), len(dataset['chars'])))
for t, char in enumerate(question):
    x[0, t, character_table.char_indices[char]] = 1.

preds = model.predict_classes([x], verbose=0)[0]

print("======================================")
print(decode(preds))
Run Code Online (Sandbox Code Playgroud)

我不知道问题是什么.我的目录中有大约90个检查点,我根据准确性加载最后一个检查点.所有这些都由以下人员保存ModelCheckpoint:

checkpoint = ModelCheckpoint(MODEL_CHECKPOINT_DIRECTORYNAME + '/' + MODEL_CHECKPOINT_FILENAME,
                         save_best_only=True)
Run Code Online (Sandbox Code Playgroud)

我被卡住了.我究竟做错了什么?

Jan*_*ski 0

当您在 Predict.py 文件中生成模型时:

model = generate_model(len(question), dataset['chars'])
Run Code Online (Sandbox Code Playgroud)

您的第一个参数与训练文件中的相同吗?或者问题长度是动态的吗?如果是这样,您将生成不同的模型,因此您保存的检查点不起作用。