コード例 #1
0
def test_sp1():
    '''
    Test two different SequencePattern instances
    '''
    sp = SequencePattern("maxmin_dup")
    y = sp.generate_output_sequence(range(10))
    assert all(y == np.array([9, 0, 2, 3, 4, 5, 6, 7, 8, 9]))
    sp = SequencePattern("sorted")
    y = sp.generate_output_sequence([5, 6, 1, 2, 9])
    assert all(y == np.array([1, 2, 5, 6, 9]))
    sp = SequencePattern("reversed")
    y = sp.generate_output_sequence(range(10))
    assert all(y == np.array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]))
コード例 #2
0
def test_sp1():
    '''
    Test two different SequencePattern instances
    '''
    sp = SequencePattern("maxmin_dup")
    y = sp.generate_output_sequence(range(10))
    assert all(y==np.array([9, 0, 2, 3, 4, 5, 6, 7, 8, 9]))    
    sp = SequencePattern("sorted")
    y = sp.generate_output_sequence([5,6,1,2,9])
    assert all(y==np.array([1, 2, 5, 6, 9]))
    sp = SequencePattern("reversed")
    y = sp.generate_output_sequence(range(10))
    assert all(y==np.array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]))
コード例 #3
0
def test_sp2():
    '''
    Test two SequencePattern instance with lengths different from default
    '''
    sp = SequencePattern("sorted", in_seq_len=20, out_seq_len=5)
    x = np.random.randint(0, 9, 20)
    y = sp.generate_output_sequence(x)
    assert len(y) == 5
    y_exp = sorted(x)[:5]
    assert all(y == y_exp)
コード例 #4
0
def test_sp2():
    '''
    Test two SequencePattern instance with lengths different from default
    '''
    sp = SequencePattern("sorted", in_seq_len=20, out_seq_len=5)
    x = np.random.randint(0, 9, 20)
    y = sp.generate_output_sequence(x)
    assert len(y)==5
    y_exp = sorted(x)[:5]
    assert all(y==y_exp)
コード例 #5
0
def CommandLine(args=None, arglist=None):
    '''
    Main command line.  Accepts args, to allow for simple unit testing.
    '''
    help_text = """
Commands:

train - give size of training set to use, as argument
predict - give input sequence as argument (or specify inputs via --from-file <filename>)

"""
    parser = argparse.ArgumentParser(
        description=help_text, formatter_class=argparse.RawTextHelpFormatter)

    parser.add_argument("cmd", help="command")
    parser.add_argument("cmd_input", nargs='*', help="input to command")
    parser.add_argument(
        '-v',
        "--verbose",
        nargs=0,
        help="increase output verbosity (add more -v to increase versbosity)",
        action=VAction,
        dest='verbose')
    parser.add_argument(
        "-m",
        "--model",
        help=
        "seq2seq model name: either embedding_rnn (default) or embedding_attention",
        default=None)
    parser.add_argument("-r",
                        "--learning-rate",
                        type=float,
                        help="learning rate (default 0.0001)",
                        default=0.0001)
    parser.add_argument("-e",
                        "--epochs",
                        type=int,
                        help="number of trainig epochs",
                        default=10)
    parser.add_argument("-i",
                        "--input-weights",
                        type=str,
                        help="tflearn file with network weights to load",
                        default=None)
    parser.add_argument(
        "-o",
        "--output-weights",
        type=str,
        help="new tflearn file where network weights are to be saved",
        default=None)
    parser.add_argument("-p",
                        "--pattern-name",
                        type=str,
                        help="name of pattern to use for sequence",
                        default=None)
    parser.add_argument(
        "-n",
        "--name",
        type=str,
        help="name of model, used when generating default weights filenames",
        default=None)
    parser.add_argument("--in-len",
                        type=int,
                        help="input sequence length (default 10)",
                        default=None)
    parser.add_argument("--out-len",
                        type=int,
                        help="output sequence length (default 10)",
                        default=None)
    parser.add_argument(
        "--from-file",
        type=str,
        help="name of file to take input data sequences from (json format)",
        default=None)
    parser.add_argument(
        "--iter-num",
        type=int,
        help=
        "training iteration number; specify instead of input- or output-weights to use generated filenames",
        default=None)
    parser.add_argument(
        "--data-dir",
        help=
        "directory to use for storing checkpoints (also used when generating default weights filenames)",
        default=None)
    # model parameters
    parser.add_argument(
        "-L",
        "--num-layers",
        type=int,
        help="number of RNN layers to use in the model (default 1)",
        default=1)
    parser.add_argument("--cell-size",
                        type=int,
                        help="size of RNN cell to use (default 32)",
                        default=32)
    parser.add_argument("--cell-type",
                        type=str,
                        help="type of RNN cell to use (default BasicLSTMCell)",
                        default="BasicLSTMCell")
    parser.add_argument("--embedding-size",
                        type=int,
                        help="size of embedding to use (default 20)",
                        default=20)
    parser.add_argument("--tensorboard-verbose",
                        type=int,
                        help="tensorboard verbosity level (default 0)",
                        default=0)

    if not args:
        args = parser.parse_args(arglist)

    if args.iter_num is not None:
        args.input_weights = args.iter_num
        args.output_weights = args.iter_num + 1

    model_params = dict(
        num_layers=args.num_layers,
        cell_size=args.cell_size,
        cell_type=args.cell_type,
        embedding_size=args.embedding_size,
        learning_rate=args.learning_rate,
        tensorboard_verbose=args.tensorboard_verbose,
    )

    if args.cmd == "train":
        try:
            num_points = int(args.cmd_input[0])
        except:
            raise Exception(
                "Please specify the number of datapoints to use for training, as the first argument"
            )
        sp = SequencePattern(args.pattern_name,
                             in_seq_len=args.in_len,
                             out_seq_len=args.out_len)
        ts2s = TFLearnSeq2Seq(sp,
                              seq2seq_model=args.model,
                              data_dir=args.data_dir,
                              name=args.name,
                              verbose=args.verbose)
        ts2s.train(num_epochs=args.epochs,
                   num_points=num_points,
                   weights_output_fn=args.output_weights,
                   weights_input_fn=args.input_weights,
                   model_params=model_params)
        return ts2s

    elif args.cmd == "predict":
        if args.from_file:
            inputs = json.loads(args.from_file)
        try:
            input_x = list(map(int, args.cmd_input))
            inputs = [input_x]
        except:
            raise Exception(
                "Please provide a space-delimited input sequence as the argument"
            )

        sp = SequencePattern(args.pattern_name,
                             in_seq_len=args.in_len,
                             out_seq_len=args.out_len)
        ts2s = TFLearnSeq2Seq(sp,
                              seq2seq_model=args.model,
                              data_dir=args.data_dir,
                              name=args.name,
                              verbose=args.verbose)
        results = []
        for x in inputs:
            prediction, y = ts2s.predict(x,
                                         weights_input_fn=args.input_weights,
                                         model_params=model_params)
            print("==> For input %s, prediction=%s (expected=%s)" %
                  (x, prediction, sp.generate_output_sequence(x)))
            results.append([prediction, y])
        ts2s.prediction_results = results
        return ts2s

    else:
        print("Unknown command %s" % args.cmd)
コード例 #6
0
def CommandLine(args=None, arglist=None):
    '''
    Main command line.  Accepts args, to allow for simple unit testing.
    '''
    help_text = """
Commands:

train - give size of training set to use, as argument
predict - give input sequence as argument (or specify inputs via --from-file <filename>)

"""
    parser = argparse.ArgumentParser(description=help_text, formatter_class=argparse.RawTextHelpFormatter)
    
    parser.add_argument("cmd", help="command")
    parser.add_argument("cmd_input", nargs='*', help="input to command")
    parser.add_argument('-v', "--verbose", nargs=0, help="increase output verbosity (add more -v to increase versbosity)", action=VAction, dest='verbose')
    parser.add_argument("-m", "--model", help="seq2seq model name: either embedding_rnn (default) or embedding_attention", default=None)
    parser.add_argument("-r", "--learning-rate", type=float, help="learning rate (default 0.0001)", default=0.0001)
    parser.add_argument("-e", "--epochs", type=int, help="number of trainig epochs", default=10)
    parser.add_argument("-i", "--input-weights", type=str, help="tflearn file with network weights to load", default=None)
    parser.add_argument("-o", "--output-weights", type=str, help="new tflearn file where network weights are to be saved", default=None)
    parser.add_argument("-p", "--pattern-name", type=str, help="name of pattern to use for sequence", default=None)
    parser.add_argument("-n", "--name", type=str, help="name of model, used when generating default weights filenames", default=None)
    parser.add_argument("--in-len", type=int, help="input sequence length (default 10)", default=None)
    parser.add_argument("--out-len", type=int, help="output sequence length (default 10)", default=None)
    parser.add_argument("--from-file", type=str, help="name of file to take input data sequences from (json format)", default=None)
    parser.add_argument("--iter-num", type=int, help="training iteration number; specify instead of input- or output-weights to use generated filenames", default=None)
    parser.add_argument("--data-dir", help="directory to use for storing checkpoints (also used when generating default weights filenames)", default=None)
    # model parameters
    parser.add_argument("-L", "--num-layers", type=int, help="number of RNN layers to use in the model (default 1)", default=1)
    parser.add_argument("--cell-size", type=int, help="size of RNN cell to use (default 32)", default=32)
    parser.add_argument("--cell-type", type=str, help="type of RNN cell to use (default BasicLSTMCell)", default="BasicLSTMCell")
    parser.add_argument("--embedding-size", type=int, help="size of embedding to use (default 20)", default=20)
    parser.add_argument("--tensorboard-verbose", type=int, help="tensorboard verbosity level (default 0)", default=0)

    if not args:
        args = parser.parse_args(arglist)
    
    if args.iter_num is not None:
        args.input_weights = args.iter_num
        args.output_weights = args.iter_num + 1

    model_params = dict(num_layers=args.num_layers,
                        cell_size=args.cell_size,
                        cell_type=args.cell_type,
                        embedding_size=args.embedding_size,
                        learning_rate=args.learning_rate,
                        tensorboard_verbose=args.tensorboard_verbose,
                    )

    if args.cmd=="train":
        try:
            num_points = int(args.cmd_input[0])
        except:
            raise Exception("Please specify the number of datapoints to use for training, as the first argument")
        sp = SequencePattern(args.pattern_name, in_seq_len=args.in_len, out_seq_len=args.out_len)
        ts2s = TFLearnSeq2Seq(sp, seq2seq_model=args.model, data_dir=args.data_dir, name=args.name, verbose=args.verbose)
        ts2s.train(num_epochs=args.epochs, num_points=num_points, weights_output_fn=args.output_weights, 
                   weights_input_fn=args.input_weights, model_params=model_params)
        return ts2s
        
    elif args.cmd=="predict":
        if args.from_file:
            inputs = json.loads(args.from_file)
        try:
            input_x = list(map(int, args.cmd_input))
            inputs = [input_x]
        except:
            raise Exception("Please provide a space-delimited input sequence as the argument")

        sp = SequencePattern(args.pattern_name, in_seq_len=args.in_len, out_seq_len=args.out_len)
        ts2s = TFLearnSeq2Seq(sp, seq2seq_model=args.model, data_dir=args.data_dir, name=args.name, verbose=args.verbose)
        results = []
        for x in inputs:
            prediction, y = ts2s.predict(x, weights_input_fn=args.input_weights, model_params=model_params)
            print("==> For input %s, prediction=%s (expected=%s)" % (x, prediction, sp.generate_output_sequence(x)))
            results.append([prediction, y])
        ts2s.prediction_results = results
        return ts2s

    else:
        print("Unknown command %s" % args.cmd)