def __init__(self): """initialice the dbs""" self.__engine = create_engine('mysql+mysqldb://{}:{}@{}/{}'.format( os.environ.get("HBNB_MYSQL_USER"), os.environ.get("HBNB_MYSQL_PWD"), os.environ.get("HBNB_MYSQL_HOST"), os.environ.get("HBNB_MYSQL_DB")), pool_pre_ping=True) if os.environ.get("HBNB_ENV") == 'test': Base.drop_all(bind=self.__engine)
def main(): device = torch.device("cuda" if torch.cuda.is_available() else "cpu") batch_size = 64 iterations = 50 test_loader = torch.utils.data.DataLoader( datasets.STL10('../data/STL10', split='train', transform=transforms.Compose([ transforms.Resize((256, 256)), transforms.ToTensor(), # rescale between 0. and 1. ])), batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True) model = Base().to(device) times = [] optimizer = optim.Adam(model.parameters(), lr=1e-2, weight_decay=0.0005) for _ in range(1, iterations + 1): # eval mode model.eval() with torch.no_grad(): data, _ = next(iter(test_loader)) data = data.to(device) start = time.perf_counter() model(data) torch.cuda.synchronize() end = time.perf_counter() # train mode with backprop # model.train() # data, _ = next(iter(test_loader)) # data = data.to(device) # optimizer.zero_grad() # start = time.perf_counter() # output = model(data) # target = torch.randn_like(output) # loss = F.mse_loss(output, target) # loss.backward() # optimizer.step() # torch.cuda.synchronize() # end = time.perf_counter() times.append(end - start) print("Took {}s".format(end - start)) print("Median {}s".format(sorted(times)[len(times) // 2]))
def all(self, cls=None): """Returns a dictionary of objects""" dict_obj = {} if cls: for obj in self.__session.query(eval(cls)).all(): key = "{}.{}".format(obj.__class__.__name__, obj.id) dict_obj[key] = obj else: for subcls in Base.__subclasses__(): for obj in self.__session.query(subcls).all(): key = "{}.{}".format(obj.__class__.__name__, obj.id) dict_obj[key] = obj return dict_obj
def all(self, cls=None): ''' Returns dictionary of all objects in the database ''' allobjs = {} if cls: allobjs = {obj.__class__.__name__ + "." + obj.id: obj for obj in self.__session.query(eval(cls)).all()} else: for tbl in Base.__subclasses__(): table = self.__session.query(tbl).all() for obj in table: allobjs[obj.__class__.__name__] = obj return allobjs
def all(self, cls=None): ''' Returns dictionary of everything in database ''' obj_list = {} if cls: for obj in self.__session.query(cls).all(): key = "{}.{}".format(obj.__class__.__name__, obj.id) obj_list[key] = obj else: for tbl in Base.__subclasses__(): table = self.__session.query(tbl).all() for obj in table: key = "{}.{}".format(obj.__class__.name, obj.id) obj_list[key] = obj return obj_list
def all(self, cls=None): '''returns dictionary representation of database if cls not specified, returns all classes ''' result = {} if cls: for obj in self.__session.query(cls).all(): key = "{}.{}".format(obj.__class__.__name__, obj.id) result[key] = obj else: for sub_c in Base.__subclasses__(): table = self.__session.query(sub_c).all() for obj in table: key = "{}.{}".format(obj.__class__.__name__, obj.id) result[key] = obj return(result)
def all(self, cls=None): """Query on the current database session all objects depending on the class name parameter. If cls=None, query all types of objects. """ found_objects = {} if cls is not None: for instance in self.__session.query(eval(cls)).all(): key = "{}.{}".format(type(instance).__name__, instance.id) found_objects[key] = instance else: tables = Base.__subclasses__() for t in tables: rows = self.__session.query(t).all() for instance in rows: key = "{}.{}".format(type(instance).__name__, instance.id) found_objects[key] = instance return found_objects
def all(self, cls=None): """ query current database based on cls name """ allobjs = {} if cls: cname = cls.__name__ query = self.__session.query(cls) for instance in query: allobjs[cname + '.' + instance.id] = instance else: for subclass in Base.__subclasses__(): query = self.__session.query(subclass) cname = subclass.__name__ for instance in query: allobjs[cname + '.' + instance.id] = instance return allobjs
def all(self, cls=None): """ Returns all instances of a class if the class name is passed. If it's empty, return all instances of valid classes. """ all_dict = {} # classes = ['Places', 'State', 'Review', 'City', 'User', 'Amenity'] if cls: result = self.__session.query(eval(cls)).all() for item in result: key = '{}.{}'.format(item.__class__.__name__, item.id) all_dict[key] = item else: for x in Base.__subclasses__(): result = self.__session.query(x).all() for item in result: key = '{}.{}'.format(item.__class__.__name__, item.id) all_dict[key] = item return all_dict