Pytoch: polynomial regression

Time:2020-11-21

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 = torch.cat([random**i for i in range(1,4)], 1)
    #Matrix multiplication
    y = x.mm(w) + 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 = loss.data.cpu().numpy()
    #Gradient zeroing
    optimizer.zero_grad()
    #Back propagation
    loss.backward()
    #Update parameters
    optimizer.step()
    
    epoch += 1
    if loss_value < 1e-3:
        break
    #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 = torch.cat([x_train**i for i in range(1,4)], 1)
y = x.mm(w) + b
#Drawing data points
plt.plot(x_train.numpy(),y.numpy(),'ro') 
#Extraction of fitting parameters
w_get = model.ploy.weight.data.T
b_get = model.ploy.bias.data
print('w:{},b:{}'.format(w_get.cpu().numpy(), b_get.cpu().numpy()))
#Calculate the predicted value
Y_get = x.mm(w_get.cpu())  + b_get.cpu()
plt.plot(x_train.numpy(), Y_get.numpy(), '-')
plt.show()

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

Pytoch: polynomial regression

4. Comments and suggestions welcome to leave a message