省略了數據集的處理過程
x = torch.tensor(input_features, dtype = float)
y = torch.tensor(labels, dtype = float)
weights = torch.randn((14, 128), dtype = float, requires_grad = True)
biases = torch.randn(128, dtype = float, requires_grad = True)
weights2 = torch.randn((128, 1), dtype = float, requires_grad = True)
biases2 = torch.randn(1, dtype = float, requires_grad = True)
learning_rate = 0.001
losses = []
for i in range(1000):
hidden = x.mm(weights) + biases
hidden = torch.relu(hidden)
predictions = hidden.mm(weights2) + biases2
loss = torch.mean((predictions - y) ** 2)
losses.append(loss.data.numpy())
if i % 100 == 0:
print('loss:', loss)
loss.backward()
weights.data.add_(- learning_rate * weights.grad.data)
biases.data.add_(- learning_rate * biases.grad.data)
weights2.data.add_(- learning_rate * weights2.grad.data)
biases2.data.add_(- learning_rate * biases2.grad.data)
weights.grad.data.zero_()
biases.grad.data.zero_()
weights2.grad.data.zero_()
biases2.grad.data.zero_()
loss: tensor(8347.9924, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(152.3170, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(145.9625, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(143.9453, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(142.8161, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(142.0664, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(141.5386, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(141.1528, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(140.8618, dtype=torch.float64, grad_fn=<MeanBackward0>)
loss: tensor(140.6318, dtype=torch.float64, grad_fn=<MeanBackward0>)
更簡單的方法
input_size = input_features.shape[1]
hidden_size = 128
output_size = 1
batch_size = 16
my_nn = torch.nn.Sequential(
torch.nn.Linear(input_size, hidden_size),
torch.nn.Sigmoid(),
torch.nn.Linear(hidden_size, output_size),
)
cost = torch.nn.MSELoss(reduction='mean')
optimizer = torch.optim.Adam(my_nn.parameters(), lr = 0.001)
losses = []
for i in range(1000):
batch_loss = []
for start in range(0, len(input_features), batch_size):
end = start + batch_size if start + batch_size < len(input_features) else len(input_features)
xx = torch.tensor(input_features[start:end], dtype = torch.float, requires_grad = True)
yy = torch.tensor(labels[start:end], dtype = torch.float, requires_grad = True)
prediction = my_nn(xx)
loss = cost(prediction, yy)
optimizer.zero_grad()
loss.backward(retain_graph=True)
optimizer.step()
batch_loss.append(loss.data.numpy())
if i % 100==0:
losses.append(np.mean(batch_loss))
print(i, np.mean(batch_loss))
0 3950.7627
100 37.9201
200 35.654438
300 35.278366
400 35.116814
500 34.986076
600 34.868954
700 34.75414
800 34.637356
900 34.516705
預測
x = torch.tensor(input_features, dtype = torch.float)
predict = my_nn(x).data.numpy()