Example #1
0
    def train(self):
        """
        Train MemN2N model using training data for tasks.
        """
        np.random.seed(42)  # for reproducing
        assert self.data_dir is not None, "data_dir is not specified."
        print("Reading data from %s ..." % self.data_dir)

        # Parse training data
        train_data_path = glob.glob('%s/qa*_*_train.txt' % self.data_dir)
        dictionary = {"nil": 0}
        train_story, train_questions, train_qstory = parse_babi_task(train_data_path, dictionary, False)

        # Parse test data just to expand the dictionary so that it covers all words in the test data too
        test_data_path = glob.glob('%s/qa*_*_test.txt' % self.data_dir)
        parse_babi_task(test_data_path, dictionary, False)

        # Get reversed dictionary mapping index to word
        self.reversed_dict = dict((ix, w) for w, ix in dictionary.items())

        # Construct model
        self.general_config = BabiConfigJoint(train_story, train_questions, dictionary)
        self.memory, self.model, self.loss = build_model(self.general_config)

        # Train model
        if self.general_config.linear_start:
            train_linear_start(train_story, train_questions, train_qstory,
                               self.memory, self.model, self.loss, self.general_config)
        else:
            train(train_story, train_questions, train_qstory,
                  self.memory, self.model, self.loss, self.general_config)

        # Save model
        self.save_model()
Example #2
0
def trainer() -> None:
    train_on_gpu = torch.cuda.is_available()
    batch_size = 32
    n_epochs = 50

    train_data = torch.load("dev_train.pt")
    valid_data = torch.load("dev_valid.pt")

    train_loader = torch.utils.data.DataLoader(train_data,
                                               batch_size=batch_size,
                                               pin_memory=True)

    valid_loader = torch.utils.data.DataLoader(valid_data,
                                               batch_size=batch_size,
                                               pin_memory=True)

    model = SiAudNet()

    if train_on_gpu:
        model = model.cuda()

    file_name = "model_siaudnet.pt"
    optimizer = optim.Adadelta(model.parameters())
    train(
        model,
        train_on_gpu,
        n_epochs,
        train_loader,
        valid_loader,
        optimizer,
        file_name,
        True,
    )
Example #3
0
    def train(self):
        """
        Train MemN2N model using training data for tasks.
        """
        np.random.seed(42)  # for reproducing
        assert self.data_dir is not None, "data_dir is not specified."
        print("Reading data from %s ..." % self.data_dir)

        # Parse training data
        train_data_path = glob.glob('%s/qa*_*_train.txt' % self.data_dir)
        dictionary = {"nil": 0}
        train_story, train_questions, train_qstory = parse_babi_task(train_data_path, dictionary, False)

        # Parse test data just to expand the dictionary so that it covers all words in the test data too
        test_data_path = glob.glob('%s/qa*_*_test.txt' % self.data_dir)
        parse_babi_task(test_data_path, dictionary, False)

        # Get reversed dictionary mapping index to word
        self.reversed_dict = dict((ix, w) for w, ix in dictionary.items())

        # Construct model
        self.general_config = BabiConfigJoint(train_story, train_questions, dictionary)
        self.memory, self.model, self.loss = build_model(self.general_config)

        # Train model
        if self.general_config.linear_start:
            train_linear_start(train_story, train_questions, train_qstory,
                               self.memory, self.model, self.loss, self.general_config)
        else:
            train(train_story, train_questions, train_qstory,
                  self.memory, self.model, self.loss, self.general_config)

        # Save model
        self.save_model()
Example #4
0
def run():
    parser = GooeyParser()
    subs = parser.add_subparsers(help='commands', dest='commands')

    train_parser = subs.add_parser('train', help='Configurate model training')
    param_group = train_parser.add_argument_group("Model parameter option", gooey_options={'show_border': True, 'columns': 2})
    args_param.add(param_group)
    data_group = train_parser.add_argument_group("Data Options", gooey_options={'show_border': True}, )
    args_data.add(data_group)
    save_group = train_parser.add_argument_group("Save option", gooey_options={'show_border': True, 'columns': 2})
    args_save.add(save_group)


    test_parser = subs.add_parser('test', help='Configurate model testining')
    data_group = test_parser.add_argument_group("Data Options", gooey_options={'show_border': True}, )
    args_data.add(data_group)
    load_group = test_parser.add_argument_group("Load option", gooey_options={'show_border': True, 'columns': 1})
    args_load.add(load_group, model_savefiles())
    save_group = test_parser.add_argument_group("Save option", gooey_options={'show_border': True, 'columns': 2})
    args_save.add(save_group)

    args = parser.parse_args()
    X, Y = load_data(args.data_path)

    if args.commands =='train':
        train(args, X, Y, save_dir)
    else:
        with open(save_dir + args.load_model, 'rb') as f:
            model = pickle.load(f)
        test(args, X, Y, save_dir, model)
def run_task(data_dir, task_id):
    """
    Train and test for each task
    """
    print("Train and test for task %d ..." % task_id)

    # Parse data
    train_files = glob.glob('%s/qa%d_*_train.txt' % (data_dir, task_id))
    test_files = glob.glob('%s/qa%d_*_test.txt' % (data_dir, task_id))

    dictionary = {"nil": 0}
    train_story, train_questions, train_qstory = parse_babi_task(
        train_files, dictionary, False)
    test_story, test_questions, test_qstory = parse_babi_task(
        test_files, dictionary, False)

    general_config = BabiConfig(train_story, train_questions, dictionary)

    memory, model, loss = build_model(general_config)

    if general_config.linear_start:
        train_linear_start(train_story, train_questions, train_qstory, memory,
                           model, loss, general_config)
    else:
        train(train_story, train_questions, train_qstory, memory, model, loss,
              general_config)

    test(test_story, test_questions, test_qstory, memory, model, loss,
         general_config)
Example #6
0
def main():
    model = Seq2Seq(input_dim=40, vocab_size=len(LETTER_LIST), hidden_dim=128)
    optimizer = optim.Adam(model.parameters(), lr=0.001)
    criterion = nn.CrossEntropyLoss(reduction=None)
    nepochs = 25
    batch_size = 64 if DEVICE == 'cuda' else 1

    speech_train, speech_valid, speech_test, transcript_train, transcript_valid = load_data(
    )
    character_text_train = transform_letter_to_index(transcript_train,
                                                     LETTER_LIST)
    character_text_valid = transform_letter_to_index(transcript_valid,
                                                     LETTER_LIST)

    train_dataset = Speech2TextDataset(speech_train, character_text_train)
    # val_dataset =
    test_dataset = Speech2TextDataset(speech_test, None, False)

    train_loader = DataLoader(train_dataset,
                              batch_size=batch_size,
                              shuffle=True,
                              collate_fn=collate)
    # val_loader =
    test_loader = DataLoader(test_dataset,
                             batch_size=batch_size,
                             shuffle=False,
                             collate_fn=collate_test)

    for epoch in range(nepochs):
        train(model, train_loader, criterion, optimizer, epoch)
        # val()
        test(model, test_loader, epoch)
Example #7
0
def train_network():
    # Adjust the training options as necessary
    training_options = {
        'learning_rate': 0.0002,
        'training_epochs': 10,
        'batch_size': 64,
        'adam_beta_1': 0.5,
        'adam_beta_2': 0.999,
        'save_path': './models/your_network.pth',        
        'continue_training_from': None, #Path to a saved neural net model
        'image_transform': transforms.Compose([
                    transforms.Resize((224,224)), #Make sure size matches model input size
                    # transforms.Pad(0),
                    # transforms.CenterCrop(224)
                    # See https://pytorch.org/docs/stable/torchvision/transforms.html
                    #   for more transforms
                ]),
        'data_source': ['lab', 'field'], # Whether to train on data from the lab or the field, or both
        'device': 'cuda:0' if torch.cuda.is_available() else 'cpu',
        'printEvery': 1 #How often to print batch summaries
    }

    network = models.alexnet(pretrained=False, num_classes=185)
    #network = Your_Network()
    train(network, training_options)
def main():
    train_story_set = StoryDataset(sis_train, vocab)
    # val_story_set = StoryDataset(sis_val, vocab)
    # test_story_set = StoryDataset(sis_test, vocab)

    train_loader = DataLoader(train_story_set,
                              shuffle=False,
                              batch_size=BATCH_SIZE,
                              collate_fn=collate_story,
                              pin_memory=False)
    # imgs of shape [BS, 5, 3, 224, 224]
    # sents BS * 5  * MAX_LEN

    model_v1 = ModelV1(vocab)

    # Learning rate is the most sensitive value to set,
    # will need to test what works well past 400 instances
    optimizer = torch.optim.Adam(model_v1.parameters(),
                                 lr=0.001)  # .001 for 400
    isTraining = True

    if isTraining:
        train(10, model_v1, train_loader, optimizer)
    else:
        model_v1.load_state_dict(torch.load('./Training/7'))
        test_loader = DataLoader(train_story_set,
                                 shuffle=False,
                                 batch_size=BATCH_SIZE,
                                 collate_fn=collate_story)
        test(model_v1, test_loader, device, vocab)
Example #9
0
def main():
    config_dict = config.get_config()
    log_dir = train_test.create_log_dir("/opt/code/my_logs")
    train_test.copy("/opt/code/src/config.py", log_dir)
    train_test.copy("/opt/code/src/models.py", log_dir)
    model_name = config_dict['model_name']
    model = config_dict['model']
    save_model = config_dict['save_model']
    optimizer = config_dict['optimizer']
    epoch = config_dict['epoch']
    long_patience = config_dict['long_patience']
    train_set = config_dict['train_set']
    test_set = config_dict['test_set']

    print()

    train_test.train(
        train_set,
        test_set,
        model,
        optimizer,
        log_dir,
        epochs=epoch,
        long_patience=long_patience,
    )

    if save_model:
        torch.save(model, model_name)
def main():
    model = Seq2Seq(input_dim=40,
                    vocab_size=len(LETTER_LIST),
                    hidden_dim=256,
                    isAttended=True)
    #     print(model)
    optimizer = optim.Adam(model.parameters(), lr=0.001, weight_decay=5e-5)
    # scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=2, gamma=0.1)
    criterion = nn.CrossEntropyLoss(reduce=False, reduction=None)
    nepochs = 18
    batch_size = 64 if DEVICE == 'cuda' else 1

    speech_train, speech_valid, speech_test, transcript_train, transcript_valid = load_data(
    )
    character_text_train = transform_letter_to_index(transcript_train,
                                                     LETTER_LIST)
    character_text_valid = transform_letter_to_index(transcript_valid,
                                                     LETTER_LIST)

    train_dataset = Speech2TextDataset(speech_train, character_text_train)
    val_dataset = Speech2TextDataset(speech_valid, character_text_valid)

    test_dataset = Speech2TextDataset(speech_test, None, False)

    train_loader = DataLoader(train_dataset,
                              batch_size=batch_size,
                              shuffle=True)  #, collate_fn=collate_train)
    val_loader = DataLoader(val_dataset, batch_size=batch_size,
                            shuffle=True)  #, collate_fn=collate_train)
    test_loader = DataLoader(test_dataset,
                             batch_size=batch_size,
                             shuffle=False)  #, collate_fn=collate_test)

    for epoch in range(nepochs):
        train(model, train_loader, criterion, optimizer, epoch)
        # scheduler.step()
        val(model, val_loader, criterion, epoch)
        # Test and Save results
        test_preds = test(model, test_loader)
        test_preds = test_preds.cpu().numpy()
        results = []
        for i in range(test_preds.shape[0]):
            result = ""
            for j in range(test_preds.shape[1]):
                if (test_preds[i, j] == 0 or (test_preds[i, j] == 33)):
                    continue
                if (test_preds[i, j] == 34):
                    break
                result = result + index2letter[test_preds[i, j]]
            results.append(result)
        name = "Epoch_" + str(epoch) + "_LAS_submission.csv"
        ids = list(range(len(test_dataset)))
        ids.insert(0, 'Id')
        results.insert(0, 'Predicted')
        with open(name, 'w') as f:
            writer = csv.writer(f)
            writer.writerows(zip(ids, results))
Example #11
0
def main():
    model = Seq2Seq(input_dim=40, vocab_size=len(LETTER_LIST), hidden_dim=128)
    optimizer = optim.SGD(model.parameters(), lr=1e-4, weight_decay=5e-4)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.93)
    criterion = nn.CrossEntropyLoss(reduction='none')
    init_epoch = 0
    nepochs = 50
    batch_size = 64 if DEVICE == 'cuda' else 1

    speech_train, speech_valid, speech_test, transcript_train, transcript_valid = load_data(
    )
    character_text_train = transform_letter_to_index(transcript_train,
                                                     LETTER_LIST)
    character_text_valid = transform_letter_to_index(transcript_valid,
                                                     LETTER_LIST)

    train_dataset = Speech2TextDataset(speech_train, character_text_train)
    val_dataset = Speech2TextDataset(speech_valid, character_text_valid)
    test_dataset = Speech2TextDataset(speech_test, None, False)

    train_loader = DataLoader(train_dataset,
                              batch_size=batch_size,
                              shuffle=True,
                              collate_fn=collate_train_val)
    val_loader = DataLoader(val_dataset,
                            batch_size=batch_size,
                            shuffle=True,
                            collate_fn=collate_train_val)
    test_loader = DataLoader(test_dataset,
                             batch_size=batch_size,
                             shuffle=False,
                             collate_fn=collate_test)

    val_distances = []
    exp = 27
    # model.load_state_dict(torch.load('BestModel9.pth'))

    with open('stats_{}'.format(exp), 'w') as file:

        file.write('Experiment: {}\n'.format(exp))

    for epoch in range(init_epoch, nepochs):
        train(model, train_loader, criterion, optimizer, scheduler, epoch, exp)
        val_distances.append(val(model, val_loader, epoch, exp))
        if val_distances[-1] == min(val_distances):
            torch.save(model.state_dict(), 'BestModel{}.pth'.format(exp))

        if epoch % 3 == 0 or epoch == nepochs - 1:
            test(model, test_loader, exp)
def main():
    model = Seq2Seq(input_dim=40,
                    vocab_size=len(LETTER_LIST),
                    hidden_dim=128,
                    value_size=128,
                    key_size=256,
                    isAttended=True)
    model.load_state_dict(torch.load('model3'))
    model.eval()
    model = model.to(DEVICE)
    optimizer = optim.Adam(model.parameters(), lr=0.0005)
    criterion = nn.CrossEntropyLoss(reduction='sum')
    nepochs = 10
    batch_size = 64 if DEVICE == 'cuda' else 1

    speech_train, speech_valid, speech_test, transcript_train, transcript_valid = load_data(
    )
    character_text_train = transform_letter_to_index(transcript_train,
                                                     LETTER_LIST)
    character_text_valid = transform_letter_to_index(transcript_valid,
                                                     LETTER_LIST)

    train_dataset = Speech2TextDataset(speech_train, character_text_train)
    val_dataset = Speech2TextDataset(speech_valid, character_text_valid)
    test_dataset = Speech2TextDataset(speech_test, None, False)

    train_loader = DataLoader(train_dataset,
                              batch_size=batch_size,
                              shuffle=True,
                              collate_fn=collate_train)
    for x in train_loader:
        pass

    val_loader = DataLoader(val_dataset,
                            batch_size=batch_size,
                            shuffle=True,
                            collate_fn=collate_train)
    test_loader = DataLoader(test_dataset,
                             batch_size=batch_size,
                             shuffle=False,
                             collate_fn=collate_test)

    for epoch in range(nepochs):
        print('==============', 'Epoch', epoch + 1, '================')
        train(model, train_loader, criterion, optimizer, epoch)
        val(model, val_loader, criterion, optimizer, epoch)
    torch.save(model.state_dict(), 'model3')

    load_model(model, test_loader)
Example #13
0
def main():
    model = Seq2Seq(input_dim=40,
                    vocab_size=len(LETTER_LIST),
                    hidden_dim=128,
                    value_size=128,
                    key_size=256,
                    is_attended=True)

    # cur_model_num = 6
    # model.load_state_dict(torch.load('model_{}'.format(cur_model_num)))

    optimizer = optim.Adam(model.parameters(), lr=0.001)
    criterion = nn.CrossEntropyLoss(reduction="sum")
    n_epochs = 30
    batch_size = 64 if DEVICE == 'cuda' else 1

    speech_train, speech_valid, speech_test, transcript_train, transcript_valid = load_data(
    )
    character_text_train = transform_letter_to_index(transcript_train,
                                                     LETTER2INDEX)
    character_text_valid = transform_letter_to_index(transcript_valid,
                                                     LETTER2INDEX)

    train_dataset = Speech2TextDataset(speech_train, character_text_train)
    val_dataset = Speech2TextDataset(speech_valid, character_text_valid)
    test_dataset = Speech2TextDataset(speech_test, None, False)

    train_loader = DataLoader(train_dataset,
                              batch_size=batch_size,
                              shuffle=True,
                              collate_fn=collate_train)
    val_loader = DataLoader(val_dataset,
                            batch_size=batch_size,
                            shuffle=True,
                            collate_fn=collate_train)
    test_loader = DataLoader(test_dataset,
                             batch_size=batch_size,
                             shuffle=False,
                             collate_fn=collate_test)

    for epoch in range(n_epochs):
        train(model, train_loader, criterion, optimizer, epoch)
        val(model, val_loader, criterion, epoch)

    # test(model, test_loader)

    torch.save(model.state_dict(), 'model_{}'.format(1))

    result_gen(test_loader, 1)
Example #14
0
def main():
    model = Seq2Seq(input_dim=40, vocab_size=len(LETTER_LIST), hidden_dim=128)
    learningRate = 0.001
    weightDecay = 5e-5
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=learningRate,
                                 weight_decay=weightDecay)
    criterion = nn.CrossEntropyLoss(reduction='none')
    nepochs = 40
    batch_size = 64 if DEVICE == 'cuda' else 1

    speech_train, speech_valid, speech_test, transcript_train, transcript_valid = load_data(
    )
    character_text_train = transform_letter_to_index(transcript_train,
                                                     LETTER_LIST)
    character_text_valid = transform_letter_to_index(transcript_valid,
                                                     LETTER_LIST)

    train_dataset = Speech2TextDataset(speech_train, character_text_train)
    val_dataset = Speech2TextDataset(speech_valid, character_text_valid)
    test_dataset = Speech2TextDataset(speech_test, None, False)

    train_loader = DataLoader(train_dataset,
                              batch_size=batch_size,
                              shuffle=True,
                              collate_fn=collate_train)
    val_loader = DataLoader(val_dataset,
                            batch_size=batch_size,
                            shuffle=False,
                            collate_fn=collate_train)
    test_loader = DataLoader(test_dataset,
                             batch_size=batch_size,
                             shuffle=False,
                             collate_fn=collate_test)

    model.train()
    model.load_state_dict(torch.load('./new1.pth'))
    model.to(DEVICE)

    scheduler = StepLR(optimizer, step_size=10, gamma=0.5)
    for epoch in range(nepochs):
        train(model, train_loader, criterion, optimizer, epoch)
        scheduler.step()

    model.eval()
    data_list = test(model, test_loader)

    save_to_csv(data_list)
    print('done')
Example #15
0
def main(epochs, batch_size, input_size, hidden_size, num_layers, spatial_num, drop_out, logged=False):
    DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    print('DEVICE: ', DEVICE)

    date = '06_03'
    group = 1
    sorted_ = True
    # sorted_ = False

    # load data from '.npy' file
    # x_train, x_test, y_train, y_test = load_group_eeg_data(date, group, sorted_=sorted_)
    x_train, x_test, y_train, y_test = load_combined_eeg_data(date, sorted_=sorted_)
    # x: (N, C, T)  N: trials  C: channels  T: times 
    train_num, test_num = x_train.shape[0], x_test.shape[0]
    
    # make dataset for train and test
    train_data = MyDataset(x_train, x_test, y_train, y_test)
    test_data = MyDataset(x_train, x_test, y_train, y_test, train=False)
    train_loader = DataLoader(train_data, batch_size=batch_size)
    test_loader = DataLoader(test_data, batch_size=batch_size)

    # model initiation
    # model = LSTM(num_classes=2, input_size=64, hidden_size=256, num_layers=2)
    
    # model = LSTM_CNN(num_classes=2, channels=x_train.shape[1], input_size=input_size, hidden_size=hidden_size, 
    #                  num_layers=num_layers, spatial_num=spatial_num, drop_out=drop_out)
    
    # model = LSTM_CNN_Half(num_classes=2, batch_size=batch_size, T=x_train.shape[-1],
    #                       C=x_train.shape[-2], input_size=input_size, hidden_size=hidden_size,
    #                       num_layers=num_layers, spatial_num=spatial_num)
    
    model = LSTM_CNN_Spatial(num_classes=2, batch_size=batch_size, T=x_train.shape[-1],
                          C=x_train.shape[-2], input_size=input_size, hidden_size=hidden_size,
                          num_layers=num_layers, spatial_num=spatial_num)
    
    model = model.to(DEVICE)
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters())

    log = []
    if logged:
        log.append(f'{epochs}\t{batch_size}\t{input_size}\t{hidden_size}\t'
                   f'{num_layers}\t{spatial_num}\t{drop_out}\t')
    train(model, criterion, optimizer, train_loader, DEVICE,train_num, epochs, logged)
    test(model, criterion, test_loader, DEVICE, test_num, log, logged)
def run_joint_tasks(data_dir):
    """
    Train and test for all tasks but the trained model is built using training data from all tasks.
    """
    print("Jointly train and test for all tasks ...")
    tasks = range(20)

    # Parse training data
    train_data_path = []
    for t in tasks:
        train_data_path += glob.glob('%s/qa%d_*_train.txt' % (data_dir, t + 1))

    dictionary = {"nil": 0}
    train_story, train_questions, train_qstory = parse_babi_task(
        train_data_path, dictionary, False)

    # Parse test data for each task so that the dictionary covers all words before training
    for t in tasks:
        test_data_path = glob.glob('%s/qa%d_*_test.txt' % (data_dir, t + 1))
        parse_babi_task(test_data_path, dictionary,
                        False)  # ignore output for now

    general_config = BabiConfigJoint(train_story, train_questions, dictionary)
    memory, model, loss = build_model(general_config)

    if general_config.linear_start:
        train_linear_start(train_story, train_questions, train_qstory, memory,
                           model, loss, general_config)
    else:
        train(train_story, train_questions, train_qstory, memory, model, loss,
              general_config)

    # Test on each task
    for t in tasks:
        print("Testing for task %d ..." % (t + 1))
        test_data_path = glob.glob('%s/qa%d_*_test.txt' % (data_dir, t + 1))
        dc = len(dictionary)
        test_story, test_questions, test_qstory = parse_babi_task(
            test_data_path, dictionary, False)
        assert dc == len(
            dictionary
        )  # make sure that the dictionary already covers all words

        test(test_story, test_questions, test_qstory, memory, model, loss,
             general_config)
Example #17
0
def run(args):
    if USE_CUDA:
        print("Using cuda...")
    else:
        print("Suggest using cuda, break now...")

    phone_map = make_phone_map()
    phone2index, index2phone, index2char = make_phone_char()
    label = make_label(phone2index)
    if args.test:
        test(args.test, args.feature, args.model, args.hidden, args.layer,
             args.output, index2char, index2phone, phone_map, phone2index)
    elif args.loss:
        train_loss(args.loss)
    else:
        train(args.feature, label, args.epochs, args.model, args.layer,
              args.hidden, args.save, args.postfix, index2char, index2phone,
              phone_map, phone2index)
Example #18
0
def main():
    # Check device available
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    print("Running on: {}".format(device))
    # parse and print arguments
    args = make_args_parser()
    print_args(args)
    # Load both source and target domain datasets
    source_dataloader = datasets.get_source_domain(args.source)
    target_dataloader = datasets.get_target_domain(args.target)
    # Create directory to save model's checkpoints
    try:
        model_root = MODEL_CHECKPOINTS + args.source + '-' + args.target
        os.makedirs(model_root)
    except OSError as e:
        if e.errno == errno.EEXIST:
            pass
        else:
            raise
    # Init model
    if args.model == 'MultibranchLeNet':
        net = models.MultibranchLeNet()
        architectures = ['conv1_d', 'conv1_t', 'conv2_d', 'conv2_t']
    if device == 'cuda':
        net.cuda()
    # Init losses
    class_loss = torch.nn.NLLLoss()
    domain_loss = torch.nn.NLLLoss()
    if device == 'cuda':
        class_criterion.cuda()
        domain_criterion.cuda()
    # Init optimizer
    optimizer = optim.SGD(net.parameters(),
                          lr=constants.LR,
                          momentum=constants.MOMENTUM)
    # Init all parameters to be optimized using Backpropagation
    for param in net.parameters():
        param.requires_grad = True
    # Train model
    for epoch in range(constants.N_EPOCHS):
        train_test.train(net, class_loss, domain_loss, source_dataloader,
                         target_dataloader, optimizer, epoch, model_root,
                         device)
        train_test.test(net, source_dataloader, target_dataloader, device)
Example #19
0
def main():
    ''' 
    run the experiment
    '''
    global env
    logging.info("Started...")
    env = utils.make_env(config)
    n_actions = env.action_space.n
    qnet = Qnet(n_actions, embedding_size=config.embedding_size).to(device)
    target_net = Qnet(n_actions,
                      embedding_size=config.embedding_size).to(device)
    target_net.load_state_dict(qnet.state_dict())
    target_net.eval()
    replay_buffer = ReplayBuffer(config.replay_buffer_size,
                                 config.embedding_size, config.path_length)
    optimizer = torch.optim.Adam(qnet.parameters(), lr=config.lr)
    value_buffer = ValueBuffer(config.value_buffer_size)
    train(env, qnet, target_net, optimizer, replay_buffer, value_buffer,
          config, device)
Example #20
0
def main():

    args = parser.parse_args()

    use_cuda = not args.no_cuda and torch.cuda.is_available()

    torch.manual_seed(args.seed)

    device = torch.device("cuda" if use_cuda else "cpu")

    kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

    train_loader = torch.utils.data.DataLoader(datasets.MNIST(
        '/tmp',
        train=True,
        download=True,
        transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307, ), (0.3081, ))
        ])),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               **kwargs)
    test_loader = torch.utils.data.DataLoader(datasets.MNIST(
        '/tmp',
        train=False,
        transform=transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.1307, ), (0.3081, ))
        ])),
                                              batch_size=args.test_batch_size,
                                              shuffle=True,
                                              **kwargs)

    model = fc.Net().to(device)
    optimizer = optim.SGD(model.parameters(), lr=args.lr)

    for epoch in range(1, args.epochs + 1):
        train(args, model, device, train_loader, optimizer, args.epochs, epoch)
        test(args, model, device, test_loader)

    print_nonzeros(model)
Example #21
0
def main(nets, net_name, data_info, batch_size, epochs, num_iteration, logged=False):
    # num_classes = date_info['num_classes']
    subject_id = data_info['subject_id']
    edge_type = data_info['edge_type']
    feature_type = data_info['feature_type']
    num_features = data_info['num_features']
    sorted_ = data_info['sorted']

    train_loader = gen_dataloader(data_info['train_lis'],
                                  batch_size=batch_size)
    test_loader = gen_dataloader(data_info['test_lis'],
                                 batch_size=batch_size)

    for i in trange(num_iteration):
        # model initiation
        if net_name == 'tag_jk' or net_name == 'tag_jk_learn':
            model = nets[net_name](num_features, 4).to(device)
        elif net_name == 'tag_lstm':
            model = nets[net_name]().to(device)
        else:
            model = nets[net_name](num_features).to(device)

        # criterion = torch.nn.BCELoss()
        # criterion = torch.nn.CrossEntropyLoss(weight=torch.tensor([1.0, 0.5]).to(device))
        criterion = torch.nn.CrossEntropyLoss()
        optimizer = optim.Adam(model.parameters())

        log = []
        if logged:
            if len(subject_id) > 2:
                log.append(f'{subject_id}\t{sorted_}\t{edge_type:<6s}\t{feature_type:<6s}\t'
                           f'{net_name:<8s}\t{batch_size:<4d}\t{epochs:<4d}\t')
            else:
                log.append(f's{subject_id:02d}\t{edge_type:<6s}\t{feature_type:<6s}\t'
                       f'{net_name:<8s}\t{batch_size:<4d}\t{epochs:<4d}\t')

        train(model, criterion, optimizer, train_loader, device,
              data_info['train_num'], epochs, logged)

        test(model, criterion, test_loader, device,
             data_info['test_num'], log, logged)
Example #22
0
    def train_new_model():
        (train_data, author_frame, domain_frame,
         subreddit_frame) = load_data(path)
        (self.trained_model, cross_validation_frame,
         test_frame) = train(train_frame, author_frame, domain_frame,
                             subreddit_frame)
        if cross_valid == 1:
            cross_validation(self.trained_model, cross_validation)
        test(trained_model, test_frame)

        def predict(self, X):
            self.trained_model.predict(X)
Example #23
0
def train():
    torch.set_default_tensor_type(torch.FloatTensor)
    # net = model.Net().cuda()

    net = torchvision.models.resnet18(pretrained=True)
    num_ftrs = net.fc.in_features
    net.fc = nn.Linear(num_ftrs, 2)
    net=net.cuda()

    criterion = nn.MSELoss()
    optimizer = optim.Adam(net.parameters(), lr=0.00001)

    dir, xy = dir_xy.get_all(img_path='data/train', label_path='data/label/train.json')
    loader = ImageLoader.myImageFloder(dir, xy)
    TrainImgLoader = torch.utils.data.DataLoader(loader,batch_size=10,shuffle = True)

    dir, xy = dir_xy.get_all(img_path='data/test', label_path='data/label/test.json')
    loader2 = ImageLoader.myImageFloder(dir, xy)
    TestImgLoader = torch.utils.data.DataLoader(loader2)

    train_test.train(net=net.cuda(), criterion=criterion, optimizer=optimizer, TrainImgLoader=TrainImgLoader,
                     TestImgLoader=TestImgLoader)
Example #24
0
def main(m):
    best_error = 100
    opt = parser_params()

    if opt.dataset == 'cifar10':
        train_loader, test_loader = cifar10_dataloaders(
            batch_size=opt.batch_size, num_workers=opt.num_workers)
        n_cls = 10
    else:
        raise NotImplementedError(opt.dataset)

    print(opt.model[m])
    model = model_dict[opt.model[m]](num_classes=n_cls)

    optimizer = optim.SGD(model.parameters(),
                          lr=opt.learning_rate,
                          momentum=opt.momentum,
                          weight_decay=opt.weight_decay)
    criterion = nn.CrossEntropyLoss()

    if torch.cuda.is_available():
        model = model.cuda()
        criterion = criterion.cuda()
        cudnn.benchmark = True

    for epoch in range(1, opt.epochs + 1):

        if m == 4 and epoch == 1:
            opt.learning_rate = 0.01
        else:
            opt.learning_rate = 0.1

        adjust_learning_rate(epoch, opt, optimizer)
        print("==> training...")

        train_error, train_loss = train(epoch, train_loader, model, criterion,
                                        optimizer, list_loss_train[m])
        print('epoch {} | train_loss: {}'.format(epoch, train_loss))
        print('epoch {} | train_error: {}'.format(epoch, train_error))

        test_error, test_loss = test(test_loader, model, criterion,
                                     list_loss_test[m])
        print('epoch {} | test_loss: {}'.format(epoch, test_loss))
        print('epoch {} | test_error: {}'.format(epoch, test_error))
        print('iterations: {}'.format(epoch * len(train_loader)))

        if best_error > test_error:
            best_error = test_error

    print('Min error: ', best_error)
def run_joint_tasks(data_dir):
    """
    Train and test for all tasks but the trained model is built using training data from all tasks.
    """
    print("Jointly train and test for all tasks ...")
    tasks = range(20)

    # Parse training data
    train_data_path = []
    for t in tasks:
        train_data_path += glob.glob('%s/qa%d_*_train.txt' % (data_dir, t + 1))

    dictionary = {"nil": 0}
    train_story, train_questions, train_qstory = parse_babi_task(train_data_path, dictionary, False)

    # Parse test data for each task so that the dictionary covers all words before training
    for t in tasks:
        test_data_path = glob.glob('%s/qa%d_*_test.txt' % (data_dir, t + 1))
        parse_babi_task(test_data_path, dictionary, False)  # ignore output for now

    general_config = BabiConfigJoint(train_story, train_questions, dictionary)
    memory, model, loss = build_model(general_config)

    if general_config.linear_start:
        train_linear_start(train_story, train_questions, train_qstory, memory, model, loss, general_config)
    else:
        train(train_story, train_questions, train_qstory, memory, model, loss, general_config)

    # Test on each task
    for t in tasks:
        print("Testing for task %d ..." % (t + 1))
        test_data_path = glob.glob('%s/qa%d_*_test.txt' % (data_dir, t + 1))
        dc = len(dictionary)
        test_story, test_questions, test_qstory = parse_babi_task(test_data_path, dictionary, False)
        assert dc == len(dictionary)  # make sure that the dictionary already covers all words

        test(test_story, test_questions, test_qstory, memory, model, loss, general_config)
def run_task(data_dir, task_id):
    """
    Train and test for each task
    """
    print("Train and test for task %d ..." % task_id)

    # Parse data
    train_files = glob.glob('%s/qa%d_*_train.txt' % (data_dir, task_id))
    test_files = glob.glob('%s/qa%d_*_test.txt' % (data_dir, task_id))

    dictionary = {"nil": 0}
    train_story, train_questions, train_qstory = parse_babi_task(train_files, dictionary, False)
    test_story, test_questions, test_qstory = parse_babi_task(test_files, dictionary, False)

    general_config = BabiConfig(train_story, train_questions, dictionary)

    memory, model, loss = build_model(general_config)

    if general_config.linear_start:
        train_linear_start(train_story, train_questions, train_qstory, memory, model, loss, general_config)
    else:
        train(train_story, train_questions, train_qstory, memory, model, loss, general_config)

    test(test_story, test_questions, test_qstory, memory, model, loss, general_config)
def run_tableQA(data_path, model_file):
    """
    Train and test for table QA
    """

    # Parse data
    train_files = glob.glob(data_path.format('train'))
    test_files = glob.glob(data_path.format('test'))
    # SV: init dict with pre-trained vectors, e.g. from fastText
    # dictionary = fasttext.load_model(EMBEDDINGS_MODEL_PATH)
    dictionary = {"nil": 0}
    train_story, train_questions, train_qstory = parse_babi_task(
        train_files, dictionary, False)
    test_story, test_questions, test_qstory = parse_babi_task(
        test_files, dictionary, False)
    # print test_questions
    print 'Dictionary:', len(dictionary)
    general_config = BabiConfig(train_story, train_questions, dictionary)

    memory, model, loss = build_model(general_config)

    if general_config.linear_start:
        train_linear_start(train_story, train_questions, train_qstory, memory,
                           model, loss, general_config)
    else:
        train(train_story, train_questions, train_qstory, memory, model, loss,
              general_config)

    test(test_story, test_questions, test_qstory, memory, model, loss,
         general_config)

    # save_model
    with gzip.open(model_file, "wb") as f:
        print("Saving model to file %s ..." % model_file)
        reversed_dict = dict((ix, w) for w, ix in dictionary.items())
        pickle.dump((reversed_dict, memory, model, loss, general_config), f)
Example #28
0
def main():
    
    # Train classifiers using training samples
    train_pos_dir = "VJ_dataset\\trainset\\faces"
    train_neg_dir = "VJ_dataset\\trainset\\non-faces"
    rounds = 10
    
    cascade = train( train_pos_dir, train_neg_dir, rounds )
    
    # Save training results
    save_cascade(cascade)
    
    # Test with test samples
    test_pos_dir = "VJ_dataset\\testset\\faces"
    test_neg_dir = "VJ_dataset\\testset\\faces"
    
    test( test_pos_dir, test_neg_dir, cascade, 10 )
Example #29
0
def run(subject, steps):
    # ===========================PROCESSING START===============================
    processing_start = time.time()

    X_Data, Y_Data = process_data(subject, steps)

    enc = preprocessing.OneHotEncoder(categories='auto')
    enc.fit(Y_Data)
    Y_Data = enc.transform(Y_Data).toarray()

    X_train, X_test, Y_train, Y_test = train_test_split(X_Data,
                                                        Y_Data,
                                                        test_size=0.25)

    processing_end = time.time()
    print("Preprocessed data in: %.2f sec\n" %
          (processing_end - processing_start))
    # ===========================TRAINING START===============================

    # train_start = time.time()

    model = train(X_train, Y_train)
    # save_model(model, "model_roi1_6steps.json")
    # model = load_model("model_roi1_5steps.json")
    f1, acc = evaluate(model, X_test, Y_test)

    # train_end = time.time()
    # print("Train and test in: %.2f sec" % (train_end - train_start))

    # ===========================TRAINING END===============================

    # result = {
    #     "processing_start": processing_start,
    #     "processing_end": processing_end,
    #     "train_start": train_start,
    #     "train_end": train_end
    # }
    #
    # with open("timestamps.json", "w") as outfile:
    #     json.dump(result, outfile)

    return subject, steps, f1, acc
Example #30
0
def run_epochs(model, train_dataloader, validation_dataloader, optimizer,
               scheduler, epochs):
    start_time = time.time()

    train_losses, train_accs = [], []
    test_losses, test_accs = [], []

    for epoch in range(epochs):
        print('======== Epoch %d ========' % epoch)

        train_loss, train_acc = train(model, train_dataloader, optimizer,
                                      scheduler)
        train_losses.append(train_loss)
        train_accs.append(train_acc)

        test_loss, test_acc = test(model, validation_dataloader)
        test_losses.append(test_loss)
        test_accs.append(test_acc)

    print("Total training took %.2f seconds" % (time.time() - start_time))

    return train_losses, train_accs, test_losses, test_accs
Example #31
0
def run(model, optimizer, criterion, train_loader, dev_loader, nepochs):
    train_losses, train_accs = [], []
    test_losses, test_accs = [], []
    epochs = []

    for e in range(nepochs):
        print('----- EPOCH %d ------- \n' % e)
        start_time = time.time()

        # Train
        train_loss, train_acc = train(train_loader, model, criterion,
                                      optimizer)
        train_losses.append(train_loss)
        train_accs.append(train_acc)

        # Test
        test_loss, test_acc = test(dev_loader, model, criterion)
        test_losses.append(test_loss)
        test_accs.append(test_acc)

    print(train_loss, train_acc, test_loss, test_acc)
    return train_losses, train_accs, test_losses, test_accs
Example #32
0
print("Loading %s" % data_cv_path)
data_cv = pickle.load(open(data_cv_path, 'rb'))
data_cv_splits = data_cv['cv_splits']
results = []

### 3. Sets-Up Main Loop
for k, data in data_cv_splits.items():
	print("*******************************************")
	print("************** SPLIT (%d/%d) **************" % (k, len(data_cv_splits.items())))
	print("*******************************************")
	if os.path.exists(os.path.join(opt.checkpoints_dir, opt.exp_name, opt.model_name, '%s_%d_patch_pred_train.pkl' % (opt.model_name, k))):
		print("Train-Test Split already made.")
		continue

	### 3.1 Trains Model
	model, optimizer, metric_logger = train(opt, data, device, k)

	### 3.2 Evalutes Train + Test Error, and Saves Model
	loss_train, cindex_train, pvalue_train, surv_acc_train, grad_acc_train, pred_train = test(opt, model, data, 'train', device)
	loss_test, cindex_test, pvalue_test, surv_acc_test, grad_acc_test, pred_test = test(opt, model, data, 'test', device)

	if opt.task == 'surv':
		print("[Final] Apply model to training set: C-Index: %.10f, P-Value: %.10e" % (cindex_train, pvalue_train))
		logging.info("[Final] Apply model to training set: C-Index: %.10f, P-Value: %.10e" % (cindex_train, pvalue_train))
		print("[Final] Apply model to testing set: C-Index: %.10f, P-Value: %.10e" % (cindex_test, pvalue_test))
		logging.info("[Final] Apply model to testing set: cC-Index: %.10f, P-Value: %.10e" % (cindex_test, pvalue_test))
		results.append(cindex_test)
	elif opt.task == 'grad':
		print("[Final] Apply model to training set: Loss: %.10f, Acc: %.4f" % (loss_train, grad_acc_train))
		logging.info("[Final] Apply model to training set: Loss: %.10f, Acc: %.4f" % (loss_train, grad_acc_train))
		print("[Final] Apply model to testing set: Loss: %.10f, Acc: %.4f" % (loss_test, grad_acc_test))
Example #33
0
  def train(self):
    """
    Train MemN2N model using training data for tasks.
    """
    #np.random.seed(42)  # for reproducing
    np.random.seed(self.rnd_seed)  # for reproducing
    print("np.random.seed: %d" % self.rnd_seed)
    assert self.data_dir is not None, "data_dir is not specified."
    print("Reading data from %s ..." % self.data_dir)

    # Parse training data
    train_data_path = glob.glob('%s/qa*_*_train.txt' % self.data_dir)
    dictionary = {"nil": 0}
    train_story, train_questions, train_qstory = parse_babi_task(train_data_path, dictionary, False)

    # Parse test data just to expand the dictionary so that it covers all words in the test data too
    test_data_path = glob.glob('%s/qa*_*_test.txt' % self.data_dir)
    parse_babi_task(test_data_path, dictionary, False)

    # Get reversed dictionary mapping index to word
    self.reversed_dict = dict((ix, w) for w, ix in dictionary.items())

    # Construct model
    #self.general_config = Babi10kConfigJoint(train_story, train_questions, dictionary)
    self.general_config = BabiConfigJoint(train_story, train_questions, dictionary)
    self.memory, self.model, self.loss = build_model(self.general_config)

    # Train model
    if self.general_config.linear_start:
      print('We will use LS training')
      self.best_model, self.best_memory = \
        train_linear_start(train_story, 
                           train_questions, 
                           train_qstory, 
                           self.memory, 
                           self.model, 
                           self.loss, 
                           self.general_config, 
                           self.log_path)
    else:
      train_logger = open(os.path.join(self.log_path, 'train.log'), 'w')
      train_logger.write('epoch batch_iter lr loss err\n')
      train_logger.flush()
      val_logger = open(os.path.join(self.log_path, 'val.log'), 'w')
      val_logger.write('epoch batch_iter lr loss err\n')
      val_logger.flush()
      global_batch_iter = 0
      train_logger, val_logger, self.best_model, self.best_memory, _ = \
        train(train_story, 
              train_questions, 
              train_qstory,
              self.memory, 
              self.model, 
              self.loss, 
              self.general_config,
              train_logger, 
              val_logger,
              global_batch_iter)

      train_logger.close()
      val_logger.close()

    # Save model
    self.save_model()
            criterion      = nn.CrossEntropyLoss(reduction='none')
            optim_backbone = optim.SGD(models['backbone'].parameters(), lr=LR, 
                momentum=MOMENTUM, weight_decay=WDECAY)
 
            sched_backbone = lr_scheduler.MultiStepLR(optim_backbone, milestones=MILESTONES)
            optimizers = {'backbone': optim_backbone}
            schedulers = {'backbone': sched_backbone}
            if method == 'lloss':
                optim_module   = optim.SGD(models['module'].parameters(), lr=LR, 
                    momentum=MOMENTUM, weight_decay=WDECAY)
                sched_module   = lr_scheduler.MultiStepLR(optim_module, milestones=MILESTONES)
                optimizers = {'backbone': optim_backbone, 'module': optim_module}
                schedulers = {'backbone': sched_backbone, 'module': sched_module}
            
            # Training and testing
            train(models, method, criterion, optimizers, schedulers, dataloaders, args.no_of_epochs, EPOCHL)
            acc = test(models, EPOCH, method, dataloaders, mode='test')
            print('Trial {}/{} || Cycle {}/{} || Label set size {}: Test acc {}'.format(trial+1, TRIALS, cycle+1, CYCLES, len(labeled_set), acc))
            np.array([method, trial+1, TRIALS, cycle+1, CYCLES, len(labeled_set), acc]).tofile(results, sep=" ")
            results.write("\n")


            if cycle == (CYCLES-1):
                # Reached final training cycle
                print("Finished.")
                break
            # Get the indices of the unlabeled samples to train on next cycle
            arg = query_samples(models, method, data_unlabeled, subset, labeled_set, cycle, args)

            # Update the labeled dataset and the unlabeled dataset, respectively
            labeled_set += list(torch.tensor(subset)[arg][-ADDENDUM:].numpy())