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

wordpress免费主题网站搜索引擎优化网站

wordpress免费主题网站,搜索引擎优化网站,中信建设有限责任公司标志,网站开发 定义py 版本:Python 3.12.7 安装库: pip install numpy torch torchvision matplotlib 运行: python test.py py版本不对可能无法运行,默认数据集需要科学上网才能下载,默认的验证代码是从验证包里取图片,注释的代码是我…

py 版本:Python 3.12.7

安装库: pip install numpy torch torchvision matplotlib

运行: python test.py

py版本不对可能无法运行,默认数据集需要科学上网才能下载,默认的验证代码是从验证包里取图片,注释的代码是我本地构造的图片,用库里的图片,替换纯色背景,手写一个数字,大小改到28x28就可以验证了

import torch
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.datasets import MNIST
import matplotlib.pyplot as plt
from PIL import Imageclass Net(torch.nn.Module):def __init__(self):super().__init__()self.fc1 = torch.nn.Linear(28*28, 64)self.fc2 = torch.nn.Linear(64, 64)self.fc3 = torch.nn.Linear(64, 64)self.fc4 = torch.nn.Linear(64, 10)def forward(self, x):x = torch.nn.functional.relu(self.fc1(x))x = torch.nn.functional.relu(self.fc2(x))x = torch.nn.functional.relu(self.fc3(x))x = torch.nn.functional.log_softmax(self.fc4(x), dim=1)return xdef get_data_loader(is_train):to_tensor = transforms.Compose([transforms.ToTensor()])data_set = MNIST("", is_train, transform=to_tensor, download=True)return DataLoader(data_set, batch_size=15, shuffle=True)def evaluate(test_data, net):n_correct = 0n_total = 0with torch.no_grad():for (x, y) in test_data:outputs = net.forward(x.view(-1, 28*28))for i, output in enumerate(outputs):if torch.argmax(output) == y[i]:n_correct += 1n_total += 1return n_correct / n_totaldef main():train_data = get_data_loader(is_train=True)test_data = get_data_loader(is_train=False)net = Net()print("initial accuracy:", evaluate(test_data, net))optimizer = torch.optim.Adam(net.parameters(), lr=0.001)for epoch in range(2):for (x, y) in train_data:net.zero_grad()output = net.forward(x.view(-1, 28*28))loss = torch.nn.functional.nll_loss(output, y)loss.backward()optimizer.step()print("epoch", epoch, "accuracy:", evaluate(test_data, net))for (n, (x, _)) in enumerate(test_data):if n > 3:breakpredict = torch.argmax(net.forward(x[0].view(-1, 28*28)))plt.figure(n)plt.imshow(x[0].view(28, 28))plt.title("prediction: " + str(int(predict)))plt.show()def load_custom_image(image_path):""" 加载自定义手写数字图片,并转换为 MNIST 兼容格式 """transform = transforms.Compose([transforms.Grayscale(num_output_channels=1),  # 转换为灰度图transforms.Resize((28, 28)),  # 调整大小transforms.ToTensor(),  # 转换为 PyTorch 张量transforms.Normalize((0.1307,), (0.3081,))  # 使用 MNIST 的归一化参数])image = Image.open(image_path)return transform(image).unsqueeze(0)  # 添加 batch 维度if __name__ == "__main__":main()"""train_data = get_data_loader(is_train=True)test_data = get_data_loader(is_train=False)net = Net()print("initial accuracy:", evaluate(test_data, net))optimizer = torch.optim.Adam(net.parameters(), lr=0.001)for epoch in range(2):for (x, y) in train_data:net.zero_grad()output = net.forward(x.view(-1, 28*28))loss = torch.nn.functional.nll_loss(output, y)loss.backward()optimizer.step()print("epoch", epoch, "accuracy:", evaluate(test_data, net))image_tensor = load_custom_image("C:\\Users\\we\\Desktop\\7.png")predict = torch.argmax(net.forward(image_tensor.view(-1, 28*28)))print("prediction: " + str(int(predict)))"""

默认代码验证结果

手写图片

验证结果

来源:【10分钟入门神经网络 PyTorch 手写数字识别】 https://www.bilibili.com/video/BV1GC4y15736/?share_source=copy_web&vd_source=33a387ed337161d7e4f60dd9167ab954

http://www.dtcms.com/wzjs/227688.html

相关文章:

  • 做装修效果图的网站有哪些软件下载seo公司哪家好用
  • 望京网站建设有什么平台可以发布推广信息
  • 郑州企业网站制作公司百度云登录入口
  • 江西网站设计哪家强哪个行业最需要推广
  • 网站建设的申请理由搜索引擎有哪些网站
  • 做论坛网站 备案站长友情链接平台
  • 云搜索一点优化
  • 开发小程序软件的公司长沙seo优化首选
  • 个人备案网站2345网址导航中国最好
  • 北京网络关键词排名优化易下拉霸屏
  • 企业介绍微网站怎么做的谷歌网站优化推广
  • 怎么新建自己的网站软文素材网
  • 动态网站设计栏目培训心得体会2000字
  • 中冶东北建设最新网站什么叫seo优化
  • 新疆机票网站制作windows优化大师卸载
  • 商城网站模板 免费西安网站推广
  • 手机公司网站建设推广渠道有哪些
  • 网站自己做推广网络推广网络营销软件
  • 餐饮类网站模板百度seo排名查询
  • 出口做谷歌网站怎么制作网站教程手机
  • 做网站和web前端一样吗武汉百度快照优化排名
  • wordpress区分移动站怎样免费制作网页
  • 做网站的IDE竞价托管哪家专业
  • qq群优惠券里面网站怎么做的福州整站优化
  • 安全员c证电子证书查询搜索引擎优化教程
  • 网站建设项目可行性报告如何优化网页加载速度
  • 商丘网站建设推广公司如何编写一个网站
  • 广州建设时时彩网站发稿平台
  • 网站建设开源项目github营销型网站
  • 做网站还是做阿里如何做电商新手入门