question
There are various classifications in life. The following uses test scores as an example. 0 to 59.99 is considered a failure, and the label is 0; 60 to 100 is a pass, and the label is 1. Although we can directly use the if statement to judge and let the computer get the result directly, if we don't tell the computer that >=60 points are considered a pass, but only give it some labeled data, can it let it know by itself? As for the passing score, the answer is yes.
plan
training set x(10000) from 0.00 to 99.99
y(10000) is 0 or 1, where x(i)>=60 when y(i)=1, otherwise y(i)=0
Logistic regression using the Sigmoid function
The default training times is 500 and the learning rate is 0.1
After the training is over, take an integer from 0 to 99 as the verification set [Actually, this is already included in the training set, it may not be very scientific, it is just an example for everyone to understand the algorithm! 】
the code
Option Explicit
Sub Echo(s)
Wscript.Echo s
End Sub
Dim xs(10000),ys(10000)
Dim i,j
Dim length:length=10000
For i=0 To length-1
xs(i)=i/100.0
ys(i)=0
If xs(i)>=60 Then
ys(i)=1.0
End If
Next
Dim k,b
Randomize
k=Rnd()
b=Rnd()
Echo "k="&k
Echo "b="&b
Function Forward(k,b,x)
Forward=1.0/(1+Exp(-k*x-b))
End Function
Dim epoch:epoch=500
Dim yh
Dim ls
Dim kp,bp
Dim lr:lr=0.1
Dim c
Dim p
For i=1 To epoch
Echo "第"&i&"轮训练"
ls=0
kp=0
bp=0
c=0
For j=0 To length-1
yh=Forward(k,b,xs(j))
ls=ls+yh
bp=bp+yh
kp=(yh-ys(j))*xs(j)
Next
ls=-ls/CDbl(length)
Echo "ls="&ls
kp=kp/CDbl(length)
bp=bp/CDbl(length)
k=k-lr*kp
b=b-lr*bp
For j=0 To length-1
yh=Forward(k,b,xs(j))
If yh<0.5 and ys(j)<0.5 or yh>=0.5 and ys(j)>=0.5 Then
c=c+1
End If
Next
p=c*100/CDbl(length)
Echo "训练集拟合度:"&Round(p,2)&"%"
Echo ""
If p>99.99999999 Then
Exit For
End If
Next
Echo "k="&k
Echo "b="&b
c=0
Dim tx
For i=0 To 99
yh=Forward(k,b,i*1.0)
If i<60 and yh<0.5 or i>=60 and yh>=0.5 Then
c=c+1
End If
Next
p=c*1.0
Echo "测试集拟合度:"&Round(p,2)&"%"
result