当前位置:网站首页>Machine learning: linear regression

Machine learning: linear regression

2022-06-24 21:57:00 Weng Weiqiang

low-level API Realization :

1. Random initialization data

import matplotlib.pyplot as plt
import tensorflow as tf
TRUE_W=3.0
TRUE_b=2.0
NUM_SAMPLES=100
# Initialize random data 
X=tf.random.normal(shape=[NUM_SAMPLES,1]).numpy()
noise=tf.random.normal(shape=[NUM_SAMPLES,1]).numpy()
y=X*TRUE_W+TRUE_b+noise # Add noise 
plt.scatter(X,y)

2.

Define the univariate regression model and fit the curve :

𝑓(𝑤,𝑏,𝑥)=𝑤∗𝑥+𝑏

class Model(object): #object Body of model 

    def __init__(self):
        self.W = tf.Variable(tf.random.uniform([1]))  #  Random initialization parameters 
        self.b = tf.Variable(tf.random.uniform([1]))

    def __call__(self, x):
        return self.W * x + self.b  # w*x + b
model = Model()  #  Instantiation model 

plt.scatter(X, y)
plt.plot(X, model(X), c='r')  

It can be seen that the fitting effect is not very good So continue training the model  

3. Using the loss function Go to Perform gradient descent iteration Get good fitting results

Loss function :

Update parameters :

𝑏←b−𝑙𝑟∗∂loss(𝑤,𝑏)

w←w−𝑙𝑟∗∂loss(𝑤,𝑏)

lr It refers to the learning rate

The last iteration is ten times


def loss_fn(model,x,y):
    y_=model(x)
    return tf.reduce_mean(tf.square(y_ -y))
EPOCHS  =10
LEARNING_RATE=0.1
for epoch in range (EPOCHS): # The number of iterations 
 with tf.GradientTape() as tape:
        loss=loss_fn(model,X,y)# Calculate the loss 
        dW,db=tape.gradient(loss,[model.W,model.b]) # Calculate the gradient 
        model.W.assign_sub(LEARNING_RATE*dW)
        model.b.assign_sub(LEARNING_RATE*db)
        # Output calculation results 
        print(f'Epoch[{epoch}/{EPOCHS}], loss[{loss}], W/b[{model.W.numpy()}/{model.b.numpy()}]')
     
        plt.scatter(X, y)
        plt.plot(X, model(X), c='r')

The following results are obtained :

Higher order API Realization :

Use tensorflow In an existing library keras

model = tf.keras.Sequential()  #  Create a new sequence model 
model.add(tf.keras.layers.Dense(units=1, input_dim=1))  #  Add a linear layer 
model.compile(optimizer='sgd', loss='mse')  #  Define loss function and optimization method 
model.fit(X, y, epochs=10, batch_size=32)  #  Training models 

原网站

版权声明
本文为[Weng Weiqiang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241510456605.html