The structure of this article:
- What is a linear unit
- What's the use?
- Code implementation
1. What is a linear unit
The difference between a linear element and a perceptron is in the activation function:
The f of the perceptron is the order function:
The activation function of the linear element is linear:
So the formula for the linear model is as follows:
2. What's the use?
A problem with the perceptron is that it may not converge when it encounters linearly irreducible data, so a linear function can be used instead of the step function, the linear element, so that it converges to an optimal approximation.
3. Code implementation
1. Inherit Perceptron, initialize the linear unit
from Import Perceptron # define activation function f Lambda x:x class Linearunit (Perceptron): def __init__ (self, input_num): " " initialize the linear element and set the number of input parameters " " Perceptron. __init__ (Self, input_num, f)
2. Define a linear unit, call train_linear_unit
for training
- Weight earned by print training
- Input parameter value [3.4] test the predicted value
if __name__=='__main__': " "Train linear units" "Linear_unit=Train_linear_unit ()#weight earned by print training PrintLinear_unit#Test Print 'Work 3.4 years, monthly salary =%.2f'% Linear_unit.predict ([3.4]) Print 'Work years, monthly salary =%.2f'% Linear_unit.predict ([15]) Print 'Work 1.5 years, monthly salary =%.2f'% Linear_unit.predict ([1.5]) Print 'Work 6.3 years, monthly salary =%.2f'% linear_unit.predict ([6.3])
- The process of training is:
- Get Training data,
- Set the number of iterations, learning rate and other parameters
- and return to the trained linear unit.
def train_linear_unit (): " " using the Data train linear unit " # to create the Perceptron, the number of characteristics of the input parameter is 1 (working life) lu = linearunit (1 # Training , Iteration 10 rounds, learning rate of 0.01input_vecs, labels = get_training_dataset () 0.01) # return the well-trained linear unit return Lu
Full code
fromPerceptronImportPerceptron#define activation function ff =Lambdax:xclassLinearunit (Perceptron):def __init__(self, input_num):" "initialize the linear element and set the number of input parameters" "Perceptron.__init__(self, input_num, f)defGet_training_dataset ():" "Fabricate 5 People's income data" " #Building Training Data #Enter a list of vectors, each of which is the working lifeInput_vecs = [[5], [3], [8], [1.4], [10.1]] #expected output list, monthly salary, note to correspond with input one by oneLabels = [5500, 2300, 7600, 1800, 11400] returnInput_vecs, Labelsdeftrain_linear_unit ():" "train linear units with data" " #create Perceptron with 1 characteristics of input parameters (working life)LU = Linearunit (1) #train, iterate 10 rounds, learning rate is 0.01Input_vecs, labels =Get_training_dataset () lu.train (input_vecs, labels,10, 0.01) #return to the well-trained linear unit returnLuif __name__=='__main__': " "Train linear units" "Linear_unit=Train_linear_unit ()#weight earned by print training PrintLinear_unit#Test Print 'Work 3.4 years, monthly salary =%.2f'% Linear_unit.predict ([3.4]) Print 'Work years, monthly salary =%.2f'% Linear_unit.predict ([15]) Print 'Work 1.5 years, monthly salary =%.2f'% Linear_unit.predict ([1.5]) Print 'Work 6.3 years, monthly salary =%.2f'% linear_unit.predict ([6.3])
Learning materials:
https://www.zybuluo.com/hanbingtao/note/448086
Linear element of neural network