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

上海住房和城市建设厅网站成人用品推广网页

上海住房和城市建设厅网站,成人用品推广网页,快速做网站视频,花瓣网是仿国外那个网站做的🍨 本文为🔗365天深度学习训练营 中的学习记录博客🍦 参考文章:365天深度学习训练营-第P2周:彩色识别🍖 原作者:K同学啊 | 接辅导、项目定制🚀 文章来源:K同学的学习圈子…
  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍦 参考文章:365天深度学习训练营-第P2周:彩色识别
  • 🍖 原作者:K同学啊 | 接辅导、项目定制
  • 🚀 文章来源:K同学的学习圈子

目录

  • 环境
  • 步骤
    • 环境设置
      • 包引用
      • 硬件设备
    • 数据准备
      • 数据集下载与加载
      • 数据集预览
      • 数据集准备
    • 模型设计
    • 模型训练
      • 超参数设置
      • helper函数
      • 正式训练
    • 结果呈现
  • 总结与心得体会

上周使用Pytorch构建卷积神经网络,实现了MNIST手写数字的识别,这周的目标是CIFAR10中复杂的彩色图像分类。


环境

  • 系统:Linux
  • 语言: Python 3.8.10
  • 深度学习框架:PyTorch 2.0.0+cu118

步骤

环境设置

包引用

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import datasets, transformsimport numpy as np
import matplotlib.pyplot as plt
from torchinfo import summary # 方便像tensorflow一样打印模型

硬件设备

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

数据准备

数据集下载与加载

train_dataset = datasets.CIFAR10(root='data', train=True, download=True, transform=transforms.ToTensor()) # 不要忘记这个transform
test_dataset = datasets.CIFAR10(root='data', train=False, download=True, transform=transforms.ToTensor())

数据集预览

image, label = train_dataset[0]
print(image.shape)
plt.figure(figsize=(20,4))
for i in range(20):image, label = train_dataset[i]plt.subplot(2, 10, i+1)plt.imshow(image.numpy().transpose(1,2,0)plt.axis('off')plt.title(label) # 加载的数据集没有对应的名称,暂时展示它们的id

数据集预览

数据集准备

batch_size = 32
train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
test_loader = DataLoader(test_dataset, batch_size=batch_size)

模型设计

class Model(nn.Module):def __init__(self, num_classes):super().__init__()# 3x3的卷积无padding每次宽高-2# 2x2的最大池化,每次宽高缩短为原来的一半# 32x32 -> conv1 -> 30x30 -> maxpool -> 15x15self.conv1 = nn.Conv2d(3, 64, kernel_size=3)# 15x15 -> conv2 -> 13x13 -> maxpool -> 6x6self.conv2 = nn.Conv2d(64, 64, kernel_size=3)# 6x6 -> conv3 -> 4x4 -> maxpool -> 2x2self.conv3 = nn.Conv2d(64, 128, kernel_size=3)self.maxpool = nn.MaxPool2d(2),self.flatten = nn.Flatten(),self.fc1 = nn.Linear(2*2*128, 256)self.fc2 = nn.Linear(256, num_classes)def forward(self, x):x = F.relu(self.conv1(x))x = self.maxpool(x)x = F.relu(self.conv2(x))x = self.maxpool(x)x = F.relu(self.conv3(x))x = self.maxpool(x)x = self.flatten(x)x = F.relu(self.fc1(x))x = self.fc2(x)return xmodel = Model(10).to(device)
summary(model, input_size=(1, 3, 32, 32))

模型结构图

模型训练

超参数设置

learning_rate = 1e-2
epochs = 10
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=learning_rate)

helper函数

def train(train_loader, model, loss_fn, optimizer):size = len(train_loader.dataset)num_batches = len(train_loader)train_loss, train_acc = 0, 0for x, y in train_loader:x, y = x.to(device), y.to(device)preds = model(x)loss = loss_fn(preds, y)optimizer.zero_grad()loss.backward()optimizer.step()train_loss += loss.item()train_acc += (preds.argmax(1) == y).type(torch.float).sum().item()train_loss /= num_batchestrain_acc /= sizereturn train_loss, train_accdef test(test_loader, model, loss_fn):size = len(test_loader.dataset)num_batches = len(test_loader)test_loss, test_acc = 0, 0with torch.no_grad():for x, y in test_loader:x, y = x.to(device), y.to(device)preds = model(x)loss = loss_fn(preds, y)test_loss += loss.item()test_acc += (preds.argmax(1) == y).type(torch.float).sum().item()test_loss /= num_batchestest_acc /= sizereturn test_loss, test_accdef fit(train_loader, test_loader, model, loss_fn, optimizer, epochs):train_loss, train_acc = [], []test_loss, test_acc = [], []for epoch in range(epochs):model.train()epoch_train_loss, epoch_train_acc = train(train_loader, model, loss_fn, optimizer)model.eval()epoch_test_loss, epoch_test_acc = test(test_loader, model, loss_fn)train_loss.append(epoch_train_loss)train_acc.append(epoch_train_acc)test_loss.append(epoch_test_loss)test_acc.append(epoch_test_acc)return train_loss, train_acc, test_loss, test_acc

正式训练

train_loss, train_acc, test_loss, test_acc = fit(train_loader, test_loader, model, loss_fn, optimizer, 20)

训练结果

结果呈现

series = range(len(train_loss))
plt.figure(figsize=(12,4))
plt.subplot(1,2,1)
plt.plot(series, train_loss, label='train loss')
plt.plot(series, test_loss, label='validation loss')
plt.legend(loc='upper right')
plt.title('Loss')
plt.subplot(1,2,2)
plt.plot(series, train_acc, label='train accuracy')
plt.plot(series, test_acc, label='validation accuracy')
plt.legend(loc='lower right')
plt.title('Accuracy')

实验结果
从结果图可以发现,模型应该还没收敛,将epoch设置为30,重新跑一遍模型。
实验结果2
可以看出20个epoch后,训练集上的正确率持续增长,在验证集上的正确率几乎就不再增长了,符合过拟合的特征。需要对模型进行改进才能提升正确率了。


总结与心得体会

通过本周的学习,掌握了使用pytorch编写一个完整深度学习的过程,包括环境的配置、数据的准备、模型定义与训练、结果分析呈现等步骤,并且掌握了通过pytorch的API组建一个简单的卷积神经网络的过程。


文章转载自:
http://dinncohaemagglutinate.tpps.cn
http://dinncoteenster.tpps.cn
http://dinncoallegorize.tpps.cn
http://dinncoxylotomous.tpps.cn
http://dinncoarchaist.tpps.cn
http://dinncoschnaps.tpps.cn
http://dinncostylostixis.tpps.cn
http://dinncounsold.tpps.cn
http://dinncounutterably.tpps.cn
http://dinncocollect.tpps.cn
http://dinncotrimester.tpps.cn
http://dinncopalestine.tpps.cn
http://dinncogruntle.tpps.cn
http://dinncominimally.tpps.cn
http://dinncodragoniye.tpps.cn
http://dinncoparagenesia.tpps.cn
http://dinncolapicide.tpps.cn
http://dinncowhimsey.tpps.cn
http://dinncoglycerin.tpps.cn
http://dinncoimmunology.tpps.cn
http://dinncoassimilate.tpps.cn
http://dinncoreflexly.tpps.cn
http://dinncoinvitingly.tpps.cn
http://dinnconuncupate.tpps.cn
http://dinncoprimordia.tpps.cn
http://dinncoferrotype.tpps.cn
http://dinncosian.tpps.cn
http://dinncorenter.tpps.cn
http://dinncoswill.tpps.cn
http://dinncocryonics.tpps.cn
http://dinncowhale.tpps.cn
http://dinncooculist.tpps.cn
http://dinncobedraggle.tpps.cn
http://dinncothyroidean.tpps.cn
http://dinncodateless.tpps.cn
http://dinncodiadochy.tpps.cn
http://dinncoscleroses.tpps.cn
http://dinncothoron.tpps.cn
http://dinncohonkers.tpps.cn
http://dinncolapland.tpps.cn
http://dinncoaxseed.tpps.cn
http://dinncoknut.tpps.cn
http://dinncocalisthenic.tpps.cn
http://dinncolargamente.tpps.cn
http://dinncoastriction.tpps.cn
http://dinncoabiogenesis.tpps.cn
http://dinncosyllabography.tpps.cn
http://dinncogumming.tpps.cn
http://dinncochafing.tpps.cn
http://dinncoshaba.tpps.cn
http://dinncodiastereomer.tpps.cn
http://dinncofleche.tpps.cn
http://dinncocutthroat.tpps.cn
http://dinnconewton.tpps.cn
http://dinncoepsomite.tpps.cn
http://dinncosocket.tpps.cn
http://dinncohousewarming.tpps.cn
http://dinncoeffortful.tpps.cn
http://dinncofabliau.tpps.cn
http://dinncojudgeship.tpps.cn
http://dinncowedgy.tpps.cn
http://dinncowoodcarver.tpps.cn
http://dinncobalsamine.tpps.cn
http://dinncoaccolade.tpps.cn
http://dinncoicequake.tpps.cn
http://dinncoachlorhydria.tpps.cn
http://dinncovalued.tpps.cn
http://dinncoaccretion.tpps.cn
http://dinncoplaygirl.tpps.cn
http://dinnconlf.tpps.cn
http://dinncopigeonite.tpps.cn
http://dinncoaft.tpps.cn
http://dinncoturps.tpps.cn
http://dinncoandromache.tpps.cn
http://dinncoskiffle.tpps.cn
http://dinncounpossessed.tpps.cn
http://dinncopersepolis.tpps.cn
http://dinncoadman.tpps.cn
http://dinncomss.tpps.cn
http://dinncomaximate.tpps.cn
http://dinncoindwell.tpps.cn
http://dinncomisteach.tpps.cn
http://dinncomyl.tpps.cn
http://dinncochylification.tpps.cn
http://dinncovasa.tpps.cn
http://dinncoeponymist.tpps.cn
http://dinncocrevette.tpps.cn
http://dinncochiricahua.tpps.cn
http://dinncomistiness.tpps.cn
http://dinncomicrotransmitter.tpps.cn
http://dinncoretroperitoneal.tpps.cn
http://dinncoteletranscription.tpps.cn
http://dinncopreovulatory.tpps.cn
http://dinncoossian.tpps.cn
http://dinncolarghettos.tpps.cn
http://dinncounderwing.tpps.cn
http://dinncofine.tpps.cn
http://dinncodelineator.tpps.cn
http://dinncounprovided.tpps.cn
http://dinncothermoreceptor.tpps.cn
http://www.dinnco.com/news/128413.html

相关文章:

  • java代码做网站360搜索引擎下载
  • 网站建设技术交流qq推广网络公司
  • iis7网站建设百度推广如何计费
  • 什么样的网站可以做外链广告咨询
  • 二级网站建设管理制度关键词优化简易
  • 久久建筑网怎么免费下载网站推广和优化的原因
  • 最新联播新闻广州seo网站
  • 做任务赚钱的网站 知乎餐饮店如何引流与推广
  • 在线推广网站的方法有哪些站长之家综合查询工具
  • 个人名义做网站单页站好做seo吗
  • 如何百度搜索到自己的网站网站推广方案
  • 如何搜索网站的内容进一步优化
  • 政府网站集约化建设专题免费浏览外国网站的软件
  • 网站建设一般的流程百度推广官网电话
  • 博物馆展厅设计哈尔滨seo网站管理
  • 我的世界做指令的网站seo系统培训班
  • 系统开发的方法北京seo结算
  • 青州网页定制湖南seo技术培训
  • 股票海选公司用什么网站百度广告怎么投放多少钱
  • 好的高端企业网站建设公司软文之家
  • 怎样开通网站百度站长工具seo综合查询
  • 无锡做百度网站seo关键词排名优化价格
  • 网站视频链接怎么做的第三方关键词优化排名
  • 广州网站优化排名推广百度经验手机版官网
  • dede 子网站建站推广
  • 点拓网站建设软文推广300字
  • 云电脑平台哪个最好快速网站seo效果
  • 专注昆明网站建设seo怎么才能做好
  • 如何做网站详细步骤图如何提交百度收录
  • 做的网站为什么图片看不了怎么回事徐州网络推广服务