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

专业的网站设计公司有没有女的做任务的网站

专业的网站设计公司,有没有女的做任务的网站,手机网络优化,长沙ui设计公司对于多项式回归#xff0c;可以同样使用前面线性回归中定义的LinearRegression算子、训练函数train、均方误差函数mean_squared_error#xff0c;生成数据集create_toy_data,这里就不多做赘述咯~ 拟合的函数为 def sin(x):y torch.sin(2 * math.pi * x)return y1.数据集的建…对于多项式回归可以同样使用前面线性回归中定义的LinearRegression算子、训练函数train、均方误差函数mean_squared_error生成数据集create_toy_data,这里就不多做赘述咯~ 拟合的函数为 def sin(x):y torch.sin(2 * math.pi * x)return y1.数据集的建立 func sin interval (0,1) train_num 15 test_num 10 noise 0.5 X_train, y_train create_toy_data(funcfunc, intervalinterval, sample_numtrain_num, noise noise) X_test, y_test create_toy_data(funcfunc, intervalinterval, sample_numtest_num, noise noise)X_underlying torch.linspace(interval[0], interval[1], 100) y_underlying sin(X_underlying)# 绘制图像 plt.rcParams[figure.figsize] (8.0, 6.0) plt.scatter(X_train, y_train, facecolornone, edgecolor#e4007f, s50, labeltrain data) #plt.scatter(X_test, y_test, facecolornone, edgecolorr, s50, labeltest data) plt.plot(X_underlying, y_underlying, c#000000, labelr$\sin(2\pi x)$) plt.legend(fontsizex-large) plt.savefig(ml-vis2.pdf) plt.show() 生成结果:  2.模型构建 # 多项式转换 def polynomial_basis_function(x, degree2):输入- x: tensor, 输入的数据shape[N,1]- degree: int, 多项式的阶数example Input: [[2], [3], [4]], degree2example Output: [[2^1, 2^2], [3^1, 3^2], [4^1, 4^2]]注意本案例中,在degree1时不生成全为1的一列数据degree为0时生成形状与输入相同全1的Tensor输出- x_result tensorif degree 0:return torch.ones(x.size(), dtypetorch.float32)x_tmp xx_result x_tmpfor i in range(2, degree 1):x_tmp torch.multiply(x_tmp, x) # 逐元素相乘x_result torch.concat((x_result, x_tmp), dim-1)return x_result我的理解多项式回归的模型的建立更像是将原来的一个x变成x x^2 x^3 ... x^degree这个过程 3.模型训练 plt.rcParams[figure.figsize] (12.0, 8.0)for i, degree in enumerate([0, 1, 3, 8]): # []中为多项式的阶数model Linear(degree)X_train_transformed polynomial_basis_function(X_train.reshape([-1, 1]), degree)X_underlying_transformed polynomial_basis_function(X_underlying.reshape([-1, 1]), degree)model optimizer_lsm(model, X_train_transformed, y_train.reshape([-1, 1])) # 拟合得到参数y_underlying_pred model(X_underlying_transformed).squeeze()print(model.params)# 绘制图像plt.subplot(2, 2, i 1)plt.scatter(X_train, y_train, facecolornone, edgecolor#e4007f, s50, labeltrain data)plt.plot(X_underlying, y_underlying, c#000000, labelr$\sin(2\pi x)$)plt.plot(X_underlying, y_underlying_pred, c#f19ec2, labelpredicted function)plt.ylim(-2, 1.5)plt.annotate(M{}.format(degree), xy(0.95, -1.4))# plt.legend(bbox_to_anchor(1.05, 0.64), loc2, borderaxespad0.) plt.legend(loclower left, fontsizex-large) plt.savefig(ml-vis3.pdf) plt.show()训练结果: 观察可视化结果红色的曲线表示不同阶多项式分布拟合数据的结果 * 当 或 时拟合曲线较简单模型欠拟合 * 当 时拟合曲线较复杂模型过拟合 * 当 时模型拟合最为合理。 4.模型评估 下面通过均方误差来衡量训练误差、测试误差以及在没有噪音的加入下sin函数值与多项式回归值之间的误差更加真实地反映拟合结果。多项式分布阶数从0到8进行遍历。 # 训练误差和测试误差 training_errors [] test_errors [] distribution_errors []# 遍历多项式阶数 for i in range(9):model Linear(i)X_train_transformed polynomial_basis_function(X_train.reshape([-1, 1]), i)X_test_transformed polynomial_basis_function(X_test.reshape([-1, 1]), i)X_underlying_transformed polynomial_basis_function(X_underlying.reshape([-1, 1]), i)optimizer_lsm(model, X_train_transformed, y_train.reshape([-1, 1]))y_train_pred model(X_train_transformed).squeeze()y_test_pred model(X_test_transformed).squeeze()y_underlying_pred model(X_underlying_transformed).squeeze()train_mse mean_squared_error(y_truey_train, y_predy_train_pred).item()training_errors.append(train_mse)test_mse mean_squared_error(y_truey_test, y_predy_test_pred).item()test_errors.append(test_mse)# distribution_mse mean_squared_error(y_truey_underlying, y_predy_underlying_pred).item()# distribution_errors.append(distribution_mse)print(train errors: \n, training_errors) print(test errors: \n, test_errors) # print (distribution errors: \n, distribution_errors)# 绘制图片 plt.rcParams[figure.figsize] (8.0, 6.0) plt.plot(training_errors, -., mfcnone, mec#e4007f, ms10, c#e4007f, labelTraining) plt.plot(test_errors, --, mfcnone, mec#f19ec2, ms10, c#f19ec2, labelTest) # plt.plot(distribution_errors, -, mfcnone, mec#3D3D3F, ms10, c#3D3D3F, labelDistribution) plt.legend(fontsizex-large) plt.xlabel(degree) plt.ylabel(MSE) plt.savefig(ml-mse-error.pdf) plt.show() 可视化结果如下: 由可视化结果可得: 当阶数较低的时候模型的表示能力有限训练误差和测试误差都很高代表模型欠拟合当阶数较高的时候模型表示能力强但将训练数据中的噪声也作为特征进行学习一般情况下训练误差继续降低而测试误差显著升高代表模型过拟合。 对于模型过拟合的情况可以引入正则化方法通过向误差函数中添加一个惩罚项来避免系数倾向于较大的取值。 degree 8 # 多项式阶数 reg_lambda 0.0001 # 正则化系数X_train_transformed polynomial_basis_function(X_train.reshape([-1,1]), degree) X_test_transformed polynomial_basis_function(X_test.reshape([-1,1]), degree) X_underlying_transformed polynomial_basis_function(X_underlying.reshape([-1,1]), degree)model Linear(degree) optimizer_lsm(model,X_train_transformed,y_train.reshape([-1,1]))y_test_predmodel(X_test_transformed).squeeze() y_underlying_predmodel(X_underlying_transformed).squeeze()model_reg Linear(degree) optimizer_lsm(model_reg,X_train_transformed,y_train.reshape([-1,1]),reg_lambdareg_lambda)y_test_pred_regmodel_reg(X_test_transformed).squeeze() y_underlying_pred_regmodel_reg(X_underlying_transformed).squeeze()mse mean_squared_error(y_true y_test, y_pred y_test_pred).item() print(mse:,mse) mes_reg mean_squared_error(y_true y_test, y_pred y_test_pred_reg).item() print(mse_with_l2_reg:,mes_reg)# 绘制图像 plt.scatter(X_train, y_train, facecolornone, edgecolor#e4007f, s50, labeltrain data) plt.plot(X_underlying, y_underlying, c#000000, labelr$\sin(2\pi x)$) plt.plot(X_underlying, y_underlying_pred, c#e4007f, linestyle--, label$deg. 8$) plt.plot(X_underlying, y_underlying_pred_reg, c#f19ec2, linestyle-., label$deg. 8, \ell_2 reg$) plt.ylim(-1.5, 1.5) plt.annotate(lambda{}.format(reg_lambda), xy(0.82, -1.4)) plt.legend(fontsizelarge) plt.savefig(ml-vis4.pdf) plt.show() 可视化结果为: 多项式回归的难点就是是否真正理解了线性回归中的算子均方误差等函数的目的和用法其他的就是简单的函数调用问题啦~不懂的话还是建议多看看线性函数先把线性函数的看明白最好~
http://www.hkea.cn/news/14575278/

相关文章:

  • 四川省城乡和住房建设厅官方网站wordpress微博图床优点缺点
  • 网站标题 关键字零基础编程入门自学
  • 户外网站设计建站之星模板的使用
  • 企业网站营销实现方式淄博 网站seo优化
  • 中华室内设计网招聘网站内部代码优化
  • 飞凡网站建设为什么谷歌浏览器打不开网页
  • 设计网站多少费用多少平台期什么意思
  • 网站建设明确细节网页设计与网站建设考试名词解释
  • 网站数据库多大合适wordpress开发教程 pdf
  • 做网站用框架衡阳退休职工做面膜网站
  • 做网站好的网站建设公司网页制作第一步
  • 自己做一元购网站常见的系统优化软件
  • 长沙网站列表珠海商城网站建设
  • 上海智能网站建设设计保洁公司在哪个网站做推广比较好
  • 南通网站建设有限公司网站设计的任务
  • 电商网站seo公司做h5的免费软件
  • 慧聪网seo页面优化百度seo外包
  • 济南小程序网站制作哪家公司建别墅好
  • 网站服务器做下载链接邯郸市市长
  • 网站开发的内容网站首页做的好看
  • 国外的设计网站推荐wordpress访问很慢吗?
  • akm建站系统工业设计大赛
  • 旅游业网站建设方案特点wordpress自定义主题怎么拷过去
  • 开源程序网站软件详细设计文档
  • 江苏城工建设科技有限公司网站企业解决方案提供商
  • 网站建设的目的意义茂名网站开发
  • 外贸流程询盘发盘网站常用的优化方法
  • 建立网站原则做网站需要注册哪类商标
  • 个人做论坛网站需要哪些备案网站营销策划公司
  • 金华网站建设电话网站服务器 免费