コード例 #1
0
def get_input_decay(name):
    """
    Generates the appropriate decay function for a given variable. The decays
    that can be generated are static (no decay over time), linear and
    exponential.
    :param name: name of the variable that will be generated
    :return: decay function for the variable with appropriate parameters
    """
    decay = input('What kind of decay for {}? [s/l/e]'.format(name)) or 'e'
    if decay == 's':
        value = float(input('Static value: '))
        return StaticDecay(value)
    if decay == 'l':
        value = float(input('Start value: '))
        rate = float(input('Rate:'))
        return LinearDecay(value, rate)
    if decay == 'e':
        value = float(input('Start value: '))
        rate = float(input('Rate:'))
        return ExponentialDecay(value, rate)
    exit('Not a valid option!')
コード例 #2
0
import matplotlib.pyplot as plt
import os
from decay import StaticDecay, LinearDecay, ExponentialDecay
from neighborhood import gaussian, bubble

DEFAULTS_EXP = (
    'rgbg_beergardens',  #dataset
    2,  #number of neurons per city
    5000,  #iterations
    1000,  #k
    1000,  #plot_k
    bubble,  #neighborhood
    ExponentialDecay(0.7, 0.9999),  #learning rate
    ExponentialDecay(30, 0.999))  #radius
DEFAULTS_LIN = ('rgbg_beergardens', 8, 40000, 5000, 5000, gaussian,
                LinearDecay(0.9, 0.0000089), LinearDecay(20, 0.0005))

DEFAULT_STA = ('rgbg_beergardens', 2, 50000, 5000, 5000, gaussian,
               StaticDecay(0.5), StaticDecay(1))

plt.figure()


def plot_map(cities, neurons, iteration):
    """
    Generates the required map of cities and neurons at a given moment and
    stores the result in a png image. The map contains all the cities
    represented as red dots and all the neurons as green, crossed by a line
    dots. The method plots the iteration in which the snapshot was taken.
    :param cities: the cities to be plotted, passed as a list of (x, y)
    coordinates
コード例 #3
0
ファイル: helper.py プロジェクト: unosonu/ntnu-som
    ('uruguay', 8, 10000, 1000, 500, bubble, ExponentialDecay(0.7, 0.9999),
     ExponentialDecay(734 * 8 / 10, 0.999)),
    'western_sahara':
    ('western_sahara', 8, 500, 50, 50, gaussian, ExponentialDecay(0.7, 0.999),
     ExponentialDecay(29 * 8 / 10, 0.995)),
    'qatar': ('qatar', 8, 5000, 500, 500, gaussian,
              ExponentialDecay(0.9,
                               0.9999), ExponentialDecay(194 * 8 / 10, 0.997)),
    'djibouti': ('djibouti', 8, 1000, 50, 50, gaussian,
                 ExponentialDecay(0.7,
                                  0.999), ExponentialDecay(89 * 8 / 10, 0.995))
}

DEFAULTS_LIN = {
    'uruguay': ('uruguay', 8, 10000, 1000, 500, gaussian,
                LinearDecay(0.9, 0.000089), LinearDecay(734 * 8 / 10, 0.055)),
    'western_sahara': ('western_sahara', 8, 500, 50, 50, gaussian,
                       LinearDecay(0.7, 0.001), LinearDecay(29 * 8 / 10,
                                                            0.04)),
    'qatar': ('qatar', 8, 10000, 500, 500, gaussian,
              LinearDecay(0.7, 0.000065), LinearDecay(194 * 8 / 10, 0.015)),
    'djibouti':
    ('djibouti', 8, 5000, 50, 50, gaussian, LinearDecay(0.7, 0.0001),
     LinearDecay(89 * 8 / 10, 0.014))
}

DEFAULT_STA = {
    'uruguay': ('uruguay', 8, 10000, 1000, 200, gaussian, StaticDecay(0.4),
                StaticDecay(500)),
    'western_sahara': ('western_sahara', 6, 5000, 1000, 200, gaussian,
                       StaticDecay(0.3), StaticDecay(5)),
コード例 #4
0
    def train(self, generator, discriminator, tool):
        print("using device: %s" % (self.device))
        '''
        print ("discriminator parameters!")
        utils.print_parameter_list(discriminator)
        print ("______________________")
        input(">")
        '''

        # build training and validation data
        tool.close_corruption()
        tool.set_batch_size(self.hps.batch_size)
        tool.build_train_data(self.hps.unpaired_train_data,
                              self.hps.paired_train_data, None)
        tool.build_valid_data(self.hps.unpaired_valid_data,
                              self.hps.paired_valid_data, None)

        print("train batch num: %d" % (tool.train_batch_num))
        print("valid batch num: %d" % (tool.valid_batch_num))

        # training logger
        logger = StyInsLogger('train')
        logger.set_batch_num(tool.train_batch_num)
        logger.set_log_steps(self.hps.log_steps)
        logger.set_log_path(self.hps.train_log_path)
        logger.set_rate('learning_rate', 0.0)
        logger.set_rate('teach_ratio', 1.0)

        # build optimizer
        optGen = torch.optim.Adam(generator.parameters(),
                                  lr=0.0,
                                  betas=(0.9, 0.999))
        optDis = torch.optim.Adam(discriminator.parameters(),
                                  lr=0.0,
                                  betas=(0.5, 0.999))

        optimizerGen = ISRScheduler(optimizer=optGen,
                                    warmup_steps=self.hps.warmup_steps,
                                    max_lr=self.hps.max_lr,
                                    min_lr=self.hps.min_lr,
                                    init_lr=self.hps.init_lr,
                                    beta=0.75)

        optimizerDis = ISRScheduler(optimizer=optDis,
                                    warmup_steps=self.hps.warmup_steps,
                                    max_lr=self.hps.max_lr,
                                    min_lr=self.hps.min_lr,
                                    init_lr=self.hps.init_lr,
                                    beta=0.5)

        # ----------------------------------------------
        criterion = Criterion(self.hps.pad_idx)

        # when paired data is available, at the beginning,
        #   we give larger weight for supervised loss and then decrease it.
        self.decay_tool = LinearDecay(
            burn_down_steps=self.hps.superv_burn_down_steps,
            decay_steps=self.hps.superv_decay_steps,
            max_v=self.hps.superv_max,
            min_v=self.hps.superv_min)

        generator.train()
        discriminator.train()

        print("In training mode: %d, %d" %
              (generator.training, discriminator.training))
        #input("Please check the parameters and press enter to continue>")

        # --------------------------------------------------------------------------------------
        for epoch in range(1, self.hps.max_epoches + 1):

            self.run_train(generator, discriminator, tool, optimizerGen,
                           optimizerDis, criterion, logger)

            if (self.hps.save_epoches >= 1) and (epoch %
                                                 self.hps.save_epoches) == 0:
                # save checkpoint
                print("saving model...")
                utils.save_checkpoint_multiple(self.hps.ckpt_path, generator,
                                               discriminator, optimizerGen,
                                               optimizerDis, epoch)

            if epoch % self.hps.valid_epoches == 0:
                print("run validation...")
                generator.eval()
                discriminator.eval()
                print("in training mode: %d, %d" %
                      (generator.training, discriminator.training))
                self.run_validation(epoch, generator, discriminator, criterion,
                                    tool, optimizerGen.rate())
                generator.train()
                discriminator.train()
                print("validation Done, mode: %d, %d" %
                      (generator.training, discriminator.training))

            logger.add_epoch()

            # each epoch, we rebuild the data, including shuffling the data, re-assigning
            #   transfer direction and resampling instances for each batch
            print("shuffle data...")
            tool.shuffle_training_data()
コード例 #5
0
ファイル: helper.py プロジェクト: Karimkarfax/ML_Sheet_3
                        ExponentialDecay(734*8/10, 0.999)),
            'western_sahara': ('western_sahara', 8, 500, 50, 50, gaussian,
                        ExponentialDecay(0.7, 0.999),
                        ExponentialDecay(29*8/10, 0.995)),
            'qatar': ('qatar', 8, 5000, 500, 500, gaussian,
                        ExponentialDecay(0.9, 0.9999),
                        ExponentialDecay(194*8/10, 0.997)),
            'djibouti': ('djibouti', 8, 1000, 50, 50, gaussian,
                        ExponentialDecay(0.7, 0.999),
                        ExponentialDecay(89*8/10, 0.995)),
            'rgbg_beergardens': ('rgbg_beergardens', 8, 500, 50, 50, gaussian,
                        ExponentialDecay(0.7, 0.999),
                        ExponentialDecay(17*8/10, 0.995))}

DEFAULTS_LIN = {'uruguay': ('uruguay', 8, 10000, 1000, 500, gaussian,
                        LinearDecay(0.9, 0.000089),
                        LinearDecay(734*8/10, 0.055)),
            'western_sahara': ('western_sahara', 8, 500, 50, 50, gaussian,
                        LinearDecay(0.7, 0.001),
                        LinearDecay(29*8/10, 0.04)),
            'qatar': ('qatar', 8, 10000, 500, 500, gaussian,
                        LinearDecay(0.7, 0.000065),
                        LinearDecay(194*8/10, 0.015)),
            'djibouti': ('djibouti', 8, 5000, 50, 50, gaussian,
                        LinearDecay(0.7, 0.0001),
                        LinearDecay(89*8/10, 0.014)),
            'rgbg_beergardens': ('rgbg_beergardens', 8, 500, 50, 50, gaussian,
                        LinearDecay(0.7, 0.001))}

DEFAULT_STA = {'uruguay': ('uruguay', 8, 10000, 1000, 200, gaussian,
                        StaticDecay(0.4),