当前位置: 首页 > news >正文

衢州网站建设公司重庆白云seo整站优化

衢州网站建设公司,重庆白云seo整站优化,中文域名网站好不好优化,做徽章标牌的企业网站本文目标: 以这个公式为例,设计一个算法,用梯度下降法来模拟训练过程,最终得出参数a,b,c 原理介绍 目标函数: 损失函数:,就是mse 损失函数展开: 损失函数对a,b,c求导数: 导数就是梯度…

本文目标:

f(x)=a*x^2+b*x+c

以这个公式为例,设计一个算法,用梯度下降法来模拟训练过程,最终得出参数a,b,c

原理介绍

目标函数:h_{\theta}(x) = a^{2}+bx+c

损失函数:Loss=\frac{1}{2m}\sum_{1}^{m}(h_{\theta}(x^{i})-y^{i})^2,就是mse

损失函数展开:Loss=\frac{1}{2m}\sum_{1}^{m}((ax^{i})^{2}+bx^i+c-y^{i})^2

损失函数对a,b,c求导数:

{L_{a}}^{'}=\frac{1}{m}\sum_{1}^{m}(ax^2+bx+c-y)*x^2

{L_{b}}^{'}=\frac{1}{m}\sum_{1}^{m}(ax^2+bx+c-y)*x

{L_{c}}^{'}=\frac{1}{m}\sum_{1}^{m}(ax^2+bx+c-y)

导数就是梯度,也就是目标参数与当前参数的差异,这个差异需要用梯度下降法更新

\Delta a{L_{a}}^{'}   \Delta b={L_{b}}^{'}     \Delta c={L_{c}}^{'}

a = a - lr*\Delta a

b = b - lr*\Delta b

c = c - lr*\Delta c

重复上面的过程,参数就可以更新了,然后可以看看新参数的效果,也就是损失有没有降低

具体流程

  1. 预设模型的表达式为:f(x)=a*x^2+b*x+c,也就是二次函数。同时随机初始化模型参数a,b,c。如果是其他函数如f(x)=ax^3+bx^2+cx,就无法在本版本适用(修改求导方式后才可用)。即本模型需要提前知道模型的表达式。
  2. 通过不断喂入(x_input,y_true),得出y_{out} = ax_{input}^{2}+bx_{input}+c.而y_out与y_true之间具有差异。
  3. 将差异封装成一个loss函数,并分别对a,b,c进行求导。得到a,b,c的梯度\Delta a\Delta b\Delta c
  4. \Delta a\Delta b\Delta c和原始的参数a,b,c和学习率作为输入,用梯度下降法来对a,b,c参数进行更新.
  5. 重复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,name="show"):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, c='r')# 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((epoch+1)/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 loss<1e-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((epoch+1)/10)))print("epoch,lr:",epoch,lr)for i in range(data_len):x_y_list = x_y[i*batch_size:(i+1)*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 loss<1e-5:print("train finish:",w)# breaktime.sleep(0.1)if __name__ == "__main__":qf = QuadraticFunc()qf.run()

http://www.hkea.cn/news/94799/

相关文章:

  • 查看网站源代码建站可以牛排seo系统
  • 政府网站建设的基本原则百度网盘电脑版
  • 张家港网站建设福州百度快速优化
  • 兼职做网站编辑百度搜索推广开户
  • 谁告诉你j2ee是做网站的宁波网站推广找哪家公司
  • 谷歌外贸建站多少钱搭建网站教程
  • 赚钱靠普的网站关键字搜索软件
  • 建设银行深分行圳招聘网站做游戏推广一个月能拿多少钱
  • 北京网站建设及推广招聘关键词排名代做
  • 对网站建设的意见建议网络营销推广的方法有哪些
  • 爬虫网站怎么做怎样才能在百度上面做广告宣传
  • 网站页码南昌做seo的公司有哪些
  • 网络设计方案包括哪些深圳百度推广seo公司
  • 亚马逊跨境电商开店站长工具seo综合查询5g
  • 网站怎么做百度快照logo百度快照优化推广
  • 山西网站建设排名seo技术培训山东
  • 日韩系成人影片成首选网站如何优化推广
  • 网站到期续费通知搜索风云排行榜
  • 网站公司说我们做的网站服务器不够用哪个杭州seo好
  • 类似淘宝网站建设费用杭州哪家seo公司好
  • 装修网站怎样做seo专员很难吗
  • 无锡网站外包如何接广告赚钱
  • 英文网站制作 官网淘宝标题优化网站
  • 电力建设网站网络推广网站的方法
  • 如何做网站窗口网站优化网络推广seo
  • 营销型网站建设效果网络营销策划推广方案
  • 专业的网站搭建多少钱网站seo优化价格
  • 广州公司网站设计制作win10优化大师官网
  • 做调查哪个网站比较可靠百度指数查询
  • 怎么在建设厅网站报名广州网站优化服务