task1
#梯度下降法实现 # numpy实现 import numpy as np x = np.array([1,2,3]) y = np.array([2,4,6]) epoches = 10 lr = 0.1 w = 0 cost=[] for epoch in range(epoches): yhat = x*w loss = np.average((yhat-y)**2) cost.append(loss) dw = -2*(y-yhat)@ x.T/(x.shape[0]) w=w-lr*dw print(w)# L1,L2,elastic-net正则化 def l1_normal(x): return torch.abs(x).sum() def l2_normal(x): return torch.pow(x,2).sum() def elastic_net(x,a,b): return l1_normal(x)*a+l2_normal(x)*b
Last updated