If you search the web for neural net regression, you will find a number of examples. Most begin by generating some data from a linear or sinusoidal function and adding a bit of noise. They then fit the resulting data with a neural net created with a tool such as PyTorch.
In this post, I want to try to accomplish two things: learn a bit about neural nets and explore PyTorch. Neural nets are often discussed in relation to deep learning. Deep learning typically involves large amounts of data and multiple network layers with complex architectures that are used in areas such as computer vision or speech recognition. The example we are considering isn't particularly deep, but I hope it illustrates a a few simple principles of neural nets. PyTorch is a Python machine learning toolkit developed by Facebook. Its main features are tensor computing and automatic numerical differentiation.
Rather than using generated data, I want to use real data and see what happens as I use a net for regression on the data. The data I will use is Temperature anomaly estimates for the period 1850 to 2021 from the NASA Global Climate Change site. We have seen this data before: here, here, and here.
A Simple Model
We will create a simple neural net with three linear hidden layers connected by LeakyReLU activation functions. The linear layers apply a linear transformation ${y = x{A^T} + b }$ to the data. A LeakyRelU function is linear for positive values and has a small negative slope for negative values.
class Net(torch.nn.Module): def __init__(self, layer1_out = 200, layer2_out = 100): super(Net, self).__init__() self.linear1 = torch.nn.Linear(1, layer1_out) self.linear2 = torch.nn.Linear(layer1_out, layer2_out) self.linear3 = torch.nn.Linear(layer2_out, 1) self.leaky_rlu = torch.nn.LeakyReLU() def forward(self, x): x = self.linear1(x) x = self.leaky_rlu(x) x = self.linear2(x) x = self.leaky_rlu(x) x = self.linear3(x) return x
net = torch.nn.Sequential( torch.nn.Linear(1, 200), torch.nn.LeakyReLU(), torch.nn.Linear(200, 100), torch.nn.LeakyReLU(), torch.nn.Linear(100, 1), )
# construct the model net = Net(layer1_out = layer1_out, layer2_out = layer2_out) optimizer = torch.optim.Adam(net.parameters(), lr = lr) loss_func = torch.nn.MSELoss() # mean squared loss # set up data for loading dataset = Data.TensorDataset(x, y) loader = Data.DataLoader( dataset = dataset, batch_size = batch_size, shuffle = True, num_workers = workers,)
# start training best_model = None min_loss = sys.maxsize for epoch in range(epochs): for step, (batch_x, batch_y) in enumerate(loader): prediction = net(batch_x) loss = loss_func(prediction, batch_y) print('epoch:', epoch, 'step:', step, 'loss:', loss.data.numpy()) if loss.data.numpy() < min_loss: min_loss = loss.data.numpy() best_model = copy.deepcopy(net) optimizer.zero_grad() # clear gradients for next iteration loss.backward() # backpropagation, compute gradients optimizer.step() # apply gradients
No comments:
Post a Comment