建网站的公司德阳建网站的公司,网址管理系统,有哪里可以做兼职翻译的网站,铜陵做网站本文目标#xff1a; 以这个公式为例#xff0c;设计一个算法#xff0c;用梯度下降法来模拟训练过程#xff0c;最终得出参数a,b,c 原理介绍 目标函数#xff1a; 损失函数#xff1a;#xff0c;就是mse 损失函数展开#xff1a; 损失函数对a,b,c求导数: 导数就是梯度… 本文目标 以这个公式为例设计一个算法用梯度下降法来模拟训练过程最终得出参数a,b,c 原理介绍 目标函数 损失函数就是mse 损失函数展开 损失函数对a,b,c求导数: 导数就是梯度也就是目标参数与当前参数的差异,这个差异需要用梯度下降法更新 重复上面的过程参数就可以更新了然后可以看看新参数的效果,也就是损失有没有降低 具体流程 预设模型的表达式为也就是二次函数。同时随机初始化模型参数a,b,c。如果是其他函数如就无法在本版本适用修改求导方式后才可用。即本模型需要提前知道模型的表达式。通过不断喂入(x_input,y_true),得出.而y_out与y_true之间具有差异。将差异封装成一个loss函数并分别对a,b,c进行求导。得到a,b,c的梯度将和原始的参数a,b,c和学习率作为输入用梯度下降法来对a,b,c参数进行更新.重复2,3,4过程。直到训练结束或者loss降低到较小值 python实现 # 初始化a,b,c为-11/6 , -395/3,-2400 目标a,b,c为2,-4,3 class QuadraticFunc():def drew(self,w,nameshow):a,b,c wx1 np.array(range(-80,80))y1 a*x1*x1 b*x1 cy2 2*x1*x1 - 4*x1 3plt.clf()plt.plot(x1, y1)plt.plot(x1, y2)plt.scatter(x1, y1, cr)# set colorplt.xlim((-50,50))plt.ylim((-500,500))plt.xlabel(X Axis)plt.ylabel(Y Axis)if name first:plt.pause(3)else:plt.pause(0.01)plt.ioff()#计算lossdef cal_loss(self,y_out,y_true):# return np.dot((y_out - y_true),(y_out - y_true)) * 0.5return np.mean((y_out - y_true)**2)#计算梯度 def cal_grad(self,x,y_out,y_true):# x(batch),y_out(batch),y_true(batch)a_grad (y_out-y_true)*x**2 #b_grad (y_out-y_true)*xc_grad (y_out-y_true)return np.array([np.mean(a_grad),np.mean(b_grad),np.mean(c_grad)]) #梯度下降法更新参数def update_theta(self,step,w,grad):new_w w - step*gradreturn new_wdef run(self):feed_x np.array(range(-400,400))/400feed_y 2*feed_x*feed_x - 4*feed_x 3step 0.5base_lr 0.5lr base_lr# 初始化参数a,b,c -11/6 , -395/3,-2400#-1,10,26w np.array([a,b,c])self.drew(w,first)epochs 100for epoch in range(epochs):# 每隔10轮 降低一半的学习率lr base_lr/(2**(int((epoch1)/10)))for i in range(len(feed_x)):x_input feed_x[i]y_true feed_y[i]y_out w[0]*x_input*x_input w[1]*x_input w[2]#计算lossloss self.cal_loss(y_out,y_true)#计算梯度grad self.cal_grad(x_input,y_out,y_true)#更新参数,梯度下降w self.update_theta(lr,w,grad)# self.drew(w)grad np.round(grad,2)loss np.round(loss,2)w np.round(w,2)print(train times is:,epoch, grad is:,grad, loss is:,%.4f%loss, w is:,w,\n)self.drew(w)if loss1e-5:print(train finish:,w)breakdef run_batch(self):feed_x np.array(range(-400,400))/400feed_y 2*feed_x*feed_x - 4*feed_x 3x_y [[feed_x[i],feed_y[i]] for i in range(len(feed_x))]base_lr 0.5lr base_lr# 初始化参数a,b,c -11/6 , -395/3,-2400#-1,10,26w np.array([a,b,c])self.drew(w,first)batch_size 16data_len len(x_y)//batch_sizeepochs 100for epoch in range(epochs):random.shuffle(x_y)# 每隔10轮 降低一半的学习率lr base_lr/(2**(int((epoch1)/10)))print(epoch,lr:,epoch,lr)for i in range(data_len):x_y_list x_y[i*batch_size:(i1)*batch_size]x_y_np np.array(x_y_list)x_input x_y_np[:,0]y_true x_y_np[:,1]y_out w[0]*x_input*x_input w[1]*x_input w[2]#计算lossloss self.cal_loss(y_out,y_true)#计算梯度grad self.cal_grad(x_input,y_out,y_true)#更新参数,梯度下降w self.update_theta(lr,w,grad)grad np.round(grad,2)loss np.round(loss,2)w np.round(w,2)print(train times is:,epoch, grad is:,grad, loss is:,%.4f%loss, w is:,w,\n)self.drew(w)if loss1e-5:print(train finish:,w)# breaktime.sleep(0.1)if __name__ __main__:qf QuadraticFunc()qf.run() 文章转载自: http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.wtyqs.cn.gov.cn.wtyqs.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.lzqtn.cn.gov.cn.lzqtn.cn http://www.morning.mnbcj.cn.gov.cn.mnbcj.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.cnlmp.cn.gov.cn.cnlmp.cn http://www.morning.bkylg.cn.gov.cn.bkylg.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.xuejitest.com.gov.cn.xuejitest.com http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.cykqg.cn.gov.cn.cykqg.cn http://www.morning.tbqdm.cn.gov.cn.tbqdm.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.ljbm.cn.gov.cn.ljbm.cn http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.yqtry.cn.gov.cn.yqtry.cn http://www.morning.ypjjh.cn.gov.cn.ypjjh.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.snnb.cn.gov.cn.snnb.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn http://www.morning.mxnfh.cn.gov.cn.mxnfh.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.wztlr.cn.gov.cn.wztlr.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.kfysh.com.gov.cn.kfysh.com http://www.morning.ppzgr.cn.gov.cn.ppzgr.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.tbcfj.cn.gov.cn.tbcfj.cn http://www.morning.ngjpt.cn.gov.cn.ngjpt.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.nfbkz.cn.gov.cn.nfbkz.cn http://www.morning.mmqhq.cn.gov.cn.mmqhq.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.qcslh.cn.gov.cn.qcslh.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.fllfc.cn.gov.cn.fllfc.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.wmlby.cn.gov.cn.wmlby.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.qdrrh.cn.gov.cn.qdrrh.cn http://www.morning.phlwj.cn.gov.cn.phlwj.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.cndxl.cn.gov.cn.cndxl.cn http://www.morning.ysjjr.cn.gov.cn.ysjjr.cn http://www.morning.wclxm.cn.gov.cn.wclxm.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.qftzk.cn.gov.cn.qftzk.cn http://www.morning.sskhm.cn.gov.cn.sskhm.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.qmbpy.cn.gov.cn.qmbpy.cn http://www.morning.pzjrm.cn.gov.cn.pzjrm.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.jnkng.cn.gov.cn.jnkng.cn http://www.morning.kndst.cn.gov.cn.kndst.cn