Пример #1
0
def main():
	parser = optparse.OptionParser()
	parser.add_option("--use-tray-icon", dest="tray_icon", 
		action="store_true", default=False)
	parser.add_option("--no-tray-icon", dest="tray_icon", action="store_false")
	parser.add_option("--hide-main-window", action="store_true", default=False)
	parser.add_option("-m", "--map", action="store", type="string", dest="filename",
		help="Open a map from a given filename (from internal database)")
	parser.add_option("-o", "--open", action="store", type="string", 
		dest="filepath", help="Open a map from a given filename (including path)")
		
	(options, args) = parser.parse_args()
	if not options.tray_icon:
		options.hide_main_window=False
	
	MapBrowser = Browser.Browser (
		start_hidden = options.hide_main_window,
		tray_icon = options.tray_icon
    )
	
	if options.filename != None:
		MapBrowser.open_map_filename (utils.get_save_dir() + options.filename)
	elif options.filepath != None:
		MapBrowser.open_map_filename (options.filepath)

	try:
		gtk.main()
	except:
		print "Exception caught while running.  Dying a death."
		sys.exit(1)
Пример #2
0
    def _download_module(self, module_url):
        request = self.request
        session = request.session
        conn = sword2cnx.Connection(session['login'].service_document_url,
                                    user_name=session['login'].username,
                                    user_pass=session['login'].password,
                                    always_authenticate=True,
                                    download_service_document=False)

        parts = urlparse.urlsplit(module_url)
        path = parts.path.split('/')
        path = path[:path.index('sword')]
        module_url = '%s://%s%s' % (parts.scheme, parts.netloc, '/'.join(path))

        # example: http://cnx.org/Members/user001/m17222/sword/editmedia
        zip_file = conn.get_cnx_module(module_url = module_url,
                                       packaging = 'zip')
         
        save_dir = get_save_dir(request)
        if save_dir is None:
            request.session['upload_dir'], save_dir = create_save_dir(request)
        extract_to_save_dir(zip_file, save_dir)

        cnxml_file = open(os.path.join(save_dir, 'index.cnxml'), 'rb')
        cnxml = cnxml_file.read()
        cnxml_file.close()
        conversionerror = None
        try:
            htmlpreview = cnxml_to_htmlpreview(cnxml)
            save_and_backup_file(save_dir, 'index.html', htmlpreview)
            files = get_files(save_dir)
            save_zip(save_dir, cnxml, htmlpreview, files)
        except libxml2.parserError:
            conversionerror = traceback.format_exc()
            raise ConversionError(conversionerror)
 def __init__(self):
     env_input_size: int = 6
     super().__init__(env_input_size)
     self.post_fn_remote = self.post_milp
     self.get_nn_fn = self.get_nn
     self.plot_fn = self.plot
     self.template_2d: np.ndarray = np.array([[1, 0, 0, 0, 0, 0],
                                              [1, -1, 0, 0, 0, 0]])
     input_boundaries, input_template = self.get_template(0)
     self.input_boundaries: List = input_boundaries
     self.input_template: np.ndarray = input_template
     _, template = self.get_template(1)
     self.analysis_template: np.ndarray = template
     collision_distance = 0
     distance = [Experiment.e(6, 0) - Experiment.e(6, 1)]
     # self.use_bfs = True
     # self.n_workers = 1
     self.rounding_value = 2**10
     self.use_rounding = False
     self.time_horizon = 40000
     self.unsafe_zone: List[Tuple] = [(distance,
                                       np.array([collision_distance]))]
     self.input_epsilon = 0
     # self.nn_path = os.path.join(utils.get_save_dir(),"tune_PPO_stopping_car/PPO_StoppingCar_acc24_00000_0_cost_fn=0,epsilon_input=0_2021-01-21_02-30-49/checkpoint_39/checkpoint-39")
     self.nn_path = os.path.join(
         utils.get_save_dir(),
         "tune_PPO_stopping_car/PPO_StoppingCar_acc24_00001_1_cost_fn=0,epsilon_input=0_2021-01-21_02-30-49/checkpoint_58/checkpoint-58"
     )
Пример #4
0
def main():
	parser = optparse.OptionParser()
	parser.add_option("--use-tray-icon", dest="tray_icon", 
		action="store_true", default=False)
	parser.add_option("--no-tray-icon", dest="tray_icon", action="store_false")
	parser.add_option("--hide-main-window", action="store_true", default=False)
	parser.add_option("-m", "--map", action="store", type="string", dest="filename",
		help="Open a map from a given filename (from internal database)")
	parser.add_option("-o", "--open", action="store", type="string", 
		dest="filepath", help="Open a map from a given filename (including path)")
		
	(options, args) = parser.parse_args()
	if not options.tray_icon:
		options.hide_main_window=False
	
	MapBrowser = Browser.Browser (
		start_hidden = options.hide_main_window,
		tray_icon = options.tray_icon
    )
	
	if options.filename != None:
		MapBrowser.open_map_filename (utils.get_save_dir() + options.filename)
	elif options.filepath != None:
		MapBrowser.open_map_filename (options.filepath)

	try:
		Gtk.main()
	except:
		print "Exception caught while running.  Dying a death."
		sys.exit(1)
Пример #5
0
 def __init__(self):
     env_input_size: int = 4
     super().__init__(env_input_size)
     self.post_fn_remote = self.post_milp
     self.get_nn_fn = self.get_nn
     self.plot_fn = self.plot
     self.template_2d: np.ndarray = np.array([[0, 0, 1, 0], [0, 0, 0, 1]])
     input_boundaries, input_template = self.get_template(0)
     self.input_boundaries: List = input_boundaries
     self.input_template: np.ndarray = input_template
     _, template = self.get_template(1)
     self.analysis_template: np.ndarray = template
     safe_angle = 12 * 2 * math.pi / 360
     theta = [self.e(4, 2)]
     neg_theta = [-self.e(4, 2)]
     self.unsafe_zone: List[Tuple] = [(theta, np.array([-safe_angle])),
                                      (neg_theta, np.array([-safe_angle]))]
     # self.use_rounding = False
     self.rounding_value = 1024
     self.time_horizon = 300
     self.nn_path = os.path.join(
         utils.get_save_dir(),
         "tune_PPO_cartpole/PPO_CartPoleEnv_0205e_00001_1_cost_fn=1,tau=0.001_2021-01-16_20-25-43/checkpoint_3090/checkpoint-3090"
     )
     # self.tau = 0.001
     self.tau = 0.02
 def __init__(self):
     super().__init__()
     self.post_fn_remote = self.post_milp
     self.before_start_fn = self.before_start
     self.show_progress_plot = False
     # self.nn_path = os.path.join(utils.get_save_dir(), "tune_PPO_bouncing_ball/PPO_BouncingBall_c7326_00000_0_2021-01-16_05-43-36/checkpoint_36/checkpoint-36")
     self.nn_path = os.path.join(
         utils.get_save_dir(),
         "tune_PPO_bouncing_ball/PPO_BouncingBall_fb929_00003_3_2021-01-19_00-20-45/checkpoint_10/checkpoint-10"
     )
Пример #7
0
 def __init__(self):
     super().__init__()
     self.post_fn_remote = self.post_milp
     self.before_start_fn = self.before_start
     self.time_horizon = 300
     self.use_rounding = False
     # self.nn_path = os.path.join(utils.get_save_dir(), "tune_PPO_cartpole/PPO_CartPoleEnv_0205e_00001_1_cost_fn=1,tau=0.001_2021-01-16_20-25-43/checkpoint_3090/checkpoint-3090")
     self.nn_path = os.path.join(
         utils.get_save_dir(),
         "tune_PPO_cartpole/PPO_CartPoleEnv_0205e_00000_0_cost_fn=0,tau=0.001_2021-01-16_20-25-43/checkpoint_193/checkpoint-193"
     )
Пример #8
0
    def import_clicked(self, button, other=None, *data):
        chooser = gtk.FileChooserDialog(title=_("Open File"), action=gtk.FILE_CHOOSER_ACTION_OPEN, \
                                                                        buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))

        filtr = gtk.FileFilter ()
        filtr.set_name(_('MAPZ Compressed Map (*.mapz)'))
        filtr.add_pattern('*.mapz')
        chooser.add_filter(filtr)

        response = chooser.run()
        if response == gtk.RESPONSE_OK:
            filename = chooser.get_filename()
            tf = tarfile.open(filename)
            mapname = os.path.join (utils.get_save_dir (), tf.getnames()[0])
            tf.extractall(utils.get_save_dir())
            tf.close()
            map = MapList.new_from_file(mapname)
            map.filename = mapname

        chooser.destroy()
Пример #9
0
    def import_clicked(self, button, other=None, *data):
        chooser = gtk.FileChooserDialog(title=_("Open File"), action=gtk.FILE_CHOOSER_ACTION_OPEN, \
                                                                        buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))

        filtr = gtk.FileFilter ()
        filtr.set_name(_('MAPZ Compressed Map (*.mapz)'))
        filtr.add_pattern('*.mapz')
        chooser.add_filter(filtr)

        response = chooser.run()
        if response == gtk.RESPONSE_OK:
            filename = chooser.get_filename()
            tf = tarfile.open(filename)
            mapname = os.path.join (utils.get_save_dir (), tf.getnames()[0])
            tf.extractall(utils.get_save_dir())
            tf.close()
            map = MapList.new_from_file(mapname)
            map.filename = mapname

        chooser.destroy()
Пример #10
0
	def import_clicked(self, button, other=None, *data):
		chooser = Gtk.FileChooserDialog(title=_("Open File"), action=Gtk.FileChooserAction.OPEN, \
										buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
										
		filtr = Gtk.FileFilter ()
		filtr.set_name(_('MAPZ Compressed Map (*.mapz)'))
		filtr.add_pattern('*.mapz')
		chooser.add_filter(filtr)
			
		response = chooser.run()
		if response == Gtk.ResponseType.OK:
			filename = chooser.get_filename()
			tf = tarfile.open(filename)
			mapname = utils.get_save_dir() + tf.getnames()[0]
			tf.extractall(utils.get_save_dir())
			tf.close()
			map = MapList.new_from_file(mapname)
			map.filename = mapname
			
		chooser.destroy()
Пример #11
0
    def import_clicked(self, button, other=None, *data):
        chooser = Gtk.FileChooserDialog(title=_("Open File"), action=Gtk.FileChooserAction.OPEN, \
                                        buttons=(Gtk.STOCK_CANCEL, Gtk.ResponeType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK))

        filtr = Gtk.FileFilter()
        filtr.set_name(_('MAPZ Compressed Map (*.mapz)'))
        filtr.add_pattern('*.mapz')
        chooser.add_filter(filtr)

        response = chooser.run()
        if response == Gtk.ResponseType.OK:
            filename = chooser.get_filename()
            tf = tarfile.open(filename)
            mapname = utils.get_save_dir() + tf.getnames()[0]
            tf.extractall(utils.get_save_dir())
            tf.close()
            map = MapList.new_from_file(mapname)
            map.filename = mapname

        chooser.destroy()
Пример #12
0
class List(object):

    DB = utils.get_save_dir() + 'hosts.db'

    def __init__(self):
        self._hosts = anydbm.open(List.DB, 'c')

    def __del__(self):
        self._hosts.close()

    def get(self, index):
        return pickle.loads(self._hosts[index])

    def add(self, host):
        assert isinstance(host, Host)

        if host.name == None:
            raise ExceptHostNameInvalid("host.name is None")

        if host.name in self._hosts:
            raise ExceptHostNameExist("host.name exist")

        self.save(host)

    def save(self, host):
        assert isinstance(host, Host)
        self._hosts[host.name] = pickle.dumps(host)

    def remove(self, host):
        if host != None:
            del self._hosts[host.name]

    def iteritems(self):
        items = []
        type(self._hosts.keys())
        for k in self._hosts.keys():
            items.append(self.get(k))

        return items

    def create_host(self):
        i = ""
        name = _("Unnamed").encode()
        while name + ((i != "" or "") and "-" + str(i)) in self._hosts:
            if i == "":
                i = 1
            else:
                i += 1

        if i != "":
            name += "-" + str(i)

        return Host(name, Type.create_type(Type.MYSQL_TCP_IP))
Пример #13
0
    def doc_save_cb (self, widget, doc, top_element):
        save_string = self.serialize_to_xml(doc, top_element)
        if not self.save_file:
            hsh = hashlib.sha256 (save_string)
            save_loc = utils.get_save_dir ()
            self.save_file = save_loc+hsh.hexdigest()+".map"
            counter = 1
            while os.path.exists(self.save_file):

                print "Warning: Duplicate File.  Saving to alternative"
                self.save_file = save_loc + "Dup"+str(counter)+hsh.hexdigest()+".map"
                counter += 1

        self.save_map(self.save_file, save_string)
        self.emit ('file_saved', self.save_file, self)
Пример #14
0
    def doc_save_cb (self, widget, doc, top_element):
        save_string = self.serialize_to_xml(doc, top_element)
        if not self.save_file:
            sham = sha.new (save_string)
            save_loc = utils.get_save_dir ()
            self.save_file = save_loc+sham.hexdigest()+".map"
            counter = 1
            while os.path.exists(self.save_file):
            
                print "Warning: Duplicate File.  Saving to alternative"
                self.save_file = save_loc + "Dup"+str(counter)+sham.hexdigest()+".map"
                counter += 1

        self.save_map(self.save_file, save_string)
        self.emit ('file_saved', self.save_file, self)
Пример #15
0
    def doc_save_cb(self, widget, doc, top_element):
        save_string = self.serialize_to_xml(doc, top_element)
        if not self.save_file:
            hsh = hashlib.sha256(save_string)
            save_loc = utils.get_save_dir()
            self.save_file = os.path.join(save_loc, hsh.hexdigest() + ".map")
            counter = 1
            while os.path.exists(self.save_file):
                print "Warning: Duplicate File. Saving to alternative"
                alt_name = "Dup" + str(counter) + hsh.hexdigest() + ".map"
                self.save_file = save_loc + os.path.join(save_loc, alt_name)
                counter += 1

        with open(self.save_file, 'w') as f:
            f.write(save_string)
        self.emit('file_saved', self.save_file, self)
Пример #16
0
    def doc_save_cb (self, widget, doc, top_element):
        save_string = self.serialize_to_xml(doc, top_element)
        if not self.save_file:
            hsh = hashlib.sha256 (save_string)
            save_loc = utils.get_save_dir ()
            self.save_file = os.path.join (save_loc, hsh.hexdigest() + ".map")
            counter = 1
            while os.path.exists(self.save_file):
                print "Warning: Duplicate File. Saving to alternative"
                alt_name = "Dup" + str(counter) + hsh.hexdigest() + ".map"
                self.save_file = save_loc + os.path.join (save_loc, alt_name)
                counter += 1

        with open(self.save_file, 'w') as f:
            f.write(save_string)
        self.emit ('file_saved', self.save_file, self)
Пример #17
0
    def __init__(self, arg, torch_device):
        self.torch_device = torch_device

        self.model_type = arg.model

        self.z_idx = 0 if arg.in_channel == 1 else arg.in_channel // 2  # middle of 2.5d slices

        self.epoch = arg.epoch
        self.start_epoch = 0

        self.batch_size = arg.batch_size

        self.save_path = utils.get_save_dir(arg)
        self.log_file_path = self.save_path + "/log.txt"

        self.logger = Logger(arg, self.save_path)
Пример #18
0
    def __init__(self, arg, torch_device):
        self.torch_device = torch_device 
        
        self.model_type = arg.model

        self.z_idx = 0

        self.epoch = arg.epoch
        self.start_epoch = 0

        self.batch_size = arg.batch_size
        
        self.save_path = utils.get_save_dir(arg)
        self.log_file_path = self.save_path+"/log.txt"

        self.logger = Logger(arg, self.save_path)
Пример #19
0
def _iter():
    for problem in ["cartpole"]:  # "bouncing_ball", "stopping_car", "cartpole"
        for method in ["standard"]:  # , "ora"
            if problem == "cartpole":
                for tau in [0.001
                            ]:  # "tau": tune.grid_search([0.001, 0.02, 0.005]
                    for template in [1]:  # [1,0,2]
                        for nn_index in range(1, min(100,
                                                     len(folder_cartpole))):
                            for checkpoint, check_folder in find_checkpoints(
                                    os.path.join(utils.get_save_dir(),
                                                 folder_cartpole[nn_index])):
                                yield problem, method, {
                                    "tau": tau,
                                    "template": template,
                                    "agent": nn_index,
                                    "checkpoint": checkpoint,
                                    "folder": check_folder
                                }
Пример #20
0
 def __init__(self):
     env_input_size: int = 2
     super().__init__(env_input_size)
     self.post_fn_remote = self.post_milp
     self.get_nn_fn = self.get_nn
     self.plot_fn = self.plot
     self.template_2d: np.ndarray = np.array([[0, 1], [1, 0]])
     input_boundaries, input_template = self.get_template(0)
     self.input_boundaries: List = input_boundaries
     self.input_template: np.ndarray = input_template
     _, template = self.get_template(0)
     self.analysis_template: np.ndarray = template
     self.time_horizon = 500
     self.rounding_value = 2**8
     p = Experiment.e(self.env_input_size, 0)
     v = Experiment.e(self.env_input_size, 1)
     self.unsafe_zone: List[Tuple] = [([p, -v, v], np.array([0, 1, 0]))]
     self.nn_path = os.path.join(
         utils.get_save_dir(),
         "tune_PPO_bouncing_ball/PPO_BouncingBall_c7326_00000_0_2021-01-16_05-43-36/checkpoint_36/checkpoint-36"
     )
Пример #21
0
    def __init__(self, arg, torch_device):
        self.torch_device = torch_device

        self.model_type = arg.model

        self.z_idx = 0

        self.epoch = arg.epoch
        self.start_epoch = 0

        self.batch_size = arg.batch_size

        self.save_path = utils.get_save_dir(arg)
        self.log_file_path = self.save_path + "/fold%s/log.txt" % (arg.fold)

        if os.path.exists(self.save_path) is False:
            os.mkdir(self.save_path)

        if os.path.exists(self.save_path + "/fold%s" % (arg.fold)) is False:
            os.mkdir(self.save_path + "/fold%s" % (arg.fold))

        self.logger = Logger(arg, self.save_path + "/fold%s" % (arg.fold))
Пример #22
0
    def _download_module(self, module_url):
        request = self.request
        session = request.session
        conn = sword2cnx.Connection(session['login'].service_document_url,
                                    user_name=session['login'].username,
                                    user_pass=session['login'].password,
                                    always_authenticate=True,
                                    download_service_document=False)

        parts = urlparse.urlsplit(module_url)
        path = parts.path.split('/')
        path = path[:path.index('sword')]
        module_url = '%s://%s%s' % (parts.scheme, parts.netloc, '/'.join(path))

        # example: http://cnx.org/Members/user001/m17222/sword/editmedia
        zip_file = conn.get_cnx_module(module_url=module_url, packaging='zip')

        save_dir = get_save_dir(request)
        if save_dir is None:
            request.session['upload_dir'], save_dir = create_save_dir(request)
        extract_to_save_dir(zip_file, save_dir)

        cnxml_file = open(os.path.join(save_dir, 'index.cnxml'), 'rb')
        cnxml = cnxml_file.read()
        cnxml_file.close()
        conversionerror = None
        try:
            structuredhtml = cnxml_to_structuredhtml(cnxml)
            save_and_backup_file(save_dir, 'index.structured.html',
                                 structuredhtml)
            htmlpreview = structuredhtml_to_htmlpreview(structuredhtml)
            save_and_backup_file(save_dir, 'index.html', htmlpreview)
            files = get_files(save_dir)
            save_zip(save_dir, cnxml, structuredhtml, files)
        except libxml2.parserError:
            conversionerror = traceback.format_exc()
            raise ConversionError(conversionerror)
Пример #23
0
                    action="store_true",
                    default=False,
                    help="don't use instructions in the model")
parser.add_argument("--no-mem",
                    action="store_true",
                    default=False,
                    help="don't use memory in the model")
args = parser.parse_args()

# Define run dir

suffix = datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S")
default_model_name = "{}_{}_seed{}_{}".format(args.env, args.algo, args.seed,
                                              suffix)
model_name = args.model or default_model_name
save_dir = utils.get_save_dir(model_name)

# Define logger, CSV writer and Tensorboard writer

logger = utils.get_logger(save_dir)
csv_writer = utils.get_csv_writer(save_dir)
if args.tb:
    from tensorboardX import SummaryWriter
    tb_writer = SummaryWriter(save_dir)

# Log command and all script arguments

logger.info("{}\n".format(" ".join(sys.argv)))
logger.info("{}\n".format(args))

# Set seed for all randomness sources
Пример #24
0
# env.state = np.array([-0.57,0.21]) #guaranteed fail
# env.state = np.array([-0.57,0.21]) #guaranteed fail
# env.state = np.array([-0.22,0.05]) #success right
# env.state = np.array([0.25, -1])  # success left
# env.state = np.array([0.39,0.9]) # fail left
# env.state = np.array([0.834,0.497]) # fail
# start_state = np.array([-0.12, 0.33]) #100% success
# start_state = np.array([0.35,-0.83]) # should be 83% but appears 100%
# start_state = np.array([-0.48, -0.44])  #100% fail
start_state = np.array([0.37, 0.43])  # min 33% max 100%

state_size = 2
agent = Agent(state_size, 2)
agent.load(
    os.path.join(
        utils.get_save_dir(),
        "Pendulum_Apr07_12-17-45_alpha=0.6, min_eps=0.01, eps_decay=0.2/checkpoint_final.pth"
    ))
action = None

render = False
n_trials = 10000
n_fail = 0
widgets = [
    progressbar.Percentage(),
    progressbar.Bar(),
    progressbar.Variable('fails'),
    ', ',
    progressbar.Variable('trials'),
]
with progressbar.ProgressBar(max_value=n_trials, widgets=widgets) as bar:
Пример #25
0
    return options


if __name__ == '__main__':

    # Book-keeping & paths
    args = get_args()

    dir_img = 'data/train/'
    dir_mask = 'data/train_masks/'
    dir_checkpoint = 'save/'
    splitfile = "data/trainval.json"

    runname = args.name
    save_path = os.path.join(dir_checkpoint, runname)
    save_dir = get_save_dir(save_path, runname,
                            training=False)  # unique save dir
    log = get_logger(save_dir, runname)  # logger
    log.info('Args: {}'.format(
        json.dumps(
            {
                "batch_size": args.batch_size,
                "taylor_batches": args.taylor_batches,
                "prune_channels": args.prune_channels,
                "gpu": args.gpu,
                "load": args.load,
                "channel_txt": args.channel_txt,
                "scale": args.scale,
                "lr": args.lr,
                "iters": args.iters,
                "epochs": args.epochs
            },
Пример #26
0
def main(args):

    #denoiser = VQ_CVAE(128, k=512, num_channels=3)
    #denoiser.load_state_dict(torch.load("/mnt/home2/dlongo/eegML/VQ-VAE-master/vq_vae/saved_models/train.pt"))
    #denoiser = torch.no_grad(denoiser)
    #denoiser.cuda()
    #denoiser = nn.DataParallel(denoiser)
    # Get device
    device, args.gpu_ids = utils.get_available_devices()
    args.train_batch_size *= max(1, len(args.gpu_ids))
    args.test_batch_size *= max(1, len(args.gpu_ids))

    # Set random seed
    utils.seed_torch(seed=SEED)

    # Get save directories
    train_save_dir = utils.get_save_dir(args.save_dir, training=True)
    args.train_save_dir = train_save_dir

    # Save args
    args_file = os.path.join(train_save_dir, ARGS_FILE_NAME)
    with open(args_file, 'w') as f:
        json.dump(vars(args), f, indent=4, sort_keys=True)

    # Set up logging and devices
    log = utils.get_logger(train_save_dir, 'train_denoised')
    tbx = SummaryWriter(train_save_dir)
    log.info('Args: {}'.format(dumps(vars(args), indent=4, sort_keys=True)))

    if args.cross_val:
        # Loop over folds
        for fold_idx in range(args.num_folds):
            log.info('Starting fold {}...'.format(fold_idx))

            # Train
            fold_save_dir = os.path.join(train_save_dir,
                                         'fold_' + str(fold_idx))
            if not os.path.exists(fold_save_dir):
                os.makedirs(fold_save_dir)

            # Training on current fold...
            train_fold(args,
                       device,
                       fold_save_dir,
                       log,
                       tbx,
                       cross_val=True,
                       fold_idx=fold_idx)
            best_path = os.path.join(fold_save_dir, 'best.pth.tar')

            # Predict on current fold with best model..
            if args.model_name == 'SeizureNet':
                model = SeizureNet(args)

            model = nn.DataParallel(model, args.gpu_ids)
            model, _ = utils.load_model(model, best_path, args.gpu_ids)

            model.to(device)
            results = evaluate_fold(model,
                                    args,
                                    fold_save_dir,
                                    device,
                                    cross_val=True,
                                    fold_idx=fold_idx,
                                    is_test=True,
                                    write_outputs=True)

            # Log to console
            results_str = ', '.join('{}: {:05.2f}'.format(k, v)
                                    for k, v in results.items())
            print('Fold {} test results: {}'.format(fold_idx, results_str))
            log.info('Finished fold {}...'.format(fold_idx))
    else:
        # no cross-validation
        # Train
        train_fold(args, device, train_save_dir, log, tbx, cross_val=False)
        best_path = os.path.join(train_save_dir, 'best.pth.tar')

        if args.model_name == 'SeizureNet':
            model = SeizureNet(args)

        model = nn.DataParallel(model, args.gpu_ids)
        model, _ = utils.load_model(model, best_path, args.gpu_ids)

        model.to(device)
        results = evaluate_fold(model,
                                args,
                                train_save_dir,
                                device,
                                cross_val=False,
                                fold_idx=None,
                                is_test=True,
                                write_outputs=True)

        # Log to console
        results_str = ', '.join('{}: {:05.2f}'.format(k, v)
                                for k, v in results.items())
        print('Test set prediction results: {}'.format(results_str))
Пример #27
0
def main(args):

    # set up logging and device
    args.save_dir = utils.get_save_dir(args.save_dir, args.name, training=True)
    logger = utils.get_logger(args.save_dir, args.name)
    tbx = SummaryWriter(args.save_dir)

    if args.local_rank == -1 or args.no_cuda:
        device = torch.device("cuda" if torch.cuda.is_available()
                              and not args.no_cuda else "cpu")
        n_gpu = torch.cuda.device_count()
    else:
        torch.cuda.set_device(args.local_rank)
        device = torch.device("cuda", args.local_rank)
        n_gpu = 1
        # Initializes the distributed backend which will take care of sychronizing nodes/GPUs
        torch.distributed.init_process_group(backend='nccl')
    logger.info(
        "device: {} n_gpu: {}, distributed training: {}, 16-bits training: {}".
        format(device, n_gpu, bool(args.local_rank != -1), args.fp16))

    if args.gradient_accumulation_steps < 1:
        raise ValueError(
            "Invalid gradient_accumulation_steps parameter: {}, should be >= 1"
            .format(args.gradient_accumulation_steps))

    args.train_batch_size = args.train_batch_size // args.gradient_accumulation_steps

    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    if n_gpu > 0:
        torch.cuda.manual_seed_all(args.seed)

    if not args.do_train and not args.do_predict:
        raise ValueError(
            "At least one of `do_train` or `do_predict` must be True.")

    if args.do_train:
        if not args.train_file:
            raise ValueError(
                "If `do_train` is True, then `train_file` must be specified.")
    if args.do_predict:
        if not args.predict_file:
            raise ValueError(
                "If `do_predict` is True, then `predict_file` must be specified."
            )

    if os.path.exists(args.output_dir) and os.listdir(
            args.output_dir) and args.do_train:
        raise ValueError(
            "Output directory () already exists and is not empty.")
    os.makedirs(args.output_dir, exist_ok=True)

    tokenizer = BertTokenizer.from_pretrained(args.bert_model,
                                              do_lower_case=args.do_lower_case)

    # Generating the dictionaries
    dep_dict, pos_dict, ent_dict, total_features = generate_dictionary(
        args.train_ling_features_file, args.eval_ling_features_file,
        args.test_ling_features_file)
    #    from IPython import embed; embed()
    # Generating total_dictionary
    total_dict = convert_string_features_to_array(total_features, dep_dict,
                                                  pos_dict, ent_dict)

    #    from IPython import embed; embed()
    train_examples = None
    num_train_optimization_steps = None
    if args.do_train:
        train_examples = read_squad_examples(
            input_file=args.train_file,
            is_training=True,
            version_2_with_negative=args.version_2_with_negative,
            total_dictionary=total_dict)
        num_train_optimization_steps = int(
            len(train_examples) / args.train_batch_size /
            args.gradient_accumulation_steps) * args.num_train_epochs
        if args.local_rank != -1:
            num_train_optimization_steps = num_train_optimization_steps // torch.distributed.get_world_size(
            )

    # Prepare model
    model = BertForQuestionAnsweringLing.from_pretrained(
        args.bert_model,
        cache_dir=PYTORCH_PRETRAINED_BERT_CACHE /
        'distributed_{}'.format(args.local_rank))

    if args.fp16:
        model.half()
    model.to(device)
    if args.local_rank != -1:
        try:
            from apex.parallel import DistributedDataParallel as DDP
        except ImportError:
            raise ImportError(
                "Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training."
            )

        model = DDP(model)
    elif n_gpu > 1:
        model = torch.nn.DataParallel(model)

    # Prepare optimizer
    param_optimizer = list(model.named_parameters())

    # hack to remove pooler, which is not used
    # thus it produce None grad that break apex
    param_optimizer = [n for n in param_optimizer if 'pooler' not in n[0]]

    no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
    optimizer_grouped_parameters = [{
        'params':
        [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)],
        'weight_decay':
        0.01
    }, {
        'params':
        [p for n, p in param_optimizer if any(nd in n for nd in no_decay)],
        'weight_decay':
        0.0
    }]

    if args.fp16:
        try:
            from apex.optimizer import FP16_Optimizer
            from apex.optimizers import FusedAdam
        except ImportError:
            raise ImportError(
                "Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training."
            )

        optimizer = FusedAdam(optimizer_grouped_parameters,
                              lr=args.learning_rate,
                              bias_correction=False,
                              max_grad_norm=1.0)
        if args.loss_scale == 0:
            optimizer = FP16_Optimizer(optimizer, dynamic_loss_scale=True)
        else:
            optimizer = FP16_Optimizer(optimizer,
                                       static_loss_scale=args.loss_scale)
    else:
        optimizer = BertAdam(optimizer_grouped_parameters,
                             lr=args.learning_rate,
                             warmup=args.warmup_proportion,
                             t_total=num_train_optimization_steps)

    global_step = 0

    # load training features
    cached_train_features_file = args.train_file + '_{0}_{1}_{2}_{3}'.format(
        list(filter(None, args.bert_model.split('/'))).pop(),
        str(args.max_seq_length), str(args.doc_stride),
        str(args.max_query_length))
    train_features = None
    print(cached_train_features_file)
    try:
        with open(cached_train_features_file, "rb") as reader:
            train_features = pickle.load(reader)
    except:
        train_features = convert_examples_to_features(
            examples=train_examples,
            tokenizer=tokenizer,
            max_seq_length=args.max_seq_length,
            doc_stride=args.doc_stride,
            max_query_length=args.max_query_length,
            is_training=True)
        if args.local_rank == -1 or torch.distributed.get_rank() == 0:
            logger.info("  Saving train features into cached file %s",
                        cached_train_features_file)
            with open(cached_train_features_file, "wb") as writer:
                pickle.dump(train_features, writer)

    # load eval features
    eval_examples = read_squad_examples(
        input_file=args.predict_file,
        is_training=False,
        version_2_with_negative=args.version_2_with_negative,
        total_dictionary=total_dict)
    eval_features = convert_examples_to_features(
        examples=eval_examples,
        tokenizer=tokenizer,
        max_seq_length=args.max_seq_length,
        doc_stride=args.doc_stride,
        max_query_length=args.max_query_length,
        is_training=False)

    test_examples = read_squad_examples(
        input_file=args.test_file,
        is_training=False,
        version_2_with_negative=args.version_2_with_negative,
        total_dictionary=total_dict)
    test_features = convert_examples_to_features(
        examples=test_examples,
        tokenizer=tokenizer,
        max_seq_length=args.max_seq_length,
        doc_stride=args.doc_stride,
        max_query_length=args.max_query_length,
        is_training=False)

    if args.do_train:
        logger.info("***** Running training *****")
        logger.info("  Num orig examples = %d", len(train_examples))
        logger.info("  Num split examples = %d", len(train_features))
        logger.info("  Batch size = %d", args.train_batch_size)
        logger.info("  Num steps = %d", num_train_optimization_steps)
        all_input_ids = torch.tensor([f.input_ids for f in train_features],
                                     dtype=torch.long)
        all_input_mask = torch.tensor([f.input_mask for f in train_features],
                                      dtype=torch.long)
        all_segment_ids = torch.tensor([f.segment_ids for f in train_features],
                                       dtype=torch.long)
        all_start_positions = torch.tensor(
            [f.start_position for f in train_features], dtype=torch.long)
        all_end_positions = torch.tensor(
            [f.end_position for f in train_features], dtype=torch.long)
        # from IPython import embed; embed()
        all_ling_features = torch.tensor(
            [f.ling_features for f in train_features], dtype=torch.float)
        train_data = TensorDataset(all_input_ids, all_input_mask,
                                   all_segment_ids, all_ling_features,
                                   all_start_positions, all_end_positions)
        steps_till_eval = args.eval_steps
        if args.local_rank == -1:
            train_sampler = RandomSampler(train_data)
        else:
            train_sampler = DistributedSampler(train_data)
        train_dataloader = DataLoader(train_data,
                                      sampler=train_sampler,
                                      batch_size=args.train_batch_size)

        model.train()

        best_F1 = 0
        for _ in trange(int(args.num_train_epochs), desc="Epoch"):
            for step, batch in enumerate(
                    tqdm(train_dataloader, desc="Iteration")):
                if n_gpu == 1:
                    batch = tuple(
                        t.to(device)
                        for t in batch)  # multi-gpu does scattering it-self
                input_ids, input_mask, segment_ids, ling_features, start_positions, end_positions = batch
                # from IPython import embed; embed()
                loss = model(input_ids, segment_ids, input_mask, ling_features,
                             start_positions, end_positions)
                if n_gpu > 1:
                    loss = loss.mean()  # mean() to average on multi-gpu.
                if args.gradient_accumulation_steps > 1:
                    loss = loss / args.gradient_accumulation_steps

                if args.fp16:
                    optimizer.backward(loss)
                else:
                    loss.backward()
                if (step + 1) % args.gradient_accumulation_steps == 0:
                    if args.fp16:
                        # modify learning rate with special warm up BERT uses
                        # if args.fp16 is False, BertAdam is used and handles this automatically
                        lr_this_step = args.learning_rate * warmup_linear(
                            global_step / num_train_optimization_steps,
                            args.warmup_proportion)
                        for param_group in optimizer.param_groups:
                            param_group['lr'] = lr_this_step
                    optimizer.step()
                    optimizer.zero_grad()
                    global_step += 1
                # add to tensorboard
                loss_val = loss.item()
                tbx.add_scalar('train/NLL', loss_val, global_step)
                tbx.add_scalar('train/LR', optimizer.param_groups[0]['lr'],
                               global_step)

                steps_till_eval -= args.train_batch_size
                if steps_till_eval <= 0:
                    steps_till_eval = args.eval_steps

                    # Evaluate and save checkpoint
                    logger.info('Evaluating at step {}...'.format(step))
                    # ema.assign(model)
                    results, _ = evaluate(model, eval_examples, eval_features,
                                          device, args, logger,
                                          args.version_2_with_negative,
                                          args.dev_eval_file)
                    # saver.save(step, model, results[args.metric_name], device)
                    # ema.resume(model)

                    # Log to console
                    results_str = ', '.join('{}: {:05.2f}'.format(k, v)
                                            for k, v in results.items())
                    logger.info('Dev {}'.format(results_str))

                    # Log to TensorBoard
                    logger.info('Visualizing in TensorBoard...')
                    for k, v in results.items():
                        tbx.add_scalar('dev/{}'.format(k), v, global_step)
                    """
                    util.visualize(tbx,
                                   pred_dict=pred_dict,
                                   eval_path=args.dev_eval_file,
                                   step=step,
                                   split='dev',
                                   num_visuals=args.num_visuals)
                    """
                    if results['F1'] > best_F1:
                        best_F1 = results['F1']
                        model_to_save = model.module if hasattr(
                            model,
                            'module') else model  # Only save the model it-self
                        output_model_file = os.path.join(
                            args.output_dir, "pytorch_model_best.bin")
                        torch.save(model_to_save.state_dict(),
                                   output_model_file)
                        #model.to(device)

    # Save a trained model
    """
    model_to_save = model.module if hasattr(model, 'module') else model  # Only save the model it-self
    output_model_file = os.path.join(args.output_dir, "pytorch_model.bin")
    if args.do_train:
        torch.save(model_to_save.state_dict(), output_model_file)
        # Load a trained model that you have fine-tuned
        model_state_dict = torch.load(output_model_file)
        model = BertForQuestionAnsweringLing.from_pretrained(args.bert_model, state_dict=model_state_dict)
    else:
        model = BertForQuestionAnsweringLing.from_pretrained(args.bert_model)

    model.to(device)
    """

    # load the best trained model and eval on the eval set and test set
    best_model_file = os.path.join(args.output_dir, "pytorch_model_best.bin")
    model_state_dict = torch.load(best_model_file)
    model = BertForQuestionAnsweringLing.from_pretrained(
        args.bert_model, state_dict=model_state_dict)
    model.to(device)

    if args.do_predict and (args.local_rank == -1
                            or torch.distributed.get_rank() == 0):
        logger.info('Evaluating at the best model')
        results, all_results = evaluate(model, eval_examples, eval_features,
                                        device, args, logger,
                                        args.version_2_with_negative,
                                        args.dev_eval_file)

        logger.info('Write the best eval results')
        output_prediction_file = os.path.join(args.output_dir,
                                              "predictions.json")
        output_nbest_file = os.path.join(args.output_dir,
                                         "nbest_predictions.json")
        output_null_log_odds_file = os.path.join(args.output_dir,
                                                 "null_odds.json")
        write_predictions(eval_examples, eval_features, all_results,
                          args.n_best_size, args.max_answer_length,
                          args.do_lower_case, output_prediction_file,
                          output_nbest_file, output_null_log_odds_file,
                          args.verbose_logging, args.version_2_with_negative,
                          args.null_score_diff_threshold, 'dev')

        logger.info('Test set at the best model')
        results, all_results = evaluate(model, test_examples, test_features,
                                        device, args, logger,
                                        args.version_2_with_negative,
                                        args.test_eval_file)

        logger.info('Write the best test set results')
        output_prediction_file = os.path.join(args.output_dir,
                                              "predictions_test.json")
        output_nbest_file = os.path.join(args.output_dir,
                                         "nbest_predictions_test.json")
        output_null_log_odds_file = os.path.join(args.output_dir,
                                                 "null_odds_test.json")
        write_predictions(test_examples, test_features, all_results,
                          args.n_best_size, args.max_answer_length,
                          args.do_lower_case, output_prediction_file,
                          output_nbest_file, output_null_log_odds_file,
                          args.verbose_logging, args.version_2_with_negative,
                          args.null_score_diff_threshold, 'test')
        """
Пример #28
0
def main(args):
    # Set up logging and devices
    args.save_dir = utils.get_save_dir(args.save_dir, args.name, training=True)
    log = utils.get_logger(args.save_dir, args.name)
    tbx = SummaryWriter(args.save_dir)
    device, args.gpu_ids = utils.get_available_devices()
    log.info(f'Args: {dumps(vars(args), indent=4, sort_keys=True)}')
    args.batch_size *= max(1, len(args.gpu_ids))

    # Set random seed
    log.info(f'Using random seed {args.seed}...')
    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)

    # Get model
    log.info('Building model...')
    model = get_model(args)
    model = nn.DataParallel(model, args.gpu_ids)
    if args.load_path:
        log.info(f'Loading checkpoint from {args.load_path}...')
        model, step = utils.load_model(model, args.load_path, args.gpu_ids)
    else:
        step = 0
    model = model.to(device)
    model.train()

    # Get saver
    saver = utils.CheckpointSaver(args.save_dir,
                                  max_checkpoints=args.max_checkpoints,
                                  metric_name=args.metric_name,
                                  maximize_metric=args.maximize_metric,
                                  log=log)

    # Set optimizer and scheduler
    optimizer_grouped_params = [{
        'params':
        [p for n, p in model.named_parameters() if 'classifier' not in n]
    }, {
        'params':
        [p for n, p in model.named_parameters() if 'classifier' in n],
        'lr':
        args.lr_1
    }]
    optimizer = optim.AdamW(optimizer_grouped_params,
                            args.lr_2,
                            weight_decay=args.l2_wd)
    scheduler = sched.LambdaLR(optimizer, lambda s: 1.)  # Constant LR

    # Get data loader
    log.info('Building dataset...')
    transform = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])
    train_dataset = NEWS(args.train_record_file, transform=transform)
    train_loader = data.DataLoader(train_dataset,
                                   batch_size=args.batch_size,
                                   shuffle=True,
                                   num_workers=args.num_workers,
                                   collate_fn=collate_fn)
    dev_dataset = NEWS(args.dev_record_file, transform=transform)
    dev_loader = data.DataLoader(dev_dataset,
                                 batch_size=args.batch_size,
                                 shuffle=False,
                                 num_workers=args.num_workers,
                                 collate_fn=collate_fn)

    # Train
    log.info('Training...')
    steps_till_eval = args.eval_steps
    epoch = step // len(train_dataset)
    while epoch != args.num_epochs:
        epoch += 1
        log.info(f'Starting epoch {epoch}...')
        with torch.enable_grad(), tqdm(
                total=len(train_loader.dataset)) as progress_bar:
            # for input_idxs, atten_masks, images, ids, y in train_loader:
            for input_idxs, atten_masks, y in train_loader:
                # print(y)
                # Setup for forward
                input_idxs = input_idxs.to(device)
                atten_masks = atten_masks.to(device)
                y = y.to(device)
                batch_size = input_idxs.size(0)
                optimizer.zero_grad()

                # Forward
                log_p = model(input_idxs, atten_masks)
                loss = torch.nn.functional.binary_cross_entropy(
                    log_p,
                    y,
                    weight=None,
                    size_average=None,
                    reduce=None,
                    reduction='mean')
                loss_val = loss.item()

                # Backward
                loss.backward()
                optimizer.step()
                scheduler.step(step // batch_size)

                # Log info
                step += batch_size
                progress_bar.update(batch_size)
                progress_bar.set_postfix(epoch=epoch, NLL=loss_val)
                tbx.add_scalar('train/NLL', loss_val, step)
                tbx.add_scalar('train/LR', optimizer.param_groups[0]['lr'],
                               step)

                steps_till_eval -= batch_size
                if steps_till_eval <= 0:
                    steps_till_eval = args.eval_steps

                    # Evaluate and save checkpoint
                    log.info(f'Evaluating at step {step}...')
                    results, pred_dict = evaluate(model, dev_loader, device)
                    saver.save(step, model, results[args.metric_name], device)

                    # Log to console
                    results_str = ', '.join(f'{k}: {v:05.2f}'
                                            for k, v in results.items())
                    log.info(f'Dev {results_str}')

                    # Log to TensorBoard
                    log.info('Visualizing in TensorBoard...')
                    for k, v in results.items():
                        tbx.add_scalar(f'dev/{k}', v, step)
Пример #29
0
                        help='Path to pre-trained model')
    parser.add_argument(
        '--freeze_layers',
        type=int,
        default=0,
        help=
        'Number of initial layers to freeze when fine-tuning | Choose from 1, 2, 3'
    )

    args = parser.parse_args()

    logs_dir = 'logs'
    if not os.path.exists(logs_dir):
        os.makedirs(logs_dir)

    args.save_dir = utils.get_save_dir(logs_dir, args.name)

    # Training
    if not torch.cuda.is_available() and args.cuda:
        print(
            '--cuda is passed but torch.cuda.is_available() returned False. Will use CPU instead.'
        )

    env = utils.wrap_deepmind(utils.make_atari(
        args.env,
        max_episode_steps=args.episode_length,
        frameskip=args.frameskip),
                              frame_stack=True,
                              stacks=args.agent_history_length)
    agent = Agent(env, args)
Пример #30
0
            if not args.do_not_eval_after_epoch:
                eval_results = evaluator.evaluate(model, global_step)
                if args.local_rank in [-1, 0]:
                    saver.save(model, global_step, eval_results)


if __name__ == '__main__':
    args = get_args()

    if args.local_rank != -1:
        torch.cuda.set_device(args.local_rank)
        torch.distributed.init_process_group(backend='nccl')

    if args.local_rank in [-1, 0]:
        args.save_dir = get_save_dir(args.save_dir, args.name)
        logger = get_logger(args.save_dir, args.name, log_file=f'log_0.txt')
        logger.info(f'Results will be saved to {args.save_dir}.')
        tb_writer = SummaryWriter(args.save_dir)
    else:
        torch.distributed.barrier()
        args.save_dir = get_save_dir(args.save_dir, args.name, use_existing_dir=True)
        logger = get_logger(args.save_dir, args.name, verbose=False, log_file=f'log_{args.local_rank}.txt')
        tb_writer = None

    if args.local_rank == 0:
        torch.distributed.barrier()

    try:
        train(args, logger, tb_writer)
    except:
Пример #31
0
              },
              "env_config": {"cost_fn": tune.grid_search([0, 1, 2]),
                             "tau": tune.grid_search([0.001, 0.02, 0.005])}
              }
    return config


if __name__ == "__main__":
    seed = 1234
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    ray.init(local_mode=False, include_dashboard=True, log_to_driver=False)
    config = get_PPO_config(use_gpu=0.5, seed=seed)
    datetime_str = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    tune.run(
        "PPO",
        stop={"info/num_steps_trained": 2e7, "episode_reward_mean": 7950},
        config=config,
        name=f"tune_PPO_cartpole",
        checkpoint_freq=10,
        checkpoint_at_end=True,
        log_to_file=True,
        local_dir=utils.get_save_dir(),
        callbacks=[MyCallback()],
        # resume="PROMPT",
        verbose=1,
    )
    ray.shutdown()
    # to see the progress run
    # tensorboard --logdir=./save/tune_PPO_cartpole
Пример #32
0
    @classmethod
    def get_iter_by_col_id(cls, col_id):
        found = False
        iter = cls.tree_view_model.get_iter_first()
        while iter:
            (num, ) = cls.tree_view_model.get(iter, MapList.COL_ID)
            if col_id == num:
                found = True
                break
            iter = cls.tree_view_model.iter_next(iter)

        if not found:
            iter = None

        return iter

    @classmethod
    def next_col_id(cls):
        next_col_id = -1
        iter = cls.tree_view_model.get_iter_first()
        while iter:
            (num, ) = cls.tree_view_model.get(iter, MapList.COL_ID)
            if next_col_id < num:
                next_col_id = num
            iter = cls.tree_view_model.iter_next(iter)

        return next_col_id + 1


MapList.load_all_from_dir(utils.get_save_dir())
Пример #33
0
# Set seed for all randomness sources

utils.seed(args.seed)

# Generate environment

envs = []
for i in range(args.procs):
    env = gym.make(args.env)
    env.seed(args.seed + 10000 * i)
    envs.append(env)
env = ParallelEnv(envs)

# Define agent

save_dir = utils.get_save_dir(args.model)
agent = utils.Agent(save_dir, env.observation_space, args.argmax, args.procs)
print("CUDA available: {}\n".format(torch.cuda.is_available()))

# Initialize logs

logs = {"num_frames_per_episode": [], "return_per_episode": []}

# Run the agent

start_time = time.time()

obss = env.reset()

log_done_counter = 0
log_episode_return = torch.zeros(args.procs, device=agent.device)
Пример #34
0
        problem, other_config = config["main_params"]
        add_string = ""
        if problem == "cartpole":
            add_string = f"phi: {other_config.get('phi', 0)} template: {other_config.get('template', 0)} agent:{other_config['nn_path']}"
        if problem == "bouncing_ball":
            add_string = f"phi: {other_config.get('phi', 0)} template: {other_config.get('template', 0)} initial_state: {other_config.get('initial_state', 0)} agent:{other_config['nn_path']}"
        if problem == "stopping_car":
            add_string = f"phi: {other_config.get('phi', 0)} template: {other_config.get('template', 0)} agent:{other_config['nn_path']}"
        return os.path.join(self.name, f"{problem} {add_string}")


if __name__ == '__main__':
    ray.init(local_mode=os.environ.get("local_mode", False), log_to_driver=False, include_dashboard=True)
    cpu = 8
    trials = list(_iter())
    n_trials = len(trials) - 1
    print(f"Total n of trials: {n_trials}")
    start_from = 0
    name_group = NameGroup()
    for i, (problem, other_config) in enumerate(trials):
        if i < start_from:
            continue
        print(f"Starting trial: {i + 1}/{n_trials + 1}")
        experiment_config = {
            "main_params": (problem, other_config),
            "n_workers": cpu
        }
        trial_dir = os.path.join(utils.get_save_dir(), "experiment_collection_NFM", name_group.trial_str_creator(experiment_config))
        run_parameterised_experiment(config=experiment_config, trial_dir=trial_dir)
        print(f"Finished trial: {i + 1}/{n_trials + 1}")
Пример #35
0
	@classmethod
	def get_iter_by_col_id(cls, col_id):
		found = False
		iter = cls.tree_view_model.get_iter_first ()
		while iter:
			(num,) = cls.tree_view_model.get (iter, MapList.COL_ID)
			if col_id == num:
				found = True
				break
			iter = cls.tree_view_model.iter_next (iter)

		if not found:
			iter = None

		return iter

	@classmethod
	def next_col_id(cls):
		next_col_id = -1
		iter = cls.tree_view_model.get_iter_first ()
		while iter:
			(num,) = cls.tree_view_model.get (iter, MapList.COL_ID)
			if next_col_id < num:
				next_col_id = num
			iter = cls.tree_view_model.iter_next (iter)

		return next_col_id + 1

MapList.load_all_from_dir(utils.get_save_dir ())
Пример #36
0
def main(args):
    
    # set up logging and device
    args.output = utils.get_save_dir(args.output, args.name, training=True)
    logger = utils.get_logger(args.output, args.name)
    tbx = SummaryWriter(args.output)

    if args.local_rank == -1 or args.no_cuda:
        device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
        n_gpu = torch.cuda.device_count()
    else:
        torch.cuda.set_device(args.local_rank)
        device = torch.device("cuda", args.local_rank)
        n_gpu = 1
        # Initializes the distributed backend which will take care of sychronizing nodes/GPUs
        torch.distributed.init_process_group(backend='nccl')
    logger.info("device: {} n_gpu: {}, distributed training: {}, 16-bits training: {}".format(
        device, n_gpu, bool(args.local_rank != -1), args.fp16))

    if args.gradient_accumulation_steps < 1:
        raise ValueError("Invalid gradient_accumulation_steps parameter: {}, should be >= 1".format(
                            args.gradient_accumulation_steps))

    args.train_batch_size = args.train_batch_size // args.gradient_accumulation_steps

    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    if n_gpu > 0:
        torch.cuda.manual_seed_all(args.seed)

    if not args.do_train and not args.do_predict:
        raise ValueError("At least one of `do_train` or `do_predict` must be True.")

    if args.do_train:
        if not args.train_file:
            raise ValueError(
                "If `do_train` is True, then `train_file` must be specified.")
    if args.do_predict:
        if not args.predict_file:
            raise ValueError(
                "If `do_predict` is True, then `predict_file` must be specified.")

    if os.path.exists(args.output_dir) and os.listdir(args.output_dir) and args.do_train:
        raise ValueError("Output directory () already exists and is not empty.")
    os.makedirs(args.output_dir, exist_ok=True)

    tokenizer = BertTokenizer.from_pretrained(args.bert_model, do_lower_case=args.do_lower_case)

    # Generating the dictionaries
    dep_dict, pos_dict, ent_dict, total_features  = generate_dictionary(args.train_ling_features_file, args.eval_ling_features_file, args.test_ling_features_file)   
#    from IPython import embed; embed()
    # Generating total_dictionary
    total_dict = convert_string_features_to_array(total_features, dep_dict, pos_dict, ent_dict)

    # Prepare model
    model = BertForQuestionAnsweringLing.from_pretrained(args.bert_model,
                cache_dir=PYTORCH_PRETRAINED_BERT_CACHE / 'distributed_{}'.format(args.local_rank))

    if args.fp16:
        model.half()
    model.to(device)
    if args.local_rank != -1:
        try:
            from apex.parallel import DistributedDataParallel as DDP
        except ImportError:
            raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training.")

        model = DDP(model)
    elif n_gpu > 1:
        model = torch.nn.DataParallel(model)


    global_step = 0

    # load eval features
    eval_examples = read_squad_examples(
            input_file=args.predict_file, is_training=False, version_2_with_negative=args.version_2_with_negative, total_dictionary=total_dict)
    eval_features = convert_examples_to_features(
            examples=eval_examples,
            tokenizer=tokenizer,
            max_seq_length=args.max_seq_length,
            doc_stride=args.doc_stride,
            max_query_length=args.max_query_length,
            is_training=False)

    test_examples = read_squad_examples(
            input_file=args.test_file, is_training=False, version_2_with_negative=args.version_2_with_negative, total_dictionary=total_dict)
    test_features = convert_examples_to_features(
            examples=test_examples,
            tokenizer=tokenizer,
            max_seq_length=args.max_seq_length,
            doc_stride=args.doc_stride,
            max_query_length=args.max_query_length,
            is_training=False)


    # Save a trained model
    """
    model_to_save = model.module if hasattr(model, 'module') else model  # Only save the model it-self
    output_model_file = os.path.join(args.output_dir, "pytorch_model.bin")
    if args.do_train:
        torch.save(model_to_save.state_dict(), output_model_file)
        # Load a trained model that you have fine-tuned
        model_state_dict = torch.load(output_model_file)
        model = BertForQuestionAnsweringLing.from_pretrained(args.bert_model, state_dict=model_state_dict)
    else:
        model = BertForQuestionAnsweringLing.from_pretrained(args.bert_model)

    model.to(device)
    """

    
    # load the best trained model and eval on the eval set and test set
    models = []
    base_path = '/home/zhangyue/results'
    model_names = ['bert_base_linear', 'bert_base_linear_best', 'bert_large_1', 'bert_large_relu_1e-5']


    best_model_file = os.path.join(base_path, model_name[0], "pytorch_model_best.bin")
    model_state_dict = torch.load(best_model_file)
    model = BertForQuestionAnsweringLing.from_pretrained('bert-base-cased', state_dict=model_state_dict)
        model.to(device)
Пример #37
0
env = StoppingCar(config)
# env = CartPoleEnv()  # gym.make("CartPole-v0")
env.seed(seed)
np.random.seed(seed)
state_size = 2
action_size = 2
STARTING_BETA = 0.6  # the higher the more it decreases the influence of high TD transitions
ALPHA = 0.6  # the higher the more aggressive the sampling towards high TD transitions
EPS_DECAY = 0.2
MIN_EPS = 0.01

agent = InvariantAgent(state_size=state_size,
                       action_size=action_size,
                       alpha=ALPHA)
agent.load(os.path.join(
    utils.get_save_dir(),
    "Aug05_16-14-33_alpha=0.6, min_eps=0.01, eps_decay=0.2/checkpoint_3000.pth"
),
           invariant=False)
agent2 = InvariantAgent(state_size=state_size,
                        action_size=action_size,
                        alpha=ALPHA)
# agent2.load("/home/edoardo/Development/SafeDRL/runs/Aug20_09-16-25_invariant/checkpoint_1000.pth")
agent2.load(
    os.path.join(utils.get_save_dir(),
                 "Aug20_11-58-57_invariant/checkpoint_1500.pth"))

agent_model = agent.qnetwork_local
invariant_model = agent2.inetwork_local

agent_model.cpu()