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

简单详细搭建网站教程wordpress文件夹分类名称

简单详细搭建网站教程,wordpress文件夹分类名称,婚纱摄影网站管理系统,青岛做教育的网站在深度学习的过程中#xff0c;需要将训练好的模型运用到我们要使用的另一个程序中#xff0c;这就需要模型的下载与转移操作 代码#xff1a; import math import torch from torch import nn from d2l import torch as d2l import matplotlib.pyplot as plt# 生成随机的…在深度学习的过程中需要将训练好的模型运用到我们要使用的另一个程序中这就需要模型的下载与转移操作 代码 import math import torch from torch import nn from d2l import torch as d2l import matplotlib.pyplot as plt# 生成随机的数据集 max_degree 20 # 多项式的最大阶数 n_train, n_test 100, 100 # 训练和测试数据集大小 true_w torch.zeros(max_degree) true_w[0:4] torch.Tensor([5, 1.2, -3.4, 5.6])# 生成特征 features torch.randn((n_train n_test, 1)) permutation_indices torch.randperm(features.size(0)) # 使用随机排列的索引来打乱features张量原地修改 features features[permutation_indices] poly_features torch.pow(features, torch.arange(max_degree).reshape(1, -1)) for i in range(max_degree):poly_features[:, i] / math.gamma(i 1)# 生成标签 labels torch.matmul(poly_features, true_w) labels torch.normal(0, 0.1, sizelabels.shape)# 以下是你原来的训练函数没有修改 def evaluate_loss(net, data_iter, loss):metric d2l.Accumulator(2)for X, y in data_iter:out net(X)y y.reshape(out.shape)l loss(out, y)metric.add(l.sum(), l.numel())return metric[0] / metric[1]def l2_penalty(w):w w[0].weightreturn torch.sum(w.pow(2)) / 2def train(train_features, test_features, train_labels, test_labels, lambd,num_epochs100):loss d2l.squared_lossinput_shape train_features.shape[-1]net nn.Sequential(nn.Linear(input_shape, 1, biasFalse)) # 模型batch_size min(10, train_labels.shape[0])train_iter d2l.load_array((train_features, train_labels.reshape(-1, 1)),batch_size)test_iter d2l.load_array((test_features, test_labels.reshape(-1, 1)),batch_size, is_trainFalse)# 用于存储训练和测试损失的列表train_losses []test_losses []total_loss 0total_samples 0for epoch in range(num_epochs):for X, y in train_iter:out net(X)y y.reshape(-1, 1) # 确保y是二维的l loss(out, y) lambd * l2_penalty(net)# 反向传播和优化器更新l.sum().backward()d2l.sgd(net.parameters(), lr0.01, batch_size batch_size)total_loss l.sum().item() # 统计所有元素损失total_samples y.numel() # 统计个数a total_loss / total_samples # 本次训练的平均损失train_losses.append(a)test_loss evaluate_loss(net, test_iter, loss)test_losses.append(test_loss)total_loss 0total_samples 0print(fEpoch {epoch 1}/{num_epochs}:)print(f训练损失 {a:.4f} 测试损失 {test_loss:.4f} )print(net[0].weight)torch.save(net.state_dict(), NetSave) # 存模型net_try nn.Sequential(nn.Linear(input_shape, 1, biasFalse))print(net_try)print(net_try[0].weight)net_try.load_state_dict(torch.load(NetSave))net_try.eval() # 评估模式print(net_try_load)print(net_try[0].weight)# 绘制损失曲线plt.figure(figsize(10, 6))plt.plot(train_losses, labeltrain, colorblue, linestyle-, marker.)plt.plot(test_losses, labeltest, colorpurple, linestyle--, marker.)plt.xlabel(epoch)plt.ylabel(loss)plt.title(Loss over Epochs)plt.legend()plt.grid(True)plt.ylim(0, 1) # 设置y轴的范围从0.01到100plt.show()# 选择多项式特征中的前4个维度 train(poly_features[:n_train, :4], poly_features[n_train:, :4],labels[:n_train], labels[n_train:], 0)## net.parameters() 是一个 PyTorch 模型的方法用于返回模型所有参数的迭代器。这个迭代器产生模型中所有可学习的参数例如权重和偏置。上述代码的模型是简单线性模型 net nn.Sequential(nn.Linear(input_shape, 1, biasFalse)) # 模型此模型的下载与储存如下 torch.save(net.state_dict(), NetSave) # 存模型net_try nn.Sequential(nn.Linear(input_shape, 1, biasFalse)) # 搭建模型框架print(net_try)print(net_try[0].weight)net_try.load_state_dict(torch.load(NetSave)) # 下载模型net_try.eval() # 评估模式print(net_try_load)print(net_try[0].weight)效果 所以说要想在另一个程序中将训练好的模型加载到上面去首先要保存训练好的模型另一个程序必须有和本模型一样的框架再将训练好的模型权重加载到另一个程序框架内即可
http://www.zqtcl.cn/news/245271/

相关文章:

  • 长春绿园网站建设哪里制作企业网站
  • 建设网站计划ppt模板核酸二维码
  • 宁波网络推广制作seo关键词推广公司
  • 东莞市网站推广西安推广公司无网不胜
  • 全国网站建设有实力建筑人才网123
  • 海安网站设计公司网站开发好学嘛
  • 网站建设深圳公司上海贸易公司注册条件
  • 深圳市坪山新区建设局网站给别人做网站去掉版权
  • 怎么做监测网站的浏览量有没有专业做股指的评论网站
  • 济南微信网站开发网上效果代码网站可以下载吗
  • 门户网站的设计常见的管理信息系统有哪些
  • 网站添加悬浮二维码成都游戏网站开发
  • 用jquery做网站百度seo排名规则
  • 免备案手机网站室内设计说明
  • 网站被做站公司贩卖怎样将qq空间建设为个人网站
  • 网站开发有哪几类淮安app开发公司
  • 营销网站建设公司哪家好兵团第二师建设环保局网站
  • 做推广最好的网站是哪个深圳办公室装修招标
  • 郑州高端网站制作wordpress那个版本好
  • wordpress屏蔽右键f12奉化首页的关键词优化
  • cn域名做犯法网站做电影网站需要哪些证
  • 官方网站有哪些韶关做网站的公司
  • 商城网站设计公司怎么样网站制作预算
  • 在济南什么人想做网站网站建设后怎么做主页
  • 联合年检怎么做网站上国家备案查询
  • 社交网站wap模板wordpress网址导航插件
  • 沈阳快速建站公司有哪些国外做二手服装网站
  • 手机如何建立网站平台seo比较好的优化
  • 电商网站建设外包禅城南庄网站制作
  • 哈尔滨企业网站开发报价免费php网站源码