예제 #1
0
    config.logger.info(json.dumps(args.__dict__, indent=2))

    # Set seeds for reproducibility
    np.random.seed(args.seed)
    random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)  # multi-GPU.
    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True

    # Set device
    device = torch.device('cuda' if (
        torch.cuda.is_available() and args.cuda) else 'cpu')
    # Load data
    X, y = data.load_data(url=args.data_url, data_size=args.data_size)
    config.logger.info("→ Raw data:\n" f"  {X[0]} → {y[0]}")

    # Preprocesss
    original_X = X
    X = data.preprocess_texts(texts=X, lower=args.lower, filters=args.filters)
    config.logger.info("→ Preprocessed data:\n" f"  {original_X[0]} → {X[0]}")

    # Split data
    X_train, X_val, X_test, y_train, y_val, y_test = data.train_val_test_split(
        X=X,
        y=y,
        val_size=args.val_size,
        test_size=args.test_size,
        shuffle=args.shuffle)
    config.logger.info("→ Data splits:\n"
예제 #2
0
def train(opt):
    print('create model')
    train_model, inference_model = create_model(opt)
    save_path = os.path.join(opt.save_path)
    tensorboard_path = os.path.join(save_path, 'tensorborad')
    check_path(save_path, create=True)
    save_opt(opt, save_path)

    saver = tf.train.Saver(max_to_keep=1)

    # 读取train集和val集,并给予训练集合创建词典
    print('load data set')
    train_dataset = load_data(opt.train_data,
                              opt.vocab_path,
                              opt.label_path,
                              create_vocab=True,
                              create_label_vocab=True,
                              vocab_size=opt.vocab_size)
    val_dataset = load_data(opt.val_data, opt.vocab_path, opt.label_path)
    test_dataset = load_data(opt.test_data, opt.vocab_path, opt.label_path)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    timer = Timer()

    best_accuary = 0
    with tf.Session(config=config) as sess:
        sess.run(tf.global_variables_initializer())
        ckpt = tf.train.get_checkpoint_state(save_path)
        if ckpt:
            print('load model from : %s' % ckpt.model_checkpoint_path)
            saver.restore(sess, ckpt.model_checkpoint_path)
        summary_writer = tf.summary.FileWriter(tensorboard_path, sess.graph)
        for epoch in range(opt.epoch_num):
            epoch_key = 'Epoch: %s' % (epoch + 1)
            print(epoch_key)
            timer.mark(epoch_key)
            total_loss, total_accuracy, total_num = 0, 0, 0
            train_batch_data = batch_iter(*train_dataset,
                                          batch_size=opt.batch_size,
                                          reverse=opt.reverse,
                                          cut_length=opt.cut_length)
            for sequences, labels, lengths in train_batch_data:
                loss, accuracy, global_step, _ = sess.run(
                    [
                        train_model.loss, train_model.accuracy,
                        train_model.global_step, train_model.optimize
                    ],
                    feed_dict=get_feed_dict(train_model, sequences, labels,
                                            lengths))

                batch_num = len(labels)
                total_num += batch_num
                total_loss += batch_num * loss
                total_accuracy += batch_num * accuracy

                if global_step % opt.print_every_step == 0:
                    train_loss = total_loss / total_num
                    train_accuary = total_accuracy / total_num

                    val_loss, val_accuary = evaluate(sess,
                                                     inference_model,
                                                     val_dataset,
                                                     batch_size=opt.batch_size,
                                                     reverse=opt.reverse,
                                                     cut_length=opt.cut_length)
                    summary = tf.Summary(value=[
                        tf.Summary.Value(tag='train_loss',
                                         simple_value=train_loss),
                        tf.Summary.Value(tag='train_accuary',
                                         simple_value=train_accuary),
                        tf.Summary.Value(tag='val_loss',
                                         simple_value=val_loss),
                        tf.Summary.Value(tag='val_accuary',
                                         simple_value=val_accuary),
                    ])
                    summary_writer.add_summary(summary, global_step)
                    cost_time = timer.cost_time()

                    print(
                        message.format(global_step, train_loss, train_accuary,
                                       val_loss, val_accuary, cost_time))
                    if val_accuary > best_accuary:
                        best_accuary = val_accuary
                        saver.save(sess,
                                   os.path.join(save_path,
                                                inference_model.name),
                                   global_step=global_step)
                    total_loss, total_accuracy, total_num = 0, 0, 0

        test_loss, test_accuary = evaluate(sess,
                                           inference_model,
                                           test_dataset,
                                           batch_size=opt.batch_size,
                                           reverse=opt.reverse,
                                           cut_length=opt.cut_length)
        cost_time = timer.cost_time()
        print('eval test data')
        print('loss:{0:6.2}, accuary:{1:7.2%}, cost:{2}'.format(
            test_loss, test_accuary, cost_time))
예제 #3
0
                        default=3,
                        help="# of epochs of continued performance regression")
    parser.add_argument('--overfit',
                        action='store_true',
                        default=False,
                        help="Overfit on a small sample of data.")
    args = parser.parse_args()
    config.logger.info(json.dumps(args.__dict__, indent=4))

    # Set seeds for reproducibility
    np.random.seed(args.seed)
    random.seed(args.seed)
    tf.random.set_seed(args.seed)

    # Load data
    X, y = data.load_data(url=args.data_url, overfit=args.overfit)
    config.logger.info("→ Raw data:\n" f"  {X[0]} → {y[0]}")

    # Split data
    X_train, X_val, X_test, y_train, y_val, y_test = data.train_val_test_split(
        X=X,
        y=y,
        val_size=args.val_size,
        test_size=args.test_size,
        shuffle=args.shuffle)
    config.logger.info("→ Data splits:\n"
                       f"  X_train: {len(X_train)}, y_train: {len(y_train)}\n"
                       f"  X_val: {len(X_val)}, y_val: {len(y_val)}\n"
                       f"  X_test: {len(X_test)}, y_test: {len(y_test)}")

    # Tokenizer