def Displayer(Cfg): ''' This function is used to display the parameters.\n Params:\n - Cfg: The configurator. ''' # Indicate whether the Cfg is a configurator or not. assert type(Cfg) is type(Config()), 'Please input the configurator.' # Set the displayer. displayer = [''.ljust(20) + f'{param}:'.ljust(30) + f'{Cfg[param]}' for param in Cfg.keys()] # Return the result of the displayer. return "\n".join(displayer)
def Generator(paramsDir = './Params.txt'): ''' This function is used to generate the configurator of parameters.\n Params:\n - paramsDir: The directory of the parameters' default setting file. ''' # Create the configurator of parameters. Cfg = Config() # Get the names of parameters. file = open(paramsDir) lines = file.readlines() # Initialize the parameters. for line in lines: Cfg[line.split("\n")[0].split(":")[0]] = Handler.Convertor(line.split("\n")[0].split(":")[1]) # Return the dictionary of the parameters. return Cfg
def Parser(Cfg): ''' This function is used to parse the parameters.\n Params:\n - Cfg: The configurator. ''' # Indicate whether the Cfg is a configurator or not. assert type(Cfg) is type(Config()), 'Please input the configurator.' # Create the parameters' parser. parser = argparse.ArgumentParser(description = 'Parameters Parser') # Add the parameters into the parser. for param in Cfg.keys(): parser.add_argument(f'-{param}', f'--{param}', f'-{param.lower()}', f'--{param.lower()}', f'-{param.upper()}', f'--{param.upper()}', dest = param, type = type(Cfg[param]), default = Cfg[param], help = f'The type of {param} is {type(Cfg[param])}') # Parse the parameters. params = vars(parser.parse_args()) # Update the configurator. Cfg.update(params) # Return the configurator. return Cfg
def ArgParser(): ''' This function is used to parsing the arguments. ''' # Getting the configurator. CFG = Cfg # Setting the arguments' parser. parser = argparse.ArgumentParser(description = 'Argument Parser') # Setting the arguments. parser.add_argument('-lr', '--learningRate', type = float, dest = 'lr', default = CFG.lr, help = 'The learning rate should be the float and constrain in [0, 1].') parser.add_argument('-momentum', '--momentum', type = float, dest = 'momentum', default = CFG.momentum, help = 'The momentum should be the float and constrain in [0, 1].') parser.add_argument('-wd', '--weightDecay', type = float, dest = 'wd', default = CFG.wd, help = 'The weight decay should be the float and constrain in [0, 1].') parser.add_argument('-smoothing', '--smoothing', type = float, dest = 'smoothing', default = CFG.smoothing, help = 'The smoothing should be the float and constrain in [0, 1]. (Other values would be seen as non-LSR)') parser.add_argument('-channels', '--channels', type = int, dest = 'channels', default = CFG.channels, help = 'The channels should be the integer and only access three values 78, 109 and 154.') parser.add_argument('-bs', '--batchSize', type = int, dest = 'bs', default = CFG.bs, help = 'The batch size should be the integer and larger than 1.') parser.add_argument('-cs', '--classSize', type = int, dest = 'cs', default = CFG.cs, help = 'The class size should be the integer and larger than 1.') parser.add_argument('-ep', '--epoches', type = int, dest = 'epoches', default = CFG.epoches, help = 'The epoches should be the integer and larger than 1.') parser.add_argument('-seed', '--seed', type = int, dest = 'seed', default = CFG.seed, help = 'The random seed should be the integer and larger then -1.') parser.add_argument('-gpu', '--GPUID', type = int, dest = 'GPUID', default = CFG.GPUID, help = 'The GPU ID should be the integer and larger than -1. (-1 means applying the Data Parallel training!)') parser.add_argument('-ggm', '--graphGenerateMethod', type = str, dest = 'ggm', default = CFG.ggm, help = 'The graph generate method should be the string. (s for simple way || others for complex way)') parser.add_argument('-gt', '--graphType', type = str, dest = 'gt', default = CFG.gt, help = 'The graph type should be the string and can only be BA, ER and WS.') parser.add_argument('-nodes', '--nodes', type = int, dest = 'nodes', default = CFG.nodes, help = 'The nodes should be the integer and larger than 3.') parser.add_argument('-initialNode', '--initialNode', type = int, dest = 'initialNode', default = CFG.initialNode, help = 'The initial nodes should be integer and smaller than nodes.') parser.add_argument('-e', '-edge', type = int, dest = 'e', default = CFG.e, help = 'The edge should be the integer and larger than 0.') parser.add_argument('-k', '--k', type = int, dest = 'k', default = CFG.k, help = 'The k should be the integer and smaller than nodes.') parser.add_argument('-p', '--prob', type = float, dest = 'p', default = CFG.p, help = 'The prob should be the float and constrain in [0, 1].') parser.add_argument('-graphDir', '--graphDir', type = str, dest = 'graphDir', default = CFG.graphDir, help = 'The graph dir should be the string.') parser.add_argument('-modelDir', '--modelDir', type = str, dest = 'modelDir', default = CFG.modelDir, help = 'The model dir should be the string.') parser.add_argument('-logDir', '--logDir', type = str, dest = 'logDir', default = CFG.logDir, help = 'The log dir should be the string.') parser.add_argument('-dataDir', '--dataDir', type = str, dest = 'dataDir', default = CFG.dataDir, help = 'The data dir should be the string.') # Parsing the argument. args = vars(parser.parse_args()) # Handling the argument. args = Configurator.Handler(args) # Updating the configurator. CFG.update(args) # Returning the configurator. return Config(CFG)
#============================================================================================# # Copyright: JarvisLee # Date: 2020/11/22 # File Name: Config.py # Description: This file is used to setting the hyperparameters and directories. #============================================================================================# # Importing the necessary library. import os import argparse from easydict import EasyDict as Config # Creating the configurator. Cfg = Config() # Setting the default values for the hyperparameters. # The default value of the class size. Cfg.cs = 3 # The default value of the learning rate. Cfg.lr = 2e-4 # The default value of the batch size. Cfg.bs = 32 # The default value of the epoches. Cfg.epoches = 100 # The default value of the random seed. Cfg.seed = 1 # The default value of the GPU ID. Cfg.GPUID = -1 # Setting the default values for the directories. # The default value of the model directory.
def argParse(): # Getting the configurator. CFG = Cfg # Creating the arguments' parser. parser = argparse.ArgumentParser(description='Argument Parser') # Setting the argument. parser.add_argument('-vs', '--vocabSize', type=int, dest='vs', default=CFG.vs, help='Integer => [1000, Infinite)') parser.add_argument('-es', '--embeddingSize', type=int, dest='es', default=CFG.es, help='Integer => [100, Infinite)') parser.add_argument('-hs', '--hiddenSize', type=int, dest='hs', default=CFG.hs, help='Integer => [100, Infinite)') parser.add_argument('-cs', '--classSize', type=int, dest='cs', default=CFG.cs, help='Integer => [1, Infinite)') parser.add_argument('-lr', '--learningRate', type=float, dest='lr', default=CFG.lr, help='Float => [0, 1]') parser.add_argument('-beta1', '--beta1', type=float, dest='beta1', default=CFG.beta1, help='Float => [0, 1]') parser.add_argument('-beta2', '--beta2', type=float, dest='beta2', default=CFG.beta2, help='Float => [0, 1]') parser.add_argument('-wd', '--weightDecay', type=float, dest='wd', default=CFG.wd, help='Float => [0, 1]') parser.add_argument('-bs', '--batchSize', type=int, dest='bs', default=CFG.bs, help='Integer => [1, Infinite)') parser.add_argument('-ep', '--epoches', type=int, dest='epoches', default=CFG.epoches, help='Integer => [1, Infinite)') parser.add_argument('-seed', '--seed', type=int, dest='seed', default=CFG.seed, help='Integer => [0, Infinite)') parser.add_argument('-gpu', '--GPUID', type=int, dest='GPUID', default=CFG.GPUID, help='Integer => [0, Infinite)') parser.add_argument('-currentTime', '--currentTime', type=str, dest='currentTime', default=CFG.currentTime, help='Format => Y-m-d-H-M-S') parser.add_argument('-modelDir', '--modelDir', type=str, dest='modelDir', default=CFG.modelDir, help='String') parser.add_argument('-logDir', '--logDir', type=str, dest='logDir', default=CFG.logDir, help='String') parser.add_argument('-dataDir', '--dataDir', type=str, dest='dataDir', default=CFG.dataDir, help='String') # Parsing the argument. args = vars(parser.parse_args()) # Updating the configurator. CFG.update(args) # Returning the configurator. return Config(CFG)
def argParse(): # Getting the configurator. CFG = Cfg # Setting the arguments parser. parser = argparse.ArgumentParser(description='Argument Parser') # Setting the arguments. parser.add_argument('-lrG', '--learningRateG', type=float, dest='lrG', default=CFG.lrG, help='Float => [0, 1]') parser.add_argument('-lrD', '--learningRateD', type=float, dest='lrD', default=CFG.lrD, help='Float => [0, 1]') parser.add_argument('-lt', '--latentSize', type=int, dest='lt', default=CFG.lt, help='Integer => [1, Infinite)') parser.add_argument('-im', '--imageSize', type=int, dest='im', default=CFG.im, help='Integer => [1, Infinite)') parser.add_argument('-ep', '--epoches', type=int, dest='epoches', default=CFG.epoches, help='Integer => [1, Infinite)') parser.add_argument('-bs', '--batchSize', type=int, dest='bs', default=CFG.bs, help='Integer => [1, Infinite)') parser.add_argument('-seed', '--seed', type=int, dest='seed', default=CFG.seed, help='Integer => [0, Infinite)') parser.add_argument('-gpu', '--GPUID', type=int, dest='GPUID', default=CFG.GPUID, help='Integer => [0, Infinite)') parser.add_argument('-modelDir', '--modelDir', type=str, dest='modelDir', default=CFG.modelDir, help='String') parser.add_argument('-logDir', '--logDir', type=str, dest='logDir', default=CFG.logDir, help='String') parser.add_argument('-dataDir', '--dataDir', type=str, dest='dataDir', default=CFG.dataDir, help='String') # Parsing the arguments. args = vars(parser.parse_args()) args['imageSize'] = args['im'] * args['im'] # Updating the configurator. CFG.update(args) # Returning the configurator. return Config(CFG)