<commit_before># -*- coding: utf-8 -*-
"""
Created on Mon Jan 09 20:49:04 2017

@author: sakurai
"""

import os
import time
import copy
import ConfigParser
import numpy as np
import matplotlib.pyplot as plt

import chainer
import chainer.functions as F
from chainer import optimizers
from chainer.dataset.convert import concat_examples
from tqdm import tqdm
import colorama

from n_pair_mc_loss import n_pair_mc_loss
import common
import chainer_datasets
from models import ModifiedGoogLeNet

colorama.init()


def worker_load_data(queue, stream):
    infinite_iterator = stream.get_epoch_iterator()
    while True:
        x_data, c_data = next(infinite_iterator)
        queue.put((x_data, c_data))


if __name__ == '__main__':
    script_filename = os.path.splitext(os.path.basename(__file__))[0]
    device = 0
    xp = chainer.cuda.cupy
    config_parser = ConfigParser.SafeConfigParser()
    config_parser.read('config')
    log_dir_path = os.path.expanduser(config_parser.get('logs', 'dir_path'))

    p = common.Logger(log_dir_path)  # hyperparameters
    p.learning_rate = 0.0001  # 0.0001 is good
    p.batch_size = 120
    p.out_dim = 64
    p.loss_l2_reg = 0.001  # penalty for the norm of the output vector
    p.l2_weight_decay = 0.001
    p.crop_size = 224
    p.num_epochs = 40
    p.num_batches_per_epoch = 500
    p.distance_type = 'euclidean'  # 'euclidean' or 'cosine'

    ##########################################################
    # load database
    ##########################################################
    iters = chainer_datasets.get_iterators(p.batch_size)
    iter_train, iter_train_eval, iter_test = iters

    ##########################################################
    # construct the model
    ##########################################################
    model = ModifiedGoogLeNet(p.out_dim).to_gpu()
    model = model.to_gpu()
    optimizer = optimizers.Adam(p.learning_rate)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(p.l2_weight_decay))

    logger = common.Logger(log_dir_path)
    logger.soft_test_best = [0]
    time_origin = time.time()
    try:
        for epoch in range(p.num_epochs):
            time_begin = time.time()
            epoch_losses = []

            for i in tqdm(range(p.num_batches_per_epoch)):
                # the first half　of a batch are the anchors and the latters
                # are the positive examples corresponding to each anchor
                batch = next(iter_train)
                x_data, c_data = concat_examples(batch, device)
                y = model(x_data, train=True)
                y_a, y_p = F.split_axis(y, 2, axis=0)

                loss = n_pair_mc_loss(y_a, y_p, p.loss_l2_reg)
                optimizer.zero_grads()
                loss.backward()
                optimizer.update()

                loss_data = loss.data.get()
                epoch_losses.append(loss_data)
                y = y_a = y_p = loss = None

            loss_average = np.mean(epoch_losses)<commit_msg>Use RMSProp<commit_after># -*- coding: utf-8 -*-
"""
Created on Mon Jan 09 20:49:04 2017

@author: sakurai
"""

import os
import time
import copy
import ConfigParser
import numpy as np
import matplotlib.pyplot as plt

import chainer
import chainer.functions as F
from chainer import optimizers
from chainer.dataset.convert import concat_examples
from tqdm import tqdm
import colorama

from n_pair_mc_loss import n_pair_mc_loss
import common
import chainer_datasets
from models import ModifiedGoogLeNet

colorama.init()


def worker_load_data(queue, stream):
    infinite_iterator = stream.get_epoch_iterator()
    while True:
        x_data, c_data = next(infinite_iterator)
        queue.put((x_data, c_data))


if __name__ == '__main__':
    script_filename = os.path.splitext(os.path.basename(__file__))[0]
    device = 0
    xp = chainer.cuda.cupy
    config_parser = ConfigParser.SafeConfigParser()
    config_parser.read('config')
    log_dir_path = os.path.expanduser(config_parser.get('logs', 'dir_path'))

    p = common.Logger(log_dir_path)  # hyperparameters
    p.learning_rate = 0.0001  # 0.0001 is good
    p.batch_size = 120
    p.out_dim = 64
    p.loss_l2_reg = 0.001  # penalty for the norm of the output vector
    p.l2_weight_decay = 0.001
    p.crop_size = 224
    p.num_epochs = 40
    p.num_batches_per_epoch = 500
    p.distance_type = 'euclidean'  # 'euclidean' or 'cosine'

    ##########################################################
    # load database
    ##########################################################
    iters = chainer_datasets.get_iterators(p.batch_size)
    iter_train, iter_train_eval, iter_test = iters

    ##########################################################
    # construct the model
    ##########################################################
    model = ModifiedGoogLeNet(p.out_dim).to_gpu()
    model = model.to_gpu()
    optimizer = optimizers.RMSprop(p.learning_rate)
    optimizer.setup(model)
    optimizer.add_hook(chainer.optimizer.WeightDecay(p.l2_weight_decay))

    logger = common.Logger(log_dir_path)
    logger.soft_test_best = [0]
    time_origin = time.time()
    try:
        for epoch in range(p.num_epochs):
            time_begin = time.time()
            epoch_losses = []

            for i in tqdm(range(p.num_batches_per_epoch)):
                # the first half　of a batch are the anchors and the latters
                # are the positive examples corresponding to each anchor
                batch = next(iter_train)
                x_data, c_data = concat_examples(batch, device)
                y = model(x_data, train=True)
                y_a, y_p = F.split_axis(y, 2, axis=0)

                loss = n_pair_mc_loss(y_a, y_p, p.loss_l2_reg)
                optimizer.zero_grads()
                loss.backward()
                optimizer.update()

                loss_data = loss.data.get()
                epoch_losses.append(loss_data)
                y = y_a = y_p = loss = None

            loss_average = np.mean(epoch_losses)
{'fraction_non_alphanumeric': 0.15826862712704445, 'fraction_numerical': 0.017511977531802413, 'mean_word_length': 2.9209844559585494, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '575169', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1895, 'n_words': 422}