您的位置:首页 > 游戏 > 手游 > seo广告_广东东莞石碣今天新闻_seo关键词外包公司_seo建设

seo广告_广东东莞石碣今天新闻_seo关键词外包公司_seo建设

2024/10/17 23:09:08 来源:https://blog.csdn.net/qq_56246012/article/details/142567591  浏览:    关键词:seo广告_广东东莞石碣今天新闻_seo关键词外包公司_seo建设
seo广告_广东东莞石碣今天新闻_seo关键词外包公司_seo建设

计算机视觉和图像处理

  1. Tensorflow入门
  2. 深度神经网络
  3. 图像分类
  4. 目标检测
  5. 图像分割
  6. OpenCV
  7. Pytorch
  8. NLP自然语言处理

Pytroch

  • 一、Pytroch部署
  • 二、Pytorch基础语法
    • 2.1 Pytorch的基本元素操作
    • 2.2 Pytorch的基本运算操作
    • 2.3 Torch Tensor和Numpy array之间的相互转换
    • 2.4 Pytorch中的autograd
  • 三、Pytorch初步用法
    • 3.1 构建一个神经网络
    • 3.2 构建一个分类器

一、Pytroch部署

  1. 创建虚拟环境
    在Anaconda终端中创建虚拟环境pytorch
conda create --name pytorch

在这里插入图片描述

  1. 激活虚拟环境
conda activate pytorch

在这里插入图片描述

  1. 安装Pytorch及其相关库
    安装torch、torchvision、torchaudio
pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple

在这里插入图片描述

  1. 查看是否安装成功
import torch
print(torch.__version__)

在这里插入图片描述

二、Pytorch基础语法

2.1 Pytorch的基本元素操作

# 创建一个没有初始化的矩阵
x = torch.empty(5,3)
print(x)

内存中存在脏数据
在这里插入图片描述

# 创建一个有初始化矩阵
x = torch.rand(5,3)
x

从均匀分布(范围通常是 [0, 1))中生成随机数
在这里插入图片描述

x = torch.randn(5,3)
x

均值为0方差为1的矩阵
在这里插入图片描述

torch.randint(0,10,(3,3))

在这里插入图片描述

# 创建⼀个全零矩阵
torch.zeros(5,3)

在这里插入图片描述

# 直接通过数据创建张量
torch.tensor([2.5,3.5])

在这里插入图片描述

# 在已有张量上创建一个新张量
x = x.new_ones(5,3)
print(x)
y = torch.rand_like(x)
print(y)

在这里插入图片描述

# 张量尺寸
x.size()

在这里插入图片描述

# 将x中的值取出
# 如果张量中只有一个元素可以直接用.item()取出
for i in range(x.size(0)):for j in range(x.size(1)):print(x[i][j].item())

在这里插入图片描述

2.2 Pytorch的基本运算操作

# 加法操作
y = torch.rand(5,3)
print(x+y)

在这里插入图片描述

torch.add(x,y)

在这里插入图片描述

result = torch.empty(5,3)
torch.add(x,y,out=result)
result

在这里插入图片描述

# 不赋值给y
y.add(x)

在这里插入图片描述

y

在这里插入图片描述

# 赋值给y
y.add_(x)

在这里插入图片描述

y

在这里插入图片描述

# 支持切片操作
y[:,1]

在这里插入图片描述

# 改变张量形状
x = torch.rand(2,6)
y = x.view(12)
z = x.view(3,4)
# -1表示自动匹配个数
p = x.view(6,-1)
print(x.size(),y.size(),z.size(),p.size())

在这里插入图片描述

2.3 Torch Tensor和Numpy array之间的相互转换

a = torch.ones(5)
a

在这里插入图片描述

# 将Torch tensor转换为Numpy array
b = a.numpy()
b

在这里插入图片描述

# tensor 和 array 共享底层内存空间,一个改变另一个也会改变
a.add_(1)
a,b

在这里插入图片描述

a = np.ones(5)
# # 将Numpy array转换为Torch tensor
b = torch.from_numpy(a)
np.add(a,1,out=a)
a,b

在这里插入图片描述

2.4 Pytorch中的autograd

# requires_grad=True计算梯度
x = torch.ones(2,2,requires_grad=True)
x

在这里插入图片描述

# 自动继承requires_grad=True这个属性
y = x + 2
y

在这里插入图片描述

# grad_fn记录了张量是如何通过各种操作生成的。
# None因为x是手动创建的,AddBackward0表示y是通过加法操作创建的
x.grad_fn,y.grad_fn

在这里插入图片描述

z = y * y * 3
out = z.mean()
z,out

在这里插入图片描述

a = torch.rand(2,2)
a = (a * a) / (a - 1)
print(a.requires_grad)

在这里插入图片描述

a.requires_grad_(True)
print(a.requires_grad)

在这里插入图片描述

b = (a * a).sum()
b.grad_fn

在这里插入图片描述

# 关于梯度Gradients
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
y = x * x
out = y.sum()# 反向传播
out.backward()print("x.grad:", x.grad)

在这里插入图片描述

x = torch.rand(2,2,requires_grad=True)
x

在这里插入图片描述

# 获得一个新的Tensor,跟x拥有相同的内容但不需要自动求导
y = x.detach()
y.requires_grad

在这里插入图片描述

# 查看x和y元素是否相同
x.eq(y)

在这里插入图片描述

x.eq(y).all()

在这里插入图片描述

三、Pytorch初步用法

3.1 构建一个神经网络

import torch
import torch.nn as nn
import torch.nn.functional as F
# 定义一个简单的网络类
class Net(nn.Module):def __init__(self):super(Net,self).__init__()# 卷积self.conv1 = nn.Conv2d(1,6,3)self.conv2 = nn.Conv2d(6,16,3)# 全连接self.fc1 = nn.Linear(16*6*6,120)self.fc2 = nn.Linear(120,84)self.fc3 = nn.Linear(84,10)def forward(self,x):x = self.conv1(x)x = F.relu(x)x = F.max_pool2d(x,(2,2))x = F.max_pool2d(F.relu(self.conv2(x)),(2,2))print(f"Conv1 output shape: {x.shape}")x = x.view(-1,self.num_flat_features(x))x = F.relu(self.fc1(x))x = F.relu(self.fc2(x))# 输出层x = self.fc3(x)return x def num_flat_features(self,x):shapes = x.size()[1:]features =1for shape in shapes:features *= shapereturn featuresnet = Net()
print(net)

在这里插入图片描述

net = Net()
input = torch.randn(32, 1, 32, 32)
out = net(input)
out

在这里插入图片描述

# 查看模型中的可训练参数
params = list(net.parameters())
params

在这里插入图片描述

params[0].size(),params[1].size(),params[2].size()

在这里插入图片描述

import torch.optim as optim
input = torch.randn(1, 1, 32, 32)
out = net(input)
target = torch.randn(10)
target = target.view(1,-1)criterion = nn.MSELoss()
optimizer = optim.SGD(net.parameters(), lr=0.01)loss = criterion(out,target)
# 梯度清零
optimizer.zero_grad()
# 反向传播
loss.backward() 
# 更新权重
optimizer.step()  
loss

在这里插入图片描述

# 第二次前向传播和反向传播
out = net(input)
loss = criterion(out,target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss

在这里插入图片描述

3.2 构建一个分类器

import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
 # 数据转换器,将PIL图像转换为PyTorch张量 并进行归一化
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])
# 加载训练集
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,download=True, transform=transform)
# 加载训练数据加载器
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,shuffle=False, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat','deer', 'dog', 'frog', 'horse', 'ship', 'truck')
def imgshow(img):# 反归一化img = img / 2 + 0.5  # 假设图像数据已经被归一化到 [-1, 1]img_np = img.numpy()plt.figure(figsize=(7, 7))plt.imshow(np.transpose(img_np, (1, 2, 0)))plt.show()# 获取一批数据
dataiter = iter(trainloader)
images, labels = next(dataiter)# 显示图像
# imgshow(torchvision.utils.make_grid(images))# 打印标签
print('GroundTruth:',' '.join('%5s' % classes[labels[j]] for j in range(4)))

在这里插入图片描述

# 定义卷积神经网络
class Net(nn.Module):def __init__(self):super(Net, self).__init__()self.conv1 = nn.Conv2d(3, 6, 5)self.pool = nn.MaxPool2d(2, 2)self.conv2 = nn.Conv2d(6, 16, 5)self.fc1 = nn.Linear(16 * 5 * 5, 120)self.fc2 = nn.Linear(120, 84)self.fc3 = nn.Linear(84, 10)def forward(self, x):x = self.pool(F.relu(self.conv1(x)))x = self.pool(F.relu(self.conv2(x)))x = x.view(-1, 16 * 5 * 5)x = F.relu(self.fc1(x))x = F.relu(self.fc2(x))x = self.fc3(x)return xnet = Net()
import torch.optim as optim# 定义损失函数
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(),lr=0.001,momentum=0.9)# 定义学习率调度器
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)# 训练模型
for epoch in range(2):running_loss = 0.0for i,data in enumerate(trainloader,0):input,label = dataoptimizer.zero_grad()output = net(input)loss = criterion(output,labels)loss.backward()optimizer.step()# 累加损失值running_loss += loss.item()# 每 2000 个批次打印一次损失值if (i + 1) % 2000 == 0:print(f'[{epoch + 1}, {i + 1}] loss: {running_loss / 2000:.3f}')running_loss = 0.0   print('Finished Training')

在这里插入图片描述

# 获取一组测试数据
dataiter = iter(testloader)
images, labels = next(dataiter)# 打印原始图⽚
# imgshow(torchvision.utils.make_grid(images))# 打印真实的标签
print('GroundTruth: ',' '.join('%5s' % classes[labels[j]] for j in range(4)))

在这里插入图片描述

# ⾸先实例化模型的类对象
net = Net()
# 利⽤模型对图⽚进⾏预测
outputs = net(images)
# 共有10个类别, 采⽤模型计算出的概率最⼤的作为预测的类别
_, predicted = torch.max(outputs, 1)
# 打印预测标签的结果
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4)))

预测不准确
在这里插入图片描述

correct = 0
total = 0
# 禁用梯度计算
with torch.no_grad():for data in testloader:images,labels = dataoutputs = net(images)_, predicted = torch.max(outputs, 1)total += labels.size(0)correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))

在这里插入图片描述

class_correct = list(0.0 for i in range(10))
class_total = list(0.0 for i in range(10))
with torch.no_grad():for data in testloader:images, labels = dataoutputs = net(images)_, predicted = torch.max(outputs, 1)# squeeze() 确保 c 是一个一维张量,便于后续的逐元素操作c = (predicted == labels).squeeze()for i in range(4):label = labels[i]class_correct[label] += int(c[i].item())class_total[label] += 1
for i in range(10):print('Accuracy of %5s : %2d %%' % (classes[i], 100 * class_correct[i] / class_total[i]))

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com