我已经在不使用丢弃的情况下预训练了LSTM编码器/解码器,这样我就可以使用GPU并加快训练速度。我现在已经重新构建了模型,添加了脱落和复发脱落,并希望在推理过程中激活它,以估计我的预测的置信区间。我不确定在哪里或如何使用顺序API在我的模型中添加training=True参数。我以为我可以在评估模型时这样做,但事实并非如此:
encoder_decoder_dropout.evaluate(val, steps=VALIDATION_STEPS, training=True)
evaluate() got an unexpected keyword argument 'training'
我的模型在下面。任何关于如何在推理过程中激活丢弃的建议都将不胜感激。
encoder = encoder_decoder_dropout_trained.layers[0]
decoder_dropout = tf.keras.Sequential([
tf.keras.layers.RepeatVector(look_back, input_shape=[60]),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(100, dropout=0.2,
recurrent_dropout=0.2,
return_sequences=False)),
tf.keras.layers.Dense(look_forward*num_features,
kernel_initializer=tf.initializers.glorot_normal()),
tf.keras.layers.Reshape([look_forward, num_features])
], name = 'decoder')
encoder_decoder_dropout = tf.keras.Sequential([encoder, decoder_dropout])
encoder_decoder_dropout.set_weights(encoder_decoder_dropout_trained.get_weights())
要激活推理时间的丢弃,您只需在感兴趣的层中指定training=True
(在您的情况下为lstm(
带training=False
inp = Input(shape=(10, 1))
x = LSTM(1, recurrent_dropout=0.3)(inp, training=False)
m = Model(inp,x)
# m.compile(...)
# m.fit(...)
X = np.random.uniform(0,1, (1,10,1))
output = []
for i in range(0,100):
output.append(m.predict(X)) # always the same
带training=True
inp = Input(shape=(10, 1))
x = LSTM(1, recurrent_dropout=0.3)(inp, training=True)
m = Model(inp,x)
# m.compile(...)
# m.fit(...)
X = np.random.uniform(0,1, (1,10,1))
output = []
for i in range(0,100):
output.append(m.predict(X)) # always different
这个需要使用keras函数格式