Pytorch 学习记录


(注:编辑器会根据文章标题自动生成目录)

pytorch笔记

Pytorch网络以及组件

pytorch网络流程

import torch
import torchvision
import torchvision.transforms as transforms
import torch.utils.data.dataloader as dataloader
import torch.nn as nn
import torch.optim as optim
import os


train_set = torchvision.datasets.MNIST(
    root="./data",
    train=True,
    transform=transforms.ToTensor(),
    download=True
)
train_loader = dataloader.DataLoader(
    dataset=train_set,
    batch_size=100,
    shuffle=False,
)

test_set = torchvision.datasets.MNIST(
    root="./data",
    train=False,
    transform=transforms.ToTensor(),
    download=True
)
test_loader = dataloader.DataLoader(
    dataset=test_set,
    batch_size=100,
    shuffle=False,
)

class NeuralNet(nn.Module):

    def __init__(self, input_num, hidden_num, output_num):
        super(NeuralNet, self).__init__()
        self.fc1 = nn.Linear(input_num, hidden_num)
        self.fc2 = nn.Linear(hidden_num, output_num)
        self.relu = nn.ReLU()

    def forward(self,x):
        x = self.fc1(x)
        x = self.relu(x)
        y = self.fc2(x)
        return y


epoches = 20
lr = 0.001
input_num = 784
hidden_num = 500
output_num = 10
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model = NeuralNet(input_num, hidden_num, output_num)
model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=lr)


for epoch in range(epoches):
    for i, data in enumerate(train_loader):
        (images, labels) = data
        images = images.reshape(-1, 28*28).to(device)
        labels = labels.to(device)

        output = model(images)
        loss = criterion(output, labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if (i+1) % 100 == 0:
            print('Epoch [{}/{}], Loss: {:.4f}'
                  .format(epoch + 1, epoches, loss.item()))


with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = images.reshape(-1, 28*28).to(device)
        labels = labels.to(device)
        output = model(images)
        _, predicted = torch.max(output, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

    print("The accuracy of total {} images: {}%".format(total, 100 * correct/total))

transform

transform=transform.compose([ transform.xxxxxxx=xxxxx])
	Resize:把给定的图片resize到given size
	Normalize:Normalized an tensor image with mean and standard deviation
	ToTensor:convert a PIL image to tensor (H*W*C) in range [0,255] to a torch.Tensor(C*H*W) in the range [0.0,1.0]
	ToPILImage: convert a tensor to PIL image
	CenterCrop:在图片的中间区域进行裁剪
	RandomCrop:在一个随机的位置进行裁剪
	RandomHorizontalFlip:以0.5的概率水平翻转给定的PIL图像
	RandomVerticalFlip:以0.5的概率竖直翻转给定的PIL图像
	RandomResizedCrop:将PIL图像裁剪成任意大小和纵横比
	Grayscale:将图像转换为灰度图像
	RandomGrayscale:将图像以一定的概率转换为灰度图像
	FiceCrop:把图像裁剪为四个角和一个中心

dataloader

class BSDS500Crop128(Dataset):
    def __init__(self, folder_path):
        self.files = sorted(glob.glob('%s/*.*' % folder_path))
        self.transform = transforms.Compose([
            transforms.RandomCrop(128),
            transforms.RandomHorizontalFlip(),
            transforms.RandomVerticalFlip(),
            transforms.ToTensor()
        ])

    def __getitem__(self, index):
        path = self.files[index % len(self.files)]
        img = Image.open(path)
        img = self.transform(img)
        return img

    def __len__(self):
        return len(self.files)
dataset=BSDS500Crop128(PATH)
dataloader = DataLoader(
    dataset,
    batch_size=args.batch_size,
    shuffle=args.shuffle,
    num_workers=args.num_workers)

基础知识记录

reshape and shape

data.reshape(A,B) //data数据重塑为(A,B)的矩阵
data.reshape(A,-1)//将数据重塑为A行自定义可扩展的列
data.shape //返回data的矩阵形状
shape(data)//等价上述
data.shape[0]//返回data的行值
data.shape[1]//返回数据的列数

dot multiply

np.dot(A,B)//是真正意义上线性代数的乘积
np.multiple(A,B)//对应元素乘积
发布了9 篇原创文章 · 获赞 10 · 访问量 2504

猜你喜欢

转载自blog.csdn.net/qq_42281425/article/details/92808469