· Use PyTorch to fit curves

In the introduction to Deep Learning blog, we used TensorFlow to fit the curve and achieved good results. We now use PyTorch for the same curve fitting to compare TensorFlow with PyTorch. The steps to build a neural network for training are basically the same, and we will now start using PyTorch.

  • Y = x* x-2x +3 + 0.1(random value from -1 to 1) curve given x range (0,3)
  • In the straight line fitting blog, we successfully fitted a straight line using the simplest y=wx+b model, and now we are further fitting the curve. The simple y=wx+ B model can no longer meet our needs, and more neurons are needed to solve the problem.
  • Generate the data
import numpy as np
import matplotlib.pyplot as plt
import torch as t
from torch.autograd import Variable as var

def get_data(x,w,b,d) :
    c,r = x.shape
    y = (w * x * x + b*x + d)+ (0.1* (2*np.random.rand(c,r)-1))
    return(y)

xs = np.arange(0.3.0.01).reshape(-1.1)
ys = get_data(xs,1, -2.3)

xs = var(t.Tensor(xs))
ys = var(t.Tensor(ys))
Copy the code

The generated data images are as follows:

  • Building a network structure
class Fit_model(t.nn.Module) :
    def __init__(self) :
        super(Fit_model,self).__init__()
        self.linear1 = t.nn.Linear(1.16)
        self.relu = t.nn.ReLU()
        self.linear2 = t.nn.Linear(16.1)

        self.criterion = t.nn.MSELoss()
        self.opt = t.optim.SGD(self.parameters(),lr=0.01)
    def forward(self, input) :
        y = self.linear1(input)
        y = self.relu(y)
        y = self.linear2(y)
        return y
Copy the code
  • Training network parameters
model = Fit_model()
for e in range(2000):
    y_pre = model(xs)

    loss = model.criterion(y_pre,ys)
    if(e%100= =0) :print(e,loss.data)
    
    # Zero gradients
    model.opt.zero_grad()
    # perform backward pass
    loss.backward()
    # update weights
    model.opt.step()
Copy the code
  • Show forecast results
ys_pre = model(xs)

plt.title("curve")
plt.plot(xs.data.numpy(),ys.data.numpy())
plt.plot(xs.data.numpy(),ys_pre.data.numpy())
plt.legend("ys"."ys_pre")
plt.show()
Copy the code
  • Run result log:
0 tensor(15.7941) 200 tensor(0.3394) 400 tensor(0.2086) 600 tensor(0.1115) 800 tensor(0.0634) 1000 tensor(0.0422) 1200 Tensor (0.0312) 1400 tensor(0.0244) 1600 tensor(0.0197) 1800 tensor(0.0165) 2000 tensor(0.0140) 2200 tensor(0.0122) 2400 Tensor (0.0108) 2600 tensor(0.0097) 2800 tensor(0.0087) 3000 tensor(0.0080) 3200 tensor(0.0074) 3400 tensor(0.0069) 3600 Tensor (0.0066) 350 tensor(0.0063) 4000 tensor(0.0060)Copy the code

Running result diagram

  • The complete code
import numpy as np
import matplotlib.pyplot as plt
import torch as t
from torch.autograd import Variable as var


def get_data(x,w,b,d) :
    c,r = x.shape
    y = (w * x * x + b*x + d)+ (0.1* (2*np.random.rand(c,r)-1))
    return(y)

xs = np.arange(0.3.0.01).reshape(-1.1)
ys = get_data(xs,1, -2.3)

xs = var(t.Tensor(xs))
ys = var(t.Tensor(ys))

class Fit_model(t.nn.Module) :
    def __init__(self) :
        super(Fit_model,self).__init__()
        self.linear1 = t.nn.Linear(1.16)
        self.relu = t.nn.ReLU()
        self.linear2 = t.nn.Linear(16.1)

        self.criterion = t.nn.MSELoss()
        self.opt = t.optim.SGD(self.parameters(),lr=0.01)
    def forward(self, input) :
        y = self.linear1(input)
        y = self.relu(y)
        y = self.linear2(y)
        return y
        
model = Fit_model()
for e in range(4001):
    y_pre = model(xs)

    loss = model.criterion(y_pre,ys)
    if(e%200= =0) :print(e,loss.data)
    
    # Zero gradients
    model.opt.zero_grad()
    # perform backward pass
    loss.backward()
    # update weights
    model.opt.step()

ys_pre = model(xs)

plt.title("curve")
plt.plot(xs.data.numpy(),ys.data.numpy())
plt.plot(xs.data.numpy(),ys_pre.data.numpy())
plt.legend("ys"."ys_pre")
plt.show()
Copy the code
  • To sum up, with the same number of network parameters, PyTorch and TensorFlow implementations can achieve similar results. To solve the problem, the network structure is the same. The difference lies in the syntax differences between the two frameworks. PyTorch is closer to Python native programming, while TensorFlow is more of a new concept, so beginners to TensorFlow are slower to get started. TensorFlow’s strengths are probably tutorials and community support. Which framework you choose depends on your preferences and your environment.