Implementation of linear fitting by Python

Time：2020-11-27

One dimensional linear fitting

The data is y = 4x + 5 plus noise

result:

``````import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
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
loss.backward()
optimizer.step()

if (epoch + 1) % 20 == 0:
print('Epoch[{}/{}], loss:{:.6f}'.format(epoch + 1, num_epochs, loss.item()))
model.eval()
predict = model(Variable(X))
predict = predict.data.numpy()
plt.plot(X.numpy(), Y.numpy(), 'ro', label='Original Data')
plt.plot(X.numpy(), predict, label='Fitting Line')
plt.show()``````

Multidimensional:

``````
from itertools import count
import torch
import torch.nn.functional as F

POLY_DEGREE = 3
def make_features(x):
"""Builds features i.e. a matrix with columns [x, x^2, x^3]."""
x = x.unsqueeze(1)

W_target = torch.randn(POLY_DEGREE, 1)
b_target = torch.randn(1)

def f(x):
return x.mm(W_target) + 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()
print(batch_x,batch_y)
for batch_idx in count(1):
# Get data

# Forward pass
output = F.smooth_l1_loss(fc(batch_x), batch_y)
loss = output.item()

# Backward pass
output.backward()

for param in fc.parameters():

# Stop criterion
if loss < 1e-3:
break

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.

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 […]