class StepByStep(): def __init__(self, model, loss_fn, optimizer): self.device = 'cuda' if torch.cuda.is_available() else 'cpu' self.model = model.to(self.device) self.loss_fn = loss_fn self.optimizer = optimizer def to(self, device): # This method allows the user to specify a different device # It sets the corresponding attribute (to be used later in # the mini-batches) and sends the model to the device try: self.device = device self.model.to(self.device) except RuntimeError: self.device = 'cuda' if torch.cuda.is_available() else 'cpu' print(f"Couldn't send it to {device}, sending it to {self.device} instead.") self.model.to(self.device)
class StepByStep(): def __init__(self, model, loss_fn, optimizer): self.device = 'cuda' if torch.cuda.is_available() else 'cpu' self.model = model.to(self.device) self.loss_fn = loss_fn self.optimizer = optimizer # These attributes are defined here, but since they are # not available at the moment of creation, we keep them None self.train_loader = None self.val_loader = None self.writer = None def to(self, device): # This method allows the user to specify a different device # It sets the corresponding attribute (to be used later in # the mini-batches) and sends the model to the device try: self.device = device self.model.to(self.device) except RuntimeError: self.device = 'cuda' if torch.cuda.is_available() else 'cpu' print(f"Couldn't send it to {device}, sending it to {self.device} instead.") self.model.to(self.device) def set_loaders(self, train_loader, val_loader=None): self.train_loader = train_loader self.val_loader = val_loader def set_tensorboard(self, name, folder='runs'): # This method allows the user to create a SummaryWriter to interface with TensorBoard suffix = datetime.now().strftime('%Y%m%d%H%M%S') self.writer = SummaryWriter(f'{folder}/{name}_{suffix}')
class StepByStep(): def __init__(self, model, loss_fn, optimizer): self.device = 'cuda' if torch.cuda.is_available() else 'cpu' self.model = model.to(self.device) self.loss_fn = loss_fn self.optimizer = optimizer # These attributes are defined here, but since they are # not available at the moment of creation, we keep them None self.train_loader = None self.val_loader = None self.writer = None # These attributes are going to be computed internally self.losses = [] self.val_losses = [] self.total_epochs = 0 def to(self, device): # This method allows the user to specify a different device # It sets the corresponding attribute (to be used later in # the mini-batches) and sends the model to the device try: self.device = device self.model.to(self.device) except RuntimeError: self.device = 'cuda' if torch.cuda.is_available() else 'cpu' print(f"Couldn't send it to {device}, sending it to {self.device} instead.") self.model.to(self.device) def set_loaders(self, train_loader, val_loader=None): self.train_loader = train_loader self.val_loader = val_loader def set_tensorboard(self, name, folder='runs'): # This method allows the user to create a SummaryWriter to interface with TensorBoard suffix = datetime.now().strftime('%Y%m%d%H%M%S') self.writer = SummaryWriter(f'{folder}/{name}_{suffix}')
标签:Chapter,None,self,loader,Going,cuda,device,2.1,model From: https://www.cnblogs.com/zhangzhihui/p/18462649