train_step = tf.train.AdamOptimizer(learning_rate).minimize(
        binary_cross_entropy)

    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()

    # DONOTCHANGE: Reserved for nsml
    bind_model(sess=sess, config=config)

    # DONOTCHANGE: Reserved for nsml
    if config.pause:
        nsml.paused(scope=locals())

    if config.mode == 'train':
        # 데이터를 로드합니다.
        dataset = KinQueryDataset(DATASET_PATH, config.strmaxlen)
        dataset_len = len(dataset)
        one_batch_size = dataset_len // config.batch
        if dataset_len % config.batch != 0:
            one_batch_size += 1
        # epoch마다 학습을 수행합니다.
        for epoch in range(config.epochs):
            avg_loss = 0.0
            for i, (left_data, right_data,
                    labels) in enumerate(_batch_loader(dataset, config.batch)):
                _, loss = sess.run([train_step, binary_cross_entropy],
                                   feed_dict={
                                       x_1: left_data,
                                       x_2: right_data,
                                       y_: labels
                                   })
示例#2
0
    tf.global_variables_initializer().run()

    # declare saver
    saver = tf.train.Saver()
    # DONOTCHANGE: Reserved for nsml
    bind_model(sess=sess, config=config)

    # DONOTCHANGE: Reserved for nsml
    if config.pause:
        nsml.paused(scope=locals())

    # train mode
    if config.mode == 'train':

        # load dataset
        dataset = KinQueryDataset(DATASET_PATH, config.strmaxlen,
                                  hidden_layer_size)
        # divide by config.batch
        dataset_len = len(dataset)
        one_batch_size = dataset_len // config.batch
        if dataset_len % config.batch != 0:
            one_batch_size += 1

        # train per epoch.
        for epoch in range(config.epochs):

            for i, (data,
                    labels) in enumerate(_batch_loader(dataset, config.batch)):

                _, c = sess.run([train, cost], feed_dict={x: data, y: labels})
                w_v, b_v, h, c, p, a = sess.run(
                    [w, b, hypothesis, cost, prediction, accuracy],
示例#3
0
def train(options):
    device = torch.device("cuda" if options['general']['usecudnn'] else "cpu")
    
    print('initialize model ABCNN' + options['model']['number'])
    if optionsoptions['model']['number'] == '3':
        model = Abcnn3
    elif optionsoptions['model']['number'] == '2':
        model = Abcnn2
    else:
        model = Abcnn1

    #batch_size, emb_dim, sentence_length, filter_w, filter_c=100, layer_size=2
    model = model(options['model']['embeddeddimension'],
                options['model']['strlenmax'],
                options['model']['filterwidth'],
                options['model']['filterchannel'],
                options['model']['layersize'],
                match=options['model']['distance'],
                inception=options['model']['inception'])
    model.apply(weights_init)

    if(options['general']['loadpretrainedmodel']):
        model.load_state_dict(torch.load(options['general']['pretrainedmodelpath']))

    model = model.to(device)

    optimizer = optim.Adam(model.parameters(),
                    lr=options['training']['learningrate'],
                    weight_decay=options['training']['weightdecay'])
    criterion = nn.BCEWithLogitsLoss()

    dataset = KinQueryDataset(options['training']['dataset'], options['model']['strlenmax'], options['model']['embeddeddimension'])
    train_loader = DataLoader(dataset=dataset,
                        batch_size=options['input']['batchsize'],
                        shuffle=options['input']['shuffle'],
                        num_workers=options['input']['numworkers'])
    total_batch = len(train_loader)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.5)

    for epoch in range(options['training']['startepoch'], options['training']['epochs']):
        avg_loss = 0.0
        scheduler.step()
        
        for data, labels in train_loader:
            x1 = data[:, 0, :, :].unsqueeze(1)
            x2 = data[:, 1, :, :].unsqueeze(1)
            
            x1 = x1.to(device)
            x2 = x2.to(device)
            
            labels = labels.to(device)
            predictions = model(x1, x2)
            
            loss = criterion(predictions.float(), labels)
            loss = loss.to(device)
                
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            avg_loss += loss.item()

        print('epoch:', epoch, ' train_loss:', float(avg_loss/total_batch))
        if options['general']['savemodel']:
            torch.save(model.state_dict(), options['general']['pretrainedmodelpath'])
示例#4
0
    main_model.summary()
    main_model.compile(optimizer=optimizers.Adam(lr=0.001, amsgrad=True),
                       loss='binary_crossentropy',
                       metrics=['accuracy'])
    #------------------------------------------------------------------

    # DONOTCHANGE: Reserved for nsml
    bind_model(main_model)

    # DONOTCHANGE: Reserved for nsml
    if config.pause:
        nsml.paused(scope=locals())

    if config.mode == 'train':
        # 데이터를 로드합니다.
        dataset = KinQueryDataset(DATASET_PATH, config.strmaxlen)
        dataset_len = len(dataset)
        one_batch_size = dataset_len // config.batch
        if dataset_len % config.batch != 0:
            one_batch_size += 1
        # epoch마다 학습을 수행합니다.
        for epoch in range(config.epochs):
            avg_loss = 0.0
            avg_acc = 0.0
            dataset.shuffle()
            for i, (data1, data2,
                    labels) in enumerate(_batch_loader(dataset, config.batch)):
                loss, acc = main_model.train_on_batch([data1, data2], labels)
                print('Batch : ', i + 1,
                      '/', one_batch_size, ', BCE in this minibatch: ',
                      float(loss), ', ACC in this minibatch: ', float(acc))
示例#5
0
    #sess = tf.InteractiveSession(config=tf.ConfigProto(log_device_placement=True))
    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()
    tf.local_variables_initializer().run()

    # DONOTCHANGE: Reserved for nsml
    bind_model(sess=sess, config=config)

    # DONOTCHANGE: Reserved for nsml
    if config.pause:
        nsml.paused(scope=locals())

    if config.mode == 'train':
        # 데이터를 로드합니다.
        dataset = KinQueryDataset(DATASET_PATH, config.strmaxlen)
        dataset_len = len(dataset)
        print("Total dataset size:", dataset_len)
        one_batch_size = dataset_len // config.batch
        if dataset_len % config.batch != 0:
            one_batch_size += 1

        iterator = dataset.make_initializable_iterator(config.batch)
        sess.run(iterator.initializer)
        next_element = iterator.get_next()

        # epoch마다 학습을 수행합니다.
        for epoch in range(config.epochs):
            avg_loss = 0.0
            for i in range(one_batch_size):
                data1, data2, d1_len, d2_len, labels = sess.run(next_element)
示例#6
0
    ##############################################################################################################

    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()

    # DONOTCHANGE: Reserved for nsml
    bind_model(sess=sess, config=config)

    # DONOTCHANGE: Reserved for nsml
    if config.pause:
        nsml.paused(scope=locals())

    if config.mode == 'train':
        # 데이터를 로드합니다.
        dataset = KinQueryDataset(DATASET_PATH, max_sentence_length,
                                  max_word_length)
        dataset_len = len(dataset)
        one_batch_size = dataset_len // config.batch
        if dataset_len % config.batch != 0:
            one_batch_size += 1

        if config.debug:
            debugset = KinQueryDataset(DEBUG2_PATH, max_sentence_length,
                                       max_word_length)
            debugset_len = len(debugset)
            one_debug_size = debugset_len // config.batch
            if debugset_len % config.batch != 0:
                one_debug_size += 1

        train_step = 0
        best_acc = 0.0