线性回归

基本

  • 模型

  • 训练

  • 数据

  • 损失函数

  • 优化


19.3 μs

y=Xw+b

4.5 μs

根据误差,利用梯度对参数Wb进行更新。

W=WlrbatchsizeL(W,b)W

其中batchsize是每批数据训练大小,lr是学习率。

39.9 μs

例子

20.8 μs
63 s
371 ms
x1
1×1000 Array{Float64,2}:
 1.1902678809862768  2.04817970778924  1.142650902867199  …  -0.5439527567544324
31.6 ms
x2
1×1000 Array{Float64,2}:
 -1.6203711972374109  0.11647271641359806  …  2.3396203656224523  -0.6175729562392942
11.9 μs
m
Chain(Dense(2, 1))
336 ms
loss (generic function with 1 method)
18.3 μs
opt
2.2 ms
19.8 s
1×1000 Array{Float32,2}:
 1.9709918  9.5093975  6.4021225  4.3541403  …  5.4332256  7.535674  0.97789097
1 s
760 ns
Predict Value -50 -40 -30 -20 -10 0 10 20 30 40 50 60 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 -50 0 50 -40 -38 -36 -34 -32 -30 -28 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 h,j,k,l,arrows,drag to pan i,o,+,-,scroll,shift-drag to zoom r,dbl-click to reset c for coordinates ? for help ? -50 -40 -30 -20 -10 0 10 20 30 40 50 60 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 -50 0 50 -40 -38 -36 -34 -32 -30 -28 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 reality value Pre vs Real
456 ms

详细步骤:👇

batch_size = 10
#init w, b
w = rand(2)
b = rand(1)

#learning rate
lr = 0.3

#epoch
epoch = 3

linreg(X, w, b) = X*w .+ b
#define Loss
function Loss(ŷ, y)
	sum((ŷ - y) .^ 2)
end

function train_model(batch_size, features, labels, w, b, lr, epoch)
	dt_row, dt_col = size(features)
	indices = 1:dt_row
	Rond_indices = Random.shuffle(indices)
	for j in 1:epoch
		for i in range(1, dt_row, step = batch_size)
			j = Rond_indices[i:min(i+batch_size, dt_row)]
			X = features[j,:]
			y = labels[j,:]
			L = Loss(linreg(X, w, b), y)
			gs = gradient(() -> Loss(linreg(X, w, b), y), Flux.params(w, b))
			w -= lr/batch_size * gs[w]
			b -= lr/batch_size * gs[b]
		end
	end
	return w,b

end

ŵ, b̂ = train_model(batch_size, features, labels, w, b, lr, epoch)

pre_y = features * ŵ .+ b̂

print(pre_y)
8.7 μs