Pytoch: polynomial regression


1. Objectives

Fitting function $f (x) = 2x_ {1}^{3}+3x_ 2^2+4x_ 3+0.5 $

2. Theory

The principle is similar to one-dimensional linear regression and multidimensional linear regression, but the frequency is higher.

3. Implementation

3.1 environment

python == 3.6
torch == 1.4

3.2 construction data

#This is the target weight and offset
w = torch.FloatTensor([2.0, 3.0, 4.0]).unsqueeze(1)
b = torch.FloatTensor([0.5])

def create_data(batch_size=32):
    random = torch.randn(batch_size)
    random =  random.unsqueeze (1) ා add a dimension
    #Longitudinal connection tensor
    x =[random**i for i in range(1,4)], 1)
    #Matrix multiplication
    y = + b[0]
    if torch.cuda.is_available():
        return x.cuda(), y.cuda()
    return x, y

3.3 building models and creating objects

class PloyRegression(nn.Module):
    def __init__(self):
        super(PloyRegression, self).__init__()
        self.ploy = nn.Linear(3,1)
    def forward(self, x):
        out = self.ploy(x)
        return out

model = PloyRegression()
if torch.cuda.is_available():
    model = model.cuda()

3.4 select optimizer

criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=1e-3)

The mean square error is used here, and the random gradient descent is used. The learning rate is 0.001

3.5 start training

epoch = 0
while True:
    #Create data
    batch_x, batch_y = create_data()
    #Forward propagation
    output = model(batch_x)
    #Loss calculation
    loss = criterion(output, batch_y)
    #Get loss value
    loss_value =
    #Gradient zeroing
    #Back propagation
    #Update parameters
    epoch += 1
    if loss_value < 1e-3:
    #Print loss every 100 steps
    if (epoch+1)%100==0:
        print("Epoch{}, loss:{:.6f}".format(epoch+1, loss_value))

3.6 validation results

model.eval () ා enable verification mode

#Construct data
x_train = np.array([[2.167],[3.1],[3.3],[4.168],[4.4],[5.313],[5.5],[6.182],[6.7],[6.9],[7.042],[7.59],[7.997],[9.779],[10.791]], dtype=np.float32)
x_train = torch.from_numpy(x_train)

x =[x_train**i for i in range(1,4)], 1)
y = + b
#Drawing data points
#Extraction of fitting parameters
w_get =
b_get =
print('w:{},b:{}'.format(w_get.cpu().numpy(), b_get.cpu().numpy()))
#Calculate the predicted value
Y_get =  + b_get.cpu()
plt.plot(x_train.numpy(), Y_get.numpy(), '-')

# print:w:[[1.9365442],[2.9985998],[4.012949 ]],b:[0.5068841]

Pytoch: polynomial regression

4. Comments and suggestions welcome to leave a message