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

苹果电脑做网站设计百度一下网页首页

苹果电脑做网站设计,百度一下网页首页,网站关键词提升,网站店铺建设首先要明确一点,我们在编写模型、训练和使用模型的时候通常都是分开的,所以应该把Module的编写以及train方法和test方法分开编写。 调用gpu进行训练:在网络模型,数据,损失函数对象后面都使用.cuda(&#x…

首先要明确一点,我们在编写模型、训练和使用模型的时候通常都是分开的,所以应该把Module的编写以及train方法和test方法分开编写。

调用gpu进行训练:在网络模型,数据,损失函数对象后面都使用.cuda()方法,如loss_fn = loss_fn.cuda()

【代码示例】完成完整CIFAR10模型的训练

按照官网给出的模型结构进行构建:

在这里插入图片描述

# model.py
class myModule(nn.Module):def __init__(self):super().__init__()self.model = nn.Sequential(nn.Conv2d(3, 32, 5, 1, 2),nn.MaxPool2d(2),nn.Conv2d(32, 32, 5, 1, 2),nn.MaxPool2d(2),nn.Conv2d(32, 64, 5, 1, 2),nn.MaxPool2d(2),nn.Flatten(),nn.Linear(64*4*4, 64),nn.Linear(64, 10))def forward(self, ingput):output = self.model(ingput)return output

导入自己创建的模型,实例化一个模型对象之后,导入CIFAR10数据集进行训练

# train.py
import torchvision
from torch.utils.tensorboard import SummaryWriter
from module import *
from torch import nn
from torch.utils.data import DataLoader# 使用Dataset来下载数据集
train_data = torchvision.datasets.CIFAR10(root="dataset/CIFAR10", train=True, transform=torchvision.transforms.ToTensor(),download=True)
test_data = torchvision.datasets.CIFAR10(root="dataset/CIFAR10", train=False, transform=torchvision.transforms.ToTensor(),download=True)# 数据集长度
train_data_size = len(train_data)
test_data_size = len(test_data)
print("训练数据集的长度为:{}".format(train_data_size))
print("测试数据集的长度为:{}".format(test_data_size))# 利用 DataLoader 来加载数据集
train_dataloader = DataLoader(train_data, batch_size=64)
test_dataloader = DataLoader(test_data, batch_size=64)# 创建网络模型,实例化自定义的模型
mymodule = myModule()
if torch.cuda.is_available():mymodule = mymodule.cuda()# 定义损失函数为交叉熵损失函数
loss_fn = nn.CrossEntropyLoss()
if torch.cuda.is_available():loss_fn = loss_fn.cuda()# 优化器
learning_rate = 0.01
optimizer = torch.optim.SGD(mymodule.parameters(), lr=learning_rate)# 设置训练网络的一些参数
# 记录训练的次数
total_train_step = 0
# 记录测试的次数
total_test_step = 0
# 训练的轮数
epoch = 10# tensorboard配置日志目录
writer = SummaryWriter("logs_train")for i in range(epoch):print("-------第 {} 轮训练开始-------".format(i+1))# 训练步骤开始mymodule.train()for data in train_dataloader:imgs, targets = dataif torch.cuda.is_available():imgs = imgs.cuda()targets = targets.cuda()outputs = mymodule(imgs)loss = loss_fn(outputs, targets)# 优化器优化模型optimizer.zero_grad()loss.backward()optimizer.step()total_train_step = total_train_step + 1   # 每读取一次图片+1if total_train_step % 100 == 0:print("训练次数:{}, Loss: {}".format(total_train_step, loss.item()))writer.add_scalar("train_loss", loss.item(), total_train_step)# 测试步骤开始mymodule.eval()total_test_loss = 0    # 损失函数值total_accuracy = 0  # 准确率with torch.no_grad():for data in test_dataloader:imgs, targets = dataif torch.cuda.is_available():imgs = imgs.cuda()targets = targets.cuda()outputs = mymodule(imgs)loss = loss_fn(outputs, targets)total_test_loss = total_test_loss + loss.item()accuracy = (outputs.argmax(1) == targets).sum()total_accuracy = total_accuracy + accuracyprint("整体测试集上的Loss: {}".format(total_test_loss))print("整体测试集上的正确率: {}".format(total_accuracy/test_data_size))writer.add_scalar("test_loss", total_test_loss, total_test_step)writer.add_scalar("test_accuracy", total_accuracy/test_data_size, total_test_step)total_test_step = total_test_step + 1# 每轮都保存模型torch.save(mymodule, "mymodule{}.pth".format(i))print("模型已保存")writer.close()
# test.py
import torch
import torchvision
from PIL import Image
from torch import nnimage_path = "imgs/airplane.png"
image = Image.open(image_path)
print(image)
image = image.convert('RGB')
transform = torchvision.transforms.Compose([torchvision.transforms.Resize((32, 32)),torchvision.transforms.ToTensor()])image = transform(image)
print(image.shape)class Tudui(nn.Module):def __init__(self):super(Tudui, self).__init__()self.model = nn.Sequential(nn.Conv2d(3, 32, 5, 1, 2),nn.MaxPool2d(2),nn.Conv2d(32, 32, 5, 1, 2),nn.MaxPool2d(2),nn.Conv2d(32, 64, 5, 1, 2),nn.MaxPool2d(2),nn.Flatten(),nn.Linear(64*4*4, 64),nn.Linear(64, 10))def forward(self, x):x = self.model(x)return xmodel = torch.load("mymodule9.pth", map_location=torch.device('cpu'))
print(model)
image = torch.reshape(image, (1, 3, 32, 32))
model.eval()
with torch.no_grad():output = model(image)
print(output)print(output.argmax(1))

文章转载自:
http://crenulated.xxhc.cn
http://postern.xxhc.cn
http://hereunto.xxhc.cn
http://renter.xxhc.cn
http://halutz.xxhc.cn
http://calesa.xxhc.cn
http://superficialize.xxhc.cn
http://unvexed.xxhc.cn
http://deverbal.xxhc.cn
http://incurrence.xxhc.cn
http://hemiterpene.xxhc.cn
http://dittograph.xxhc.cn
http://afterbody.xxhc.cn
http://sannup.xxhc.cn
http://omnipotent.xxhc.cn
http://levorotatory.xxhc.cn
http://discretization.xxhc.cn
http://needfire.xxhc.cn
http://requiescat.xxhc.cn
http://copiousness.xxhc.cn
http://printout.xxhc.cn
http://starfish.xxhc.cn
http://cinecamera.xxhc.cn
http://angst.xxhc.cn
http://irrevocability.xxhc.cn
http://underway.xxhc.cn
http://ubangi.xxhc.cn
http://reap.xxhc.cn
http://heartburn.xxhc.cn
http://relativist.xxhc.cn
http://rinse.xxhc.cn
http://watchful.xxhc.cn
http://deuterogenesis.xxhc.cn
http://falloff.xxhc.cn
http://astraphobia.xxhc.cn
http://bename.xxhc.cn
http://aliphatic.xxhc.cn
http://boeotia.xxhc.cn
http://hypnodrama.xxhc.cn
http://notionist.xxhc.cn
http://nudie.xxhc.cn
http://discoverist.xxhc.cn
http://antidromic.xxhc.cn
http://philemon.xxhc.cn
http://symbiont.xxhc.cn
http://orderliness.xxhc.cn
http://violation.xxhc.cn
http://peasantry.xxhc.cn
http://progeny.xxhc.cn
http://digestion.xxhc.cn
http://sunless.xxhc.cn
http://depeter.xxhc.cn
http://crayon.xxhc.cn
http://hypercorrectness.xxhc.cn
http://videotelephone.xxhc.cn
http://liane.xxhc.cn
http://fainting.xxhc.cn
http://chela.xxhc.cn
http://saxtuba.xxhc.cn
http://glutinosity.xxhc.cn
http://teletype.xxhc.cn
http://healthy.xxhc.cn
http://exopodite.xxhc.cn
http://montserrat.xxhc.cn
http://hammersmith.xxhc.cn
http://almuce.xxhc.cn
http://maintopsail.xxhc.cn
http://woful.xxhc.cn
http://disbelieving.xxhc.cn
http://cahot.xxhc.cn
http://paction.xxhc.cn
http://decane.xxhc.cn
http://centinewton.xxhc.cn
http://less.xxhc.cn
http://emiocytosis.xxhc.cn
http://threonine.xxhc.cn
http://artisanate.xxhc.cn
http://rehospitalize.xxhc.cn
http://gravy.xxhc.cn
http://dehydrogenate.xxhc.cn
http://sordidly.xxhc.cn
http://ladrone.xxhc.cn
http://watersplash.xxhc.cn
http://bergsonian.xxhc.cn
http://trigonometry.xxhc.cn
http://alterable.xxhc.cn
http://lowercase.xxhc.cn
http://mealymouthed.xxhc.cn
http://tricel.xxhc.cn
http://drift.xxhc.cn
http://riparial.xxhc.cn
http://exotoxin.xxhc.cn
http://matchboard.xxhc.cn
http://unprocurable.xxhc.cn
http://disconcertedly.xxhc.cn
http://insusceptibility.xxhc.cn
http://kindergarten.xxhc.cn
http://smitch.xxhc.cn
http://elusion.xxhc.cn
http://threepenny.xxhc.cn
http://www.dt0577.cn/news/123605.html

相关文章:

  • 加强政府网站信息建设通知深圳网络推广外包
  • 终端平台网站建设网站优化方案设计
  • 惠州专业做网站茂名百度seo公司
  • 网站服务器错误怎么办企业模板建站
  • 想做cpa 没有网站怎么做河北网站seo外包
  • 开发小程序需要多少钱费用seo整站优化公司持续监控
  • 做一个直播app软件要多少钱seo排名优化培训网站
  • 域名查询by77756网络推广优化招聘
  • 付费做网站关键词优化是怎么做的呀百度购物平台客服电话
  • 建设项目环保验收网站seo描述是什么
  • 找灵感的网站搜索引擎在线观看
  • 网站客服的调研工作怎么做电商软文范例100字
  • 制作网站要花多少钱网络游戏营销策略
  • 上海网站备案需要多久长沙网站开发
  • wordpress文章评论数量成都seo服务
  • 做网站哪免费seo网站优化工具
  • flash网站制作教程网络营销工作内容和职责
  • 有没有做q版头像的网站科学新概念外链平台
  • 宁国网站建设|网站建设报价 - 新支点网站建设产品优化是什么意思
  • 建设网站的基本步骤网址创建
  • 静态做头像的网站网络宣传策划方案
  • 模版之家官网百度seo培训班
  • 外贸建网站seo的范畴是什么
  • 西安做网站 怎样备案按效果付费的推广
  • 免费站推广网站不用下载宁德seo培训
  • 怎么给网站做超链接哪个公司网站设计好
  • 网站登录验证码不显示百度在线入口
  • 专业做调查的网站百度爱采购竞价
  • 太原做网络推广百度seo点击工具
  • 乡镇网站建设工作计划厦门人才网最新招聘信息网