gpt4 book ai didi

python - 保存和恢复 Keras BLSTM CTC 模型

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:03 25 4
gpt4 key购买 nike

我一直在研究语音情感识别深度神经网络。我使用了带有 CTC 损失的 keras 双向 LSTM。我训练了模型并保存了

model_json = model.to_json()
打开(“ctc_model.json”,“w”)作为 json_file:
json_file.write(model_json)
模型.save_weights("ctc_weights.h5")

问题是我不能使用这个模型来测试看不见的数据,因为模型接受 4 个参数作为输入并计算 ctc 损失..只需构建模型和训练。那么我怎样才能以只需要一个输入的方式保存模型。不是标签和长度。基本上我如何将模型保存为这个函数 test_func = K.function([net_input], [output])

def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args

shift = 2
y_pred = y_pred[:, shift:, :]
input_length -= shift
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
def build_model(nb_feat, nb_class, optimizer='Adadelta'):
net_input = Input(name="the_input", shape=(200, nb_feat))
forward_lstm1 = LSTM(output_dim=64,
return_sequences=True,
activation="tanh"
)(net_input)
backward_lstm1 = LSTM(output_dim=64,
return_sequences=True,
activation="tanh",
go_backwards=True
)(net_input)
blstm_output1 = Merge(mode='concat')([forward_lstm1, backward_lstm1])

forward_lstm2 = LSTM(output_dim=64,
return_sequences=True,
activation="tanh"
)(blstm_output1)
backward_lstm2 = LSTM(output_dim=64,
return_sequences=True,
activation="tanh",
go_backwards=True
)(blstm_output1)
blstm_output2 = Merge(mode='concat')([forward_lstm2, backward_lstm2])

hidden = TimeDistributed(Dense(512, activation='tanh'))(blstm_output2)
output = TimeDistributed(Dense(nb_class + 1, activation='softmax')) (hidden)

labels = Input(name='the_labels', shape=[1], dtype='float32')
input_length = Input(name='input_length', shape=[1], dtype='int64')
label_length = Input(name='label_length', shape=[1], dtype='int64')
loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name="ctc")([output, labels, input_length, label_length])
model = Model(input=[net_input, labels, input_length, label_length], output=[loss_out])
model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=optimizer, metrics=[])

test_func = K.function([net_input], [output])

return model, test_func
model, test_func = build_model(nb_feat=nb_feat, nb_class=nb_class, optimizer=optimizer)
for epoch in range(number_epoches):
inputs_train = {'the_input': X_train[i:i+batch_size],
'the_labels': y_train[i:i+batch_size],
'input_length': np.sum(X_train_mask[i:i+batch_size], axis=1, dtype=np.int32),
'label_length': np.squeeze(y_train_mask[i:i+batch_size]),
}
outputs_train = {'ctc': np.zeros([inputs_train["the_labels"].shape[0]])}

ctcloss = model.train_on_batch(x=inputs_train, y=outputs_train)

total_ctcloss += ctcloss * inputs_train["the_input"].shape[0] * 1.
loss_train[epoch] = total_ctcloss / X_train.shape[0]

enter image description here

Here is the my model summary

最佳答案

尝试以下解决方案:

import keras.backend as K

def get_prediction_function(model):
input_tensor = model.layers[0].input
output_tensor = model.layers[-5].output
net_function = K.function([input_tensor, K.learning_phase()], [output_tensor])
def _result_function(x):
return net_function([x, 0])[0]
return _result_function

现在您的网络功能可能通过以下方式获得:

test_function = get_prediction_function(model)

关于python - 保存和恢复 Keras BLSTM CTC 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47283356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com