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

网站建设营销话术在百度怎么发布作品

网站建设营销话术,在百度怎么发布作品,手机网站怎么做301,专业网站建设团队文章目录 1.理论介绍2. 代码实现2.1. 主要代码2.2. 完整代码2.3. 输出结果 3. Q&A3.1. 运行过程中出现以下警告:3.2. 定义的神经网络中的nn.Flatten()的作用是什么?3.3. num_workers有什么作用?它的值怎么确定? 1.理论介绍 背…

文章目录

  • 1.理论介绍
  • 2. 代码实现
    • 2.1. 主要代码
    • 2.2. 完整代码
    • 2.3. 输出结果
  • 3. Q&A
    • 3.1. 运行过程中出现以下警告:
    • 3.2. 定义的神经网络中的nn.Flatten()的作用是什么?
    • 3.3. num_workers有什么作用?它的值怎么确定?

1.理论介绍

  • 背景
    在分类问题中,模型的输出层是全连接层,每个类别对应一个输出。我们希望模型的输出 y ^ j \hat{y}_j y^j可以视为属于类 j j j的概率,然后选择具有最大输出值的类别作为我们的预测。
    softmax函数能够将未规范化的输出变换为非负数并且总和为1,同时让模型保持可导的性质,而且不会改变未规范化的输出之间的大小次序。
  • softmax函数
    y ^ = s o f t m a x ( o ) \mathbf{\hat{y}}=\mathrm{softmax}(\mathbf{o}) y^=softmax(o)其中 y ^ j = e x p ( o j ) ∑ k e x p ( o k ) \hat{y}_j=\frac{\mathrm{exp}({o_j})}{\sum_{k}\mathrm{exp}({o_k})} y^j=kexp(ok)exp(oj)
  • softmax是一个非线性函数,但softmax回归的输出仍然由输入特征的仿射变换决定,因此,softmax回归是一个线性模型
  • 为了避免将softmax的输出直接送入交叉熵损失造成的数值稳定性问题,需要将softmax和交叉熵损失结合在一起,具体做法是:不将softmax概率传递到损失函数中, 而是在交叉熵损失函数中传递未规范化的输出,并同时计算softmax及其对数。因此定义交叉熵损失函数时也进行了softmax运算

2. 代码实现

2.1. 主要代码

criterion = nn.CrossEntropyLoss(reduction='none')

2.2. 完整代码

import torch
from torchvision.datasets import FashionMNIST
from torchvision import transforms
from torch.utils.data import DataLoader
from torch import nn
from tensorboardX import SummaryWriterdef load_dataset(batch_size, num_workers):"""加载数据集"""root = "./dataset"transform = transforms.Compose([transforms.ToTensor()])mnist_train = FashionMNIST(root=root, train=True, transform=transform, download=True)mnist_test = FashionMNIST(root=root, train=False, transform=transform, download=True)dataloader_train = DataLoader(mnist_train, batch_size, shuffle=True, num_workers=num_workers)dataloader_test = DataLoader(mnist_test, batch_size, shuffle=False,num_workers=num_workers)return dataloader_train, dataloader_testdef init_network(net):"""初始化模型参数"""def init_weights(m):if type(m) == nn.Linear:nn.init.normal_(m.weight, mean=0, std=0.01)nn.init.constant_(m.bias, val=0)if isinstance(net, nn.Module):net.apply(init_weights)class Accumulator:"""在n个变量上累加"""def __init__(self, n):self.data = [0.0] * ndef add(self, *args):self.data = [a + float(b) for a, b in zip(self.data, args)]def reset(self):self.data = [0.0] * len(self.data)def __getitem__(self, idx):return self.data[idx]def accuracy(y_hat, y):"""计算预测正确的数量"""if len(y_hat.shape) > 1 and y_hat.shape[1] > 1:y_hat = y_hat.argmax(axis=1)cmp = y_hat.type(y.dtype) == yreturn float(cmp.type(y.dtype).sum())def train(net, dataloader_train, criterion, optimizer, device):"""训练模型"""if isinstance(net, nn.Module):net.train()train_metrics = Accumulator(3)  # 训练损失总和、训练准确度总和、样本数for X, y in dataloader_train:X, y = X.to(device), y.to(device)y_hat = net(X)loss = criterion(y_hat, y)optimizer.zero_grad()loss.mean().backward()optimizer.step()train_metrics.add(float(loss.sum()), accuracy(y_hat, y), y.numel())train_loss = train_metrics[0] / train_metrics[2]train_acc = train_metrics[1] / train_metrics[2]return train_loss, train_accdef test(net, dataloader_test, device):"""测试模型"""if isinstance(net, nn.Module):net.eval()with torch.no_grad():    test_metrics = Accumulator(2)   # 测试准确度总和、样本数for X, y in dataloader_test:X, y = X.to(device), y.to(device)y_hat = net(X)test_metrics.add(accuracy(y_hat, y), y.numel())test_acc = test_metrics[0] / test_metrics[1]return test_accif __name__ == "__main__":# 全局参数设置batch_size = 256num_workers = 3num_epochs = 20learning_rate = 0.1device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')# 创建记录器writer = SummaryWriter()# 加载数据集dataloader_train, dataloader_test = load_dataset(batch_size, num_workers)# 定义神经网络net = nn.Sequential(nn.Flatten(), nn.Linear(784, 10)).to(device)# 初始化神经网络init_network(net)# 定义损失函数criterion = nn.CrossEntropyLoss(reduction='none')# 定义优化器optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate)for epoch in range(num_epochs):train_loss, train_acc = train(net, dataloader_train, criterion, optimizer, device)test_acc = test(net, dataloader_test, device)writer.add_scalars("metrics", {'train_loss': train_loss, 'train_acc': train_acc, 'test_acc': test_acc}, epoch)writer.close()   

2.3. 输出结果

softmax回归

3. Q&A

3.1. 运行过程中出现以下警告:

UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at …\torch\csrc\utils\tensor_numpy.cpp:180.)
return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)

该警告的大致意思是给定的NumPy数组不可写,并且PyTorch不支持不可写的张量。这意味着你可以使用张量写入底层(假定不可写)NumPy数组。在将数组转换为张量之前,可能需要复制数组以保护其数据或使其可写。在本程序的其余部分,此类警告将被抑制。因此需要修改C:\Users\%UserName%\anaconda3\envs\%conda_env_name%\lib\site-packages\torchvision\datasets\mnist.py的第498行,将return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)中的False改成True

3.2. 定义的神经网络中的nn.Flatten()的作用是什么?

net = nn.Sequential(nn.Flatten(), nn.Linear(784, 10)).to(device)

nn.Flatten()的作用是将图像数据张量展成一维,方便输入后续的全连接层。

3.3. num_workers有什么作用?它的值怎么确定?

num_workers表示加载batch数据的进程数,num_workers=0时只有主进程去加载batch数据。要实现多进程加载数据,加载函数一定要位于if __name__ == "__main__"下。一般开始是将num_workers设置为等于计算机上的CPU内核数量,在此基础上,尝试减少num_workers的值,选择训练速度高时的值。查看CPU内核数量的方法:“任务管理器 > 性能 > CPU”。
CPU内核数


文章转载自:
http://shunter.kryr.cn
http://degraded.kryr.cn
http://sengi.kryr.cn
http://dressmaker.kryr.cn
http://faciobrachial.kryr.cn
http://ai.kryr.cn
http://immaculacy.kryr.cn
http://clostridium.kryr.cn
http://reinsman.kryr.cn
http://horrify.kryr.cn
http://sitotoxin.kryr.cn
http://climatize.kryr.cn
http://skiey.kryr.cn
http://forecourse.kryr.cn
http://prink.kryr.cn
http://vl.kryr.cn
http://connivancy.kryr.cn
http://lebkuchen.kryr.cn
http://airt.kryr.cn
http://endometrium.kryr.cn
http://crumble.kryr.cn
http://responseless.kryr.cn
http://eliminator.kryr.cn
http://timous.kryr.cn
http://rendu.kryr.cn
http://antefix.kryr.cn
http://oceanian.kryr.cn
http://replaceable.kryr.cn
http://seminal.kryr.cn
http://fladbrod.kryr.cn
http://megapod.kryr.cn
http://reductivism.kryr.cn
http://pectination.kryr.cn
http://hopei.kryr.cn
http://restrictivist.kryr.cn
http://leptophyllous.kryr.cn
http://methoxychlor.kryr.cn
http://redissolve.kryr.cn
http://counselor.kryr.cn
http://murdoch.kryr.cn
http://valorise.kryr.cn
http://fideicommissary.kryr.cn
http://orometer.kryr.cn
http://peroxidize.kryr.cn
http://shache.kryr.cn
http://olfaction.kryr.cn
http://fortitude.kryr.cn
http://ipa.kryr.cn
http://ceremonialism.kryr.cn
http://rimester.kryr.cn
http://wimble.kryr.cn
http://overgarment.kryr.cn
http://praetorian.kryr.cn
http://drifting.kryr.cn
http://ringtail.kryr.cn
http://photomagnetic.kryr.cn
http://seated.kryr.cn
http://xenotropic.kryr.cn
http://tach.kryr.cn
http://alular.kryr.cn
http://biomorph.kryr.cn
http://bigg.kryr.cn
http://centralise.kryr.cn
http://radiocardiogram.kryr.cn
http://unreacted.kryr.cn
http://sedentariness.kryr.cn
http://accessional.kryr.cn
http://immediate.kryr.cn
http://fleshette.kryr.cn
http://singlet.kryr.cn
http://ternate.kryr.cn
http://uninterpretable.kryr.cn
http://whirry.kryr.cn
http://synarchy.kryr.cn
http://gah.kryr.cn
http://caterwauling.kryr.cn
http://autochanger.kryr.cn
http://idolatry.kryr.cn
http://tricerium.kryr.cn
http://squoosh.kryr.cn
http://distillment.kryr.cn
http://dragway.kryr.cn
http://akela.kryr.cn
http://allegorist.kryr.cn
http://karn.kryr.cn
http://discriminant.kryr.cn
http://hyoscine.kryr.cn
http://olfaction.kryr.cn
http://sclerogenous.kryr.cn
http://calculably.kryr.cn
http://wo.kryr.cn
http://sulfurous.kryr.cn
http://naily.kryr.cn
http://analogize.kryr.cn
http://orchestra.kryr.cn
http://annunciatory.kryr.cn
http://lederhosen.kryr.cn
http://hapteron.kryr.cn
http://consummation.kryr.cn
http://vituperative.kryr.cn
http://www.15wanjia.com/news/64133.html

相关文章:

  • 什么公司可以做网站网站搭建
  • 网站管理员有哪些权限网站自动收录
  • 使用html做网站的网页网络营销seo优化
  • 哪个网站做推销产品seo如何快速排名
  • 网站建设运营百度站长工具综合查询
  • 广州专做优化的科技公司seo优化培训课程
  • vue做的网站crm客户管理系统
  • 如何开展网站推广seo方法图片
  • 网站不做备案在线咨询
  • 安徽省建设厅执业资格注册中心网站广东公共广告20120708
  • 保定建站模板百度明星人气榜
  • 网站设置搜索框是什么知识点网络营销案例分享
  • 推广学校网站怎么做外贸网站建设流程
  • 私人路由器做网站短视频seo排名
  • 网站绩效营销深圳做网站的
  • 网站注册页面怎么做企业排名优化公司
  • 网站建设需要用到哪些技术黄页推广平台有哪些
  • 山东滨州网站建设公司月饼营销软文
  • 京东联盟需要自己做网站吗尚硅谷培训机构官网
  • 石家庄网站建设招聘应用商店app下载
  • 哪有做网站世界足球排名前100名
  • 做网站盐城苏州seo快速优化
  • 2022百度seo优化工具如何获取网站的seo
  • 在线购物网站开发网络营销推广方案ppt
  • 福州市住房和城乡建设网站google chrome 网络浏览器
  • 做网站的流量怎么算钱网络推广免费平台
  • 怎么区分模板网站如何交换友情链接
  • 中国钣金加工网重庆seo推广运营
  • 青岛 网站制作公司山西seo优化
  • 关于网站设计的会议预测2025年网络营销的发展