예제 #1
0
 def __init__(self,
              name: str,
              install_command: str or List[str],
              update_command: str or List[str],
              check_install_command: str = None):
     self.name = name
     self.install_command = get_command(install_command)
     self.update_command = get_command(update_command)
     self.check_install_command = get_command(check_install_command)
예제 #2
0
    async def on_slash_command_error(self, ctx: SlashContext,
                                     error: Exception):
        if isinstance(error, discord.NotFound) and error.code == 10062:
            # NotFound with code 10062 means that the interaction timed out before we sent a response
            _log.warning("Interaction timed out: " + get_command(ctx))
            return

        # Forward the exception to the regular error handler
        await self.cog_command_error(ctx, error)
예제 #3
0
def save_log(mode="w"):
    with open(log_path, mode) as outFile:
        if mode == "a":
            print("----------resume training/early stopping ---------",
                  file=outFile)
        else:
            print("-----------Tokenizer---------", file=outFile)

        # commands for later training
        long, short = util.get_command(str(args))
        p = "python Tokenizer.py "
        long = p + long
        short = p + short
        print(">>> command with full parameters", file=outFile)
        print(long, file=outFile)
        print("\n>>> command with short parameters", file=outFile)
        print(short, file=outFile)
        print("", file=outFile)

        # model parameters and losses
        print("file name = ", args.save_to, file=outFile)
        print("", file=outFile)
        print(">>> ", args.add_note, file=outFile)
        print("bi_lstm ", bi_lstm, file=outFile)
        print("train classifier for " + str(args.epoch) + " epoch",
              file=outFile)
        print("trainLosses ", trainLosses, file=outFile)
        print("devLosses ", devLosses, file=outFile)
        print("", file=outFile)
        print("count train sample ", count_train_samples, file=outFile)
        print("count dev sample ", count_dev_samples, file=outFile)

        # model parameters
        print("", file=outFile)
        l = str(args)[10:].strip(")").split(",")

        for i in l:
            print(i, file=outFile)
        print("", file=outFile)

        # data set
        print("train set: ", train_path, file=outFile)
        print("dev set: ", dev_path, file=outFile)
        print("test set: ", test_path, file=outFile)
        print("", file=outFile)

        # number of samples
        print("count train sample ", count_train_samples, file=outFile)
        print("count dev sample ", count_dev_samples, file=outFile)

        # parameter flags (used when later import this model to another model)
        print("config for later download : ", file=outFile)
        p = util.get_param(str(args))
        print(p, file=outFile)
        print("", file=outFile)
예제 #4
0
 def __init__(self,
              brew_name: str,
              name: str = None,
              setup: str or List[str] = None,
              brew_type: str = ""):
     if not name:
         name = brew_name
     install_command = ' '.join(["brew", brew_type, "install", brew_name])
     if setup:
         install_command = get_command(
             append_or_merge([install_command], setup))
     update_command = ' '.join(["brew", brew_type, "upgrade", brew_name])
     super(BrewDependency, self).__init__(name, install_command,
                                          update_command)
예제 #5
0
def save_csv(f="LM_log.csv"):
    csv_path = os.path.join(CHECKPOINTS_LM, f)
    with open(csv_path, "a+") as table:
        print(args.save_to, file=table, end=';')
        print(args.dataset, file=table, end=';')
        print(num_epoch + 1, file=table, end=';')
        print("trainLosses ", trainLosses, file=table, end=';')
        print("devLosses ", devLosses, file=table, end=';')
        print(args.add_note, file=table, end=';')
        p = util.get_param(str(args))
        print(p, file=table, end=';')
        long, short = util.get_command(str(args))
        p = "python LanguageModel.py "
        long = p + long
        print(long, file=table, end='\n')
예제 #6
0
 async def cog_command_error(self, ctx, error):
     """
     We use this method to handle the most common errors in the cog, so it doesn't have to be
     done for each command separately. It will be called automatically from discord.py.
     """
     if isinstance(error, pylast.PyLastError):
         _log.warning("Last.fm did not respond on time.")
         await self.reply_on_error(
             ctx,
             "There was an error while communicating with the Last.fm API, please try again later."
         )
     elif isinstance(error, tekore.HTTPError):
         _log.warning("Spotify did not respond on time.")
         await self.reply_on_error(
             ctx,
             "There was an error while communicating with the Spotify API, please try again later."
         )
     else:
         _log.error("Unhandled error during command: " + get_command(ctx),
                    exc_info=error)
         await self.reply_on_error(
             ctx,
             "There was an unknown error, please contact the bot owner.")
예제 #7
0
def save_log(mode="w"):
    with open(log_path, mode) as outFile:
        if mode == "a":
            print("-----------resume the training ---------", file=outFile)
        else:
            print("-----------Language Model---------", file=outFile)
        print("file name = ", CHECKPOINTS_LM + args.save_to, file=outFile)
        print("", file=outFile)
        long, short = util.get_command(str(args))
        p = "python LanguageModel.py "
        long = p + long
        short = p + short
        print(">>> command with full parameters", file=outFile)
        print(long, file=outFile)
        print("\n>>> command with short parameters", file=outFile)
        print(short, file=outFile)
        print("", file=outFile)

        print(">>> ", args.add_note, file=outFile)
        print("trainLosses ", trainLosses, file=outFile)
        print("devLosses ", devLosses, file=outFile)
        print("", file=outFile)
        l = str(args)[10:].strip(")").split(",")

        for i in l:
            print(i, file=outFile)

        print("", file=outFile)
        print("train set: ", train_path, file=outFile)
        print("dev set: ", dev_path, file=outFile)
        print("", file=outFile)
        print("config for later download : ", file=outFile)
        p = util.get_param(str(args))
        print(p, file=outFile)
        print("", file=outFile)
        print("save log file to ", args.save_to)
예제 #8
0
def test():
    global model
    count_test_samples = 0

    correct = 0
    falsePositives = 0
    falseNegatives = 0
    count_real_word = 0
    count_predict_word = 0
    word_label = []
    word_pred = []
    word_correct = 0
    precision = 0

    model.rnn.train(False)
    print("testing......")

    test_data_CL = load_data_tokenizer(
        test_path,
        len_chunk=args.len_lines_per_chunk,
        doShuffling=False,
    )
    test_chars = create_tensor_classifier(test_data_CL)

    while True:
        try:
            numeric = next(test_chars)
        except StopIteration:
            break

        loss, numberOfCharacters, log_probs_cal, input_tensor_cal, target_tensor_cal = model.forward_cl(
            numeric, train=False)
        count_test_samples += args.batchSize
        tag_score = log_probs_cal

        flat_input = input_tensor_cal.transpose(1, 0)
        flat_label = target_tensor_cal.transpose(1, 0)
        tag_score = tag_score.transpose(0, 1)
        val, argmax = tag_score.max(dim=2)

        for b in range(args.batchSize):
            chars = [
                itos[element.item()] if element.item() != 0 else "-"
                for element in flat_input[b]
            ]
            pred_seq = chars.copy()
            label = flat_label[b]
            pred = argmax[b]

            for i in range(args.sequence_length):

                if label[i] == 1:
                    chars[i] = " " + chars[i]

                if pred[i] == 1:
                    pred_seq[i] = " " + pred_seq[i]

                    if pred[i] == label[i]:
                        correct += 1

                if pred[i] != label[i] and pred[i] == 0:
                    falseNegatives += 1

                if pred[i] != label[i] and pred[i] == 1:
                    falsePositives += 1
                # word level evaluation
                word_label.append(label[i])
                word_pred.append(pred[i])
                if pred[i] == 1:
                    count_predict_word += 1
                if label[i] == 1:
                    if word_label == word_pred:
                        word_correct += 1
                    word_pred = []
                    word_label = []
                    count_real_word += 1

            if printAllPrediction and "".join(chars) != "".join(pred_seq):
                print("".join(chars))
                print("".join(pred_seq))

        if printPrediction and "".join(chars) != "".join(pred_seq):
            print("".join(chars))
            print("".join(pred_seq))

    print("train losses ", trainLosses)
    print("dev losses ", devLosses)

    print()
    print("============Evaluation===========")
    print()

    if correct == 0:
        print("correct predicted boundaries ", correct)
        print("correct predicted words ", word_correct)
    else:
        precision = correct / (correct + falsePositives)
        recall = correct / (correct + falseNegatives)

        f1 = 2 * (precision * recall) / (precision + recall)
        print("Boundary Measure")
        print("False Negatives ", falseNegatives)
        print("False Positives ", falsePositives)
        print("Correctly predicted boundaries", correct)

        print("Precision", round(precision * 100, 2), "Recall",
              round(recall * 100, 2), "F1", round(f1 * 100, 2))
        print()
        print("Word Measure")
        word_precision = word_correct / (count_predict_word)
        word_recall = word_correct / (count_real_word)
        word_f1 = 2 * (word_precision * word_recall) / (word_precision +
                                                        word_recall)
        print("Correctly predicted words", word_correct)
        print("Number of predicted words", count_predict_word)
        print("Number of real words", count_real_word)
        print("Precision", round(word_precision * 100, 2), "Recall",
              round(word_recall * 100, 2), "F1", round(word_f1 * 100, 2))
        print()
        print("sample result")
        print("label  : " + "".join(chars))
        print("predict: " + "".join(pred_seq))

        with open(log_path, "a+") as outFile:

            print("falseNegatives ", falseNegatives, file=outFile)
            print("falsePositives ", falsePositives, file=outFile)
            print("correct ", correct, file=outFile)

            print("Boundary measures: ", file=outFile)
            print("Precision",
                  precision,
                  "Recall",
                  recall,
                  "F1",
                  2 * (precision * recall) / (precision + recall),
                  file=outFile)
            print("\nWord measures",
                  "Precision",
                  word_precision,
                  "Recall",
                  word_recall,
                  "F1",
                  2 * (word_precision * word_recall) /
                  (word_precision + word_recall),
                  file=outFile)
            print("word_correct :", word_correct, file=outFile)
            print("count_predict_word: ", count_predict_word, file=outFile)
            print("count_real_word: ", count_real_word, file=outFile)

            print("", file=outFile)
            print("".join(chars), file=outFile)
            print("".join(pred_seq), file=outFile)
            print(total_time, file=outFile)
    csv_path = os.path.join(CHECKPOINTS_TOKENIZER, "tokenizer_result.csv")
    with open(csv_path, "a+") as table:

        print()
        print("..........................")
        print("-", file=table, end=';')
        if str(args.load_from)[:2] == "LM":
            print("LM", file=table, end=';')
        else:
            print("-", file=table, end=';')

        print(args.save_to, file=table, end=';')
        print(args.dataset, file=table, end=';')

        print("boundary", file=table, end=';')
        precision = round(precision * 100, 2)
        recall = round(recall * 100, 2)
        f1 = round(f1 * 100, 2)

        print(precision, file=table, end=';')
        print(recall, file=table, end=';')
        print(f1, file=table, end=';')

        print("word", file=table, end=';')
        word_precision = round(word_precision * 100, 2)
        word_recall = round(word_recall * 100, 2)
        word_f1 = round(word_f1 * 100, 2)

        print(word_precision, file=table, end=';')
        print(word_recall, file=table, end=';')
        print(word_f1, file=table, end=';')

        print(num_epoch + 1, file=table, end=';')
        print(total_time, file=table, end=';')

        print("trainLosses ", trainLosses, file=table, end=';')
        print("devLosses ", devLosses, file=table, end=';')
        print(args.add_note, file=table, end=';')
        p = util.get_param(str(args))
        print(p, file=table, end=';')
        long, short = util.get_command(str(args))
        p = "python Tokenizer.py "
        long = p + long
        print(long, file=table, end='\n')