import torch import torch.optim as optim import torch.nn as nn from torchviz import make_dot device = 'cuda' if torch.cuda.is_available() else 'cpu' # Our data was in Numpy arrays, but we need to transform them into PyTorch's Tensors # and then we send them to the chosen device x_train_tensor = torch.from_numpy(x_train).float().to(device) y_train_tensor = torch.from_numpy(y_train).float().to(device) # Here we can see the difference - notice that .type() is more useful # since it also tells us WHERe the tensor is (device) print(type(x_train), type(x_train_tensor), x_train_tensor.type())
def make_train_step(model, loss_fn, optimizer): # Builds function that performs a step in the train loop def train_step(x, y): # Sets model to TRAIN mode model.train() # Makes predictions yhat = model(x) # Computes loss loss = loss_fn(y, yhat) # Computes gradients loss.backward() # Updates parameters and zeroes gradients optimizer.step() optimizer.zero_grad() # Returns the loss return loss.item() # Returns the function that will be called inside the train loop return train_step
from torch.utils.data.dataset import random_split from torch.utils.data import DataLoader x_tensor = torch.from_numpy(x).float() y_tensor = torch.from_numpy(y).float() dataset = TensorDataset(x_tensor, y_tensor) train_dataset, val_dataset = random_split(dataset, [80, 20]) train_loader = DataLoader(dataset=train_dataset, batch_size=16) val_loader = DataLoader(dataset=val_dataset, batch_size=20)
losses = [] val_losses = [] train_step = make_train_step(model, loss_fn, optimizer) for epoch in range(n_epochs): for x_batch, y_batch in train_loader: x_batch = x_batch.to(device) y_batch = y_batch.to(device) loss = train_step(x_batch, y_batch) losses.append(loss) with torch.no_grad(): for x_val, y_val in val_loader: x_val = x_val.to(device) y_val = y_val.to(device) model.eval() yhat = model(x_val) val_loss = loss_fn(y_val, yhat) val_losses.append(val_loss.item()) print(model.state_dict())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)