Implementation of linear fitting by Python


One dimensional linear fitting

The data is y = 4x + 5 plus noise


import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
from torch.autograd import Variable
import torch
from torch import nn
X = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)
Y = 4*X + 5 + torch.rand(X.size())
class LinearRegression(nn.Module):
 def __init__(self):
  super(LinearRegression, self).__init__()
  self.linear  = nn.Linear (1, 1) the dimensions of input and output are both 1
 def forward(self, X):
  out = self.linear(X)
  return out
model = LinearRegression()
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-2)
num_epochs = 1000
for epoch in range(num_epochs):
 inputs = Variable(X)
 target = Variable(Y)
 #Forward propagation
 out = model(inputs)
 loss = criterion(out, target)
 #Backward propagation
 optimizer.zero_ Grad () ා note that each iteration needs to be cleared
 if (epoch + 1) % 20 == 0:
  print('Epoch[{}/{}], loss:{:.6f}'.format(epoch + 1, num_epochs, loss.item()))
predict = model(Variable(X))
predict =
plt.plot(X.numpy(), Y.numpy(), 'ro', label='Original Data')
plt.plot(X.numpy(), predict, label='Fitting Line')


from itertools import count
import torch
import torch.autograd
import torch.nn.functional as F
def make_features(x):
 """Builds features i.e. a matrix with columns [x, x^2, x^3]."""
 x = x.unsqueeze(1)
 return[x ** i for i in range(1, POLY_DEGREE+1)], 1)
W_target = torch.randn(POLY_DEGREE, 1)
b_target = torch.randn(1)
def f(x):
 return + b_target.item()
def get_batch(batch_size=32):
 random = torch.randn(batch_size)
 x = make_features(random)
 y = f(x)
 return x, y
# Define model
fc = torch.nn.Linear(W_target.size(0), 1)
batch_x, batch_y = get_batch()
for batch_idx in count(1):
 # Get data
 # Reset gradients
 # Forward pass
 output = F.smooth_l1_loss(fc(batch_x), batch_y)
 loss = output.item()
 # Backward pass
 # Apply gradients
 for param in fc.parameters(): *
 # Stop criterion
 if loss < 1e-3:
def poly_desc(W, b):
 """Creates a string description of a polynomial."""
 result = 'y = '
 for i, w in enumerate(W):
  result += '{:+.2f} x^{} '.format(w, len(W) - i)
 result += '{:+.2f}'.format(b[0])
 return result
print('Loss: {:.6f} after {} batches'.format(loss, batch_idx))
print('==> Learned function:\t' + poly_desc(fc.weight.view(-1), fc.bias))
print('==> Actual function:\t' + poly_desc(W_target.view(-1), b_target))

The above article Python to achieve linear fitting is the small editor to share all the content, I hope to give you a reference, also hope that you can support developeppaer.

Recommended Today

The actor model is so excellent under distributed high concurrency

Write at the beginning In general, there are two strategies for communicating in concurrent threads: shared data and messaging. One of the biggest problems of concurrent programming with shared data is data condition competition. It’s a headache to deal with all kinds of locks. Most of the traditional popular language concurrency is based on shared […]