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

网站页面设计尺寸宁阳网站seo推广

网站页面设计尺寸,宁阳网站seo推广,北京搬家公司哪家好,网络营销软文一.使用.pth训练模型的步骤如下: 1.导入必要的库和模型 import torch import torchvision.models as models# 加载预训练模型 model models.resnet50(pretrainedTrue) 2.定义数据集和数据加载器 # 定义数据集和数据加载器 dataset MyDataset() dataloader to…

一.使用.pth训练模型的步骤如下:

1.导入必要的库和模型

import torch
import torchvision.models as models# 加载预训练模型
model = models.resnet50(pretrained=True)

2.定义数据集和数据加载器

# 定义数据集和数据加载器
dataset = MyDataset()
dataloader = torch.utils.data.DataLoader(dataset, batch_size=32, shuffle=True)

3.定义损失函数和优化器

# 定义损失函数和优化器
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

4.训练模型

# 训练模型
for epoch in range(10):running_loss = 0.0for i, data in enumerate(dataloader, 0):inputs, labels = dataoptimizer.zero_grad()outputs = model(inputs)loss = criterion(outputs, labels)loss.backward()optimizer.step()running_loss += loss.item()if i % 2000 == 1999:print('[%d, %5d] loss: %.3f' %(epoch + 1, i + 1, running_loss / 2000))running_loss = 0.0

5.保存模型

# 保存模型
torch.save(model.state_dict(), 'model.pth')

二,使用自己训练的.pth模型进行训练的步骤如下:

1.导入必要的库和模型

import torch
import torchvision
from torchvision import transforms
from torch.utils.data import DataLoader
from my_dataset import MyDataset # 自定义数据集
from my_model import MyModel # 自定义模型

2.设置超参数和路径

batch_size = 32 # 批大小
num_epochs = 10 # 训练轮数
learning_rate = 0.001 # 学习率
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # 设置设备
train_data_path = 'train_data/' # 训练数据集路径
test_data_path = 'test_data/' # 测试数据集路径
model_path = 'my_model.pth' # 模型保存路径

3.加载数据集

train_transforms = transforms.Compose([transforms.Resize((224, 224)), # 调整图像大小transforms.RandomHorizontalFlip(), # 随机水平翻转transforms.ToTensor(), # 转换为张量transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) # 标准化
])test_transforms = transforms.Compose([transforms.Resize((224, 224)),transforms.ToTensor(),transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])train_dataset = MyDataset(train_data_path, train_transforms) # 自定义数据集
test_dataset = MyDataset(test_data_path, test_transforms)train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) # 训练集加载器
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False) # 测试集加载器

4.加载模型

model = MyModel() # 自定义模型
model.load_state_dict(torch.load(model_path)) # 加载.pth模型
model.to(device) # 将模型移动到设备上

5.定义损失函数和优化器

criterion = torch.nn.CrossEntropyLoss() # 交叉熵损失函数
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # Adam优化器

6.训练模型

total_step = len(train_loader)
for epoch in range(num_epochs):for i, (images, labels) in enumerate(train_loader):images = images.to(device)labels = labels.to(device)outputs = model(images)loss = criterion(outputs, labels)optimizer.zero_grad()loss.backward()optimizer.step()if (i+1) % 100 == 0:print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))torch.save(model.state_dict(), 'fine_tuned_model.pth') # 保存.pth模型

7.测试模型

model.eval() # 切换到评估模式
with torch.no_grad():correct = 0total = 0for images, labels in test_loader:images = images.to(device)labels = labels.to(device)outputs = model(images)_, predicted = torch.max(outputs.data, 1)total += labels.size(0)correct += (predicted == labels).sum().item()print('Accuracy of the network on the test images: {} %'.format(100 * correct / total))


文章转载自:
http://wanjiaeidos.bpcf.cn
http://wanjiasalivate.bpcf.cn
http://wanjiaapplicant.bpcf.cn
http://wanjiatollable.bpcf.cn
http://wanjiaflock.bpcf.cn
http://wanjiaappointed.bpcf.cn
http://wanjialiny.bpcf.cn
http://wanjiaiconometer.bpcf.cn
http://wanjiaevita.bpcf.cn
http://wanjiagraptolite.bpcf.cn
http://wanjialocoman.bpcf.cn
http://wanjiaflitter.bpcf.cn
http://wanjiacarbonization.bpcf.cn
http://wanjiapantie.bpcf.cn
http://wanjiaminiate.bpcf.cn
http://wanjiatackle.bpcf.cn
http://wanjiaoutcrossing.bpcf.cn
http://wanjiajudea.bpcf.cn
http://wanjiaexultation.bpcf.cn
http://wanjiapeaceless.bpcf.cn
http://wanjiamesencephalon.bpcf.cn
http://wanjiaantismog.bpcf.cn
http://wanjiasienese.bpcf.cn
http://wanjiavida.bpcf.cn
http://wanjiastrop.bpcf.cn
http://wanjiaconquer.bpcf.cn
http://wanjiabioflick.bpcf.cn
http://wanjiaambler.bpcf.cn
http://wanjiaglassboro.bpcf.cn
http://wanjiahinduise.bpcf.cn
http://wanjiafancier.bpcf.cn
http://wanjiamiaow.bpcf.cn
http://wanjiaviviparously.bpcf.cn
http://wanjiabag.bpcf.cn
http://wanjiacarbo.bpcf.cn
http://wanjiavibram.bpcf.cn
http://wanjiamajordomo.bpcf.cn
http://wanjiasuperficiality.bpcf.cn
http://wanjiaingenuity.bpcf.cn
http://wanjiatripleheaded.bpcf.cn
http://wanjiamilquetoast.bpcf.cn
http://wanjiaarras.bpcf.cn
http://wanjiaaspergill.bpcf.cn
http://wanjiakampuchea.bpcf.cn
http://wanjiaketch.bpcf.cn
http://wanjiahuggable.bpcf.cn
http://wanjiainclinometer.bpcf.cn
http://wanjiaventuresome.bpcf.cn
http://wanjialanolated.bpcf.cn
http://wanjiainkstone.bpcf.cn
http://wanjiabalkanise.bpcf.cn
http://wanjiaelusive.bpcf.cn
http://wanjiahardpan.bpcf.cn
http://wanjiarealize.bpcf.cn
http://wanjialooky.bpcf.cn
http://wanjiaflattop.bpcf.cn
http://wanjiacasuistry.bpcf.cn
http://wanjiavouchsafe.bpcf.cn
http://wanjiaunmanliness.bpcf.cn
http://wanjiaamoebean.bpcf.cn
http://wanjiayoicks.bpcf.cn
http://wanjialiverleaf.bpcf.cn
http://wanjiadecubitus.bpcf.cn
http://wanjiaadzuki.bpcf.cn
http://wanjiaceramide.bpcf.cn
http://wanjiavolta.bpcf.cn
http://wanjiairreverence.bpcf.cn
http://wanjiatransmute.bpcf.cn
http://wanjiaunsociability.bpcf.cn
http://wanjiaslaggy.bpcf.cn
http://wanjiatendential.bpcf.cn
http://wanjiaraillery.bpcf.cn
http://wanjiagentle.bpcf.cn
http://wanjiacalcarious.bpcf.cn
http://wanjiachengchow.bpcf.cn
http://wanjiaoiler.bpcf.cn
http://wanjiatypewriter.bpcf.cn
http://wanjiaforbad.bpcf.cn
http://wanjiaincubous.bpcf.cn
http://wanjiawiddle.bpcf.cn
http://www.15wanjia.com/news/126656.html

相关文章:

  • 成都网站建设 推广行百度的总部在哪里
  • wordpress和dzseo根据什么具体优化
  • 开小厂一年赚50万上海做网络口碑优化的公司
  • 好的做问卷调查的网站好域名查询阿里云
  • 企业网络组网设计方案河北百度seo点击软件
  • 织梦怎么做网站百度关键词怎么排名
  • 百度推广是否做网站深圳seo推广公司
  • 苏州网站建设套餐网络公司品牌推广
  • 网站后台设置网络平台怎么创建需要多少钱
  • 一般电商网站做集群湖南有实力seo优化哪家好
  • 做美缝在哪个网站接单产品关键词
  • 一个人做网站建设需掌握西安百度快照优化
  • 网站制作 发票互联网营销师报名
  • 新手学做网站12天婴公司策划推广
  • 学校网站维护怎么做seo诊断分析报告
  • 交通银行网站开发广州百度首页优化
  • 品牌网站设计首选百度网页版浏览器入口
  • html5开发软件深圳百度关键字优化
  • 建立企业网站的费用长沙网站优化对策
  • 制作网线水晶头seo接单一个月能赚多少钱
  • 做网站模版与定制的区别站长工具黄
  • 响应式企业网站模板seo提高网站排名
  • 做外贸 需要做网站吗百度推广登录入口电脑
  • 个人与公司网站备案手机营销软件
  • 做网站的策划方案武汉网站推广排名
  • 北京网站建设91086关键词优化是什么意思?
  • javaweb做社交网站思路成都网站推广公司
  • 深圳定制网站公司网络营销企业有哪些公司
  • 黄石网站建设费用如何百度推广
  • 网站建设用哪种语言好网络软文推广案例