VBScript Deep Learning Introduction - Linear Regression

background

Broken computers cannot install mainstream compilers such as VS, Py, IDea, Golang or their language operating environments, but they come with .Net FrameWork 3.5, and can use VBScript for scripting. Learning framework.

analyze

After understanding the principle of the deep learning algorithm of linear regression, you can use vbs for code implementation, and deeply understand the essence of this algorithm.

Suppose there is a training set

x(10)={1,2,3,4,5,6,7,8,9,10}

y(10)={11,21,31,41,51,61,71,81,91,101}

I believe everyone has seen it, y(i)=x(i)*10+1, where i=0~9

But the computer doesn't know this, we need to tell the computer algorithm to let it find the coefficient k=10, the intercept b=1, and then let it calculate the test set

input(11)={20,21,22,23,24,25,26,27,28,29,30} corresponds to the value of output(11)

Define the summation function Forward to perform forward operation

' 前向运算
Function Forward(x,k,b)
	Forward=x*k+b
End Function

Define the loss function Loss and perform loss evaluation

' 损失值
Function Loss(res,y)
	Loss=(res-y)^2/2
End Function

Coefficient k and intercept b are initialized to a random number from 0 to 1

' 随机种子
Randomize
' 初始给k、b随机赋值
Dim k,b
k=Rnd()
b=Rnd()

After iterating 500 rounds of training, the result is obtained

It can be seen that the results are in line with expectations, the algorithm can be mastered, and the code structure, modelization, and engineering still need to be improved. I hope to make persistent efforts with you.

Complete code [main.vbs]

' 启用显式编程风格,所有变量使用前必须定义
Option Explicit

' 简单封装输出函数
Sub Echo(s)
	Wscript.Echo s
End Sub

' 训练数据 x、y
Dim x:x=Array(1,2,3,4,5,6,7,8,9,10)
Dim y:y=Array(11,21,31,41,51,61,71,81,91,101)

' 随机种子
Randomize
' 初始给k、b随机赋值
Dim k,b
k=Rnd()
b=Rnd()

' 前向运算
Function Forward(x,k,b)
	Forward=x*k+b
End Function

' 损失值
Function Loss(res,y)
	Loss=(res-y)^2/2
End Function

' 训练轮数
Dim epoch:epoch=500
' 学习率
Dim lr:lr=0.01

Dim i,j
' 预测值
Dim yh
' 损失总和
Dim ls

' 系数损失
Dim kh
' 截距损失
Dim bh

' 训练
For i=0 To epoch
	For j=0 To UBound(x)
		yh=Forward(x(j),k,b)
		ls=Loss(yh,y(j))
		kh=(yh-y(j))*x(j)
		bh=yh-y(j)
		k=k-kh*lr
		b=b-bh*lr
		Echo ls
	Next
Next

' k、b保留最后三位小数
Echo "k="&k
Echo "b="&b
Echo "保留三位小数"
k=Round(k,3)
b=Round(b,3)
Echo "k="&k
Echo "b="&b

' 测试集
Dim input:input=Array(20,21,22,23,24,25,26,27,28,29,30)
Dim output
For i=0 To UBound(input)
	output=Forward(input(i),k,b)
	' 结果保留三位小数
	output=Round(output,3)
	Echo input(i)&"*"&k&"+"&b&"="&output
Next

Guess you like

Origin blog.csdn.net/qq_36694133/article/details/131029493