Ejemplo n.º 1
0
def validate(model,
             x,
             y_true,
             input_len,
             label_len,
             y_strings,
             test=False,
             save_file=None):
    input_len = np.expand_dims(input_len, axis=1)
    label_len = np.expand_dims(label_len, axis=1)

    y_pred = model(x)
    loss = ctc_batch_cost(y_true, y_pred, input_len, label_len)

    input_len = np.squeeze(input_len)
    y_decode = ctc_decode(y_pred, input_len)[0][0]

    accuracy = 0.0

    for i in range(len(y_strings)):
        predicted_sentence = indices_to_string(y_decode[i].numpy())
        accuracy += wer(predicted_sentence, y_strings[i])

        if test:
            save_file.write("Correct Sentence:" + str(y_strings[i]) + "\n")
            save_file.write("Predicted Sentence:" + predicted_sentence + "\n")

    return tf.reduce_mean(loss), accuracy / len(y_strings)
Ejemplo n.º 2
0
def train_one_step(model, optimizer, x, y_true, input_len, label_len,
                   y_strings):

    input_len = np.expand_dims(input_len, axis=1)
    label_len = np.expand_dims(label_len, axis=1)

    with tf.GradientTape() as tape:
        y_pred = model(x)
        loss = ctc_batch_cost(y_true, y_pred, input_len, label_len)

    grads = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(grads, model.trainable_variables))

    input_len = np.squeeze(input_len)
    y_decode = ctc_decode(y_pred, input_len)[0][0]

    accuracy = 0.0

    for i in range(len(y_strings)):
        predicted_sentence = indices_to_string(y_decode[i].numpy())
        accuracy += wer(predicted_sentence, y_strings[i])

    return tf.reduce_mean(loss), accuracy / len(y_strings)
Ejemplo n.º 3
0
            generate_batches(dataset, batch_size=128)):
        y_pred = generater(batch_dict['x_data'])

        loss = utils.sequence_loss(y_pred, batch_dict['y_target'],
                                   vectorizer.char_vocab.mask_index)
        acc = utils.compute_accuracy(y_pred, batch_dict['y_target'],
                                     vectorizer.char_vocab.mask_index)

        loss_epoch += (loss.item() - loss_epoch) / (batch_index + 1)
        acc_epoch += (acc - acc_epoch) / (batch_index + 1)

    index_view = torch.randint(low=0,
                               high=len(batch_dict['x_data']),
                               size=(1, )).item()
    print("{}\n{}".format(
        utils.indices_to_string(batch_dict['x_data'][index_view].numpy(),
                                vectorizer.char_vocab),
        utils.indices_to_string(
            torch.argmax(torch.softmax(y_pred, dim=2)[index_view],
                         dim=1).numpy(), vectorizer.char_vocab)))

    print("epoch {}: train loss {}, acc: {:.2f}\n\n".format(
        epoch_index, loss_epoch, acc_epoch))

samples_indices = utils.sample_from_model(model=generater,
                                          vectorizer=vectorizer,
                                          num_samples=10,
                                          sample_size=10,
                                          temperature=0.5)

print("Sample generate: ")
for indices in samples_indices: