import torch


def save_model(avg_epoch_loss, encoder, decoder, encoder_name, decoder_name):
    """
    :param avg_epoch_loss: Loss value of a testset per epoch
    :param encoder: input language encoder
    :param decoder: taget language decoder
    :param encoder_name: to save a trained encoder with a name
    :param decoder_name: to save a trained decoder with a name
    :return: lowest loss achieved on testset in an epoch
    """
    lowest_loss = 100.00
    if avg_epoch_loss < lowest_loss:
        print(avg_epoch_loss)
        lowest_loss = avg_epoch_loss
        torch.save(encoder.state_dict(), "models/"+encoder_name+'_enc_weights.pt')
        torch.save(decoder.state_dict(), "models/"+decoder_name+'_dec_weights.pt')
    return lowest_loss