Pytorch基础篇--0

源码:github code
1.自动求导基础例子 1
2.自动求导基础例子 2
3.numpy转tensor;tensor转numpy
4.输入管道 and自定义自用数据输入管道
5.预训练模型(模型微调)、保存、加载模型(整个模型or仅模型的参数)

1.自动求导基础例子 1

import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms

# Create tensors.创建张量
x = torch.tensor(9., requires_grad=True)
w = torch.tensor(6., requires_grad=True)
b = torch.tensor(7., requires_grad=True)

# Build a computational graph.建立计算图
y = w * x + b    # y = 6 * x + 7

# Compute gradients.计算梯度
y.backward()

# Print out the gradients.打印梯度
print(x.grad)    # x.grad 对x求导
print(w.grad)    # w.grad 对w求导
print(b.grad)    # b.grad 对b求导
print(y.grad)    #y.grad 对y求导

tensor(6.)
tensor(9.)
tensor(1.)
None

2.自动求导基础例子 2

import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms

# Create tensors of shape (10, 3) and (10, 2).
# 创建10组数据,x源数据特征维度为3,y目标数据维度为2
x = torch.randn(10, 3)
y = torch.randn(10, 2)

# Build a fully connected layer.
# 建立一个全连接层
linear = nn.Linear(3, 2)
print('w: ', linear.weight)
print('b: ', linear.bias)

# Build loss function and optimizer.
# 建立损失函数和梯度优化器
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(linear.parameters(), lr=0.01)

# Forward pass.
# 前向传递
pred = linear(x)

# Compute loss.
# 计算损失值
loss = criterion(pred, y)
print('loss: ', loss.item())

# Backward pass.
# 反向传递
loss.backward()

# Print out the gradients.
# 打印梯度
print('dL/dw: ', linear.weight.grad)
print('dL/db: ', linear.bias.grad)

# 1-step gradient descent.
# 梯度下降优化
optimizer.step()

# You can also perform gradient descent at the low level.
# 可以按以下方式将梯度下降的步子调得更小
# linear.weight.data.sub_(0.01 * linear.weight.grad.data)
# linear.bias.data.sub_(0.01 * linear.bias.grad.data)

# Print out the loss after 1-step gradient descent.
# 打印第一步梯度优化后的loss值
pred = linear(x)
loss = criterion(pred, y)
print('loss after 1 step optimization: ', loss.item())

w: Parameter containing:
tensor([[-0.2890, 0.4896, 0.5295], [-0.5184, 0.0823, -0.0142]], requires_grad=True)
b: Parameter containing:
tensor([-0.2531, 0.1505], requires_grad=True)
loss: 2.2962872982025146
dL/dw: tensor([[-0.2974, -0.1092, 0.8263], [-1.0187, -0.3140, 0.0013]])
dL/db: tensor([-0.2196, 1.0152])
loss after 1 step optimization: 2.2665205001831055

3.numpy转tensor;tensor转numpy

import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms

# Create a numpy array.
x = np.array([[1, 2], [3, 4]])
print('numpy x:',x)

# Convert the numpy array to a torch tensor.
# numpy数组转换为tensor
y = torch.from_numpy(x)
print('tensor y:',y)

# Convert the torch tensor to a numpy array.
# tensor转换为numpy数组
z = y.numpy()
print('numpy z:',z)

numpy x: [[1 2] [3 4]]
tensor y: tensor([[1, 2], [3, 4]])
numpy z: [[1 2] [3 4]]

4.输入管道 and自定义自用数据输入管道

import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms

# Download and construct CIFAR-10 dataset.
# 下载10分类图像数据
train_dataset = torchvision.datasets.CIFAR10(root='../../data/',
                                             train=True,
                                             transform=transforms.ToTensor(),
                                             download=True)

# Fetch one data pair (read data from disk).
# 取一个数据对
image, label = train_dataset[0]
print(image.size())
print(label)

# Data loader (this provides queues and threads in a very simple way).
# 加载数据
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=64,
                                           shuffle=True)

# When iteration starts, queue and thread start to load data from files.
# 使数据变成可迭代的数据
data_iter = iter(train_loader)

# Mini-batch images and labels.
# 通过.next()获取一小批次数据
images, labels = data_iter.next()

# Actual usage of the data loader is as below.
for images, labels in train_loader:
    # Training code should be written here.
    pass
# ----------自定义数据输入管道------------------ #
# You should build your custom dataset as below.
class CustomDataset(torch.utils.data.Dataset):
    def __init__(self):
        # TODO
        # 1. Initialize file paths or a list of file names. 
        pass
    def __getitem__(self, index):
        # TODO
        # 1. Read one data from file (e.g. using numpy.fromfile, PIL.Image.open).
        # 2. Preprocess the data (e.g. torchvision.Transform).
        # 3. Return a data pair (e.g. image and label).
        pass
    def __len__(self):
        # You should change 0 to the total size of your dataset.
        return 0 

# You can then use the prebuilt data loader. 
custom_dataset = CustomDataset()
train_loader = torch.utils.data.DataLoader(dataset=custom_dataset,
                                           batch_size=64, 
                                           shuffle=True)

5.预训练模型、保存、加载模型

import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms

# Download and load the pretrained ResNet-18.
# resnet = torchvision.models.resnet18(pretrained=True)
resnet = torchvision.models.resnet18(pretrained=False)

# If you want to finetune only the top layer of the model, set as below.
for param in resnet.parameters():
    param.requires_grad = False
    print(type(param.data), param.size())

# Replace the top layer for finetuning.
resnet.fc = nn.Linear(resnet.fc.in_features, 100)  # 100 is an example.

# Forward pass.
# 一小批图片,resnet模型预测
images = torch.randn(64, 3, 224, 224)
outputs = resnet(images)
print(outputs.size())     # (64, 100)

# Forward pass.
# 一张图片,resnet模型预测
images = torch.randn(1, 3, 224, 224)
outputs = resnet(images)
print(outputs)     # (64, 100)

# Save and load the entire model.
# 保存、加载整个模型
torch.save(resnet, 'model.ckpt')
model = torch.load('model.ckpt')

# Save and load only the model parameters (recommended).
# 仅保存、加载模型的参数
torch.save(resnet.state_dict(), 'params.ckpt')
resnet.load_state_dict(torch.load('params.ckpt'))

<class 'torch.Tensor'> torch.Size([64, 3, 7, 7])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64, 64, 3, 3])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64, 64, 3, 3])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64, 64, 3, 3])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64, 64, 3, 3])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([64])
<class 'torch.Tensor'> torch.Size([128, 64, 3, 3])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128, 128, 3, 3])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128, 64, 1, 1])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128, 128, 3, 3])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128, 128, 3, 3])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([128])
<class 'torch.Tensor'> torch.Size([256, 128, 3, 3])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256, 256, 3, 3])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256, 128, 1, 1])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256, 256, 3, 3])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256, 256, 3, 3])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([256])
<class 'torch.Tensor'> torch.Size([512, 256, 3, 3])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512, 512, 3, 3])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512, 256, 1, 1])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512, 512, 3, 3])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512, 512, 3, 3])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([512])
<class 'torch.Tensor'> torch.Size([1000, 512])
<class 'torch.Tensor'> torch.Size([1000])
torch.Size([64, 100])

tensor([[-0.1283, -1.1639, 0.0621, -0.2726, 0.8435, -0.4389, -0.1071, 0.4760,
0.6909, -0.2902, -0.4895, 0.1199, -0.0188, 0.2916, 0.1144, 0.1217,
-0.1636, 0.5199, 0.9729, -0.2111, -0.6451, 0.7002, -0.9556, 0.0440,
1.0738, -0.5497, 0.3759, 0.2009, 0.1486, 0.2877, -0.0455, -0.3775,
-0.3144, -0.3720, -0.6317, 0.2952, 0.5883, 0.0979, -0.0203, 0.0584,
0.4892, -0.0672, 0.8977, -0.1919, 0.7094, 0.2328, -0.6055, 0.4562,
0.0236, -0.6255, -0.6796, 0.2483, 0.9799, -0.7623, 0.4199, 0.0543,
-1.0435, -0.5571, -0.4757, -0.4201, -0.0206, -1.0947, 0.1431, 0.0288,
0.0987, 0.4257, -0.3387, -0.3624, 0.3040, -0.5715, -1.3718, 0.2144,
-0.3756, -1.2408, -0.3292, -0.3726, 0.1383, -0.8428, -0.4842, 0.2166,
0.7600, 0.1267, -0.3318, 0.5117, 0.1809, -0.1803, 0.3676, 0.8243,
-0.3718, -0.3557, 0.0967, 0.2485, 0.0497, -0.0762, 0.2572, -0.0783,
-0.3285, -0.2424, -0.0206, -0.2670]], grad_fn=<AddmmBackward>)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,922评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,591评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,546评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,467评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,553评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,580评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,588评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,334评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,780评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,092评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,270评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,925评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,573评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,194评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,437评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,154评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,127评论 2 352

推荐阅读更多精彩内容

  • layer filters size input ...
    张凯宇阅读 247评论 0 0
  • 文/魏绍寒 壹 骏衫和曼函是在学校里那棵三百年的老槐树下相见的 骏衫的喉结上下滚动,紧贴在裤线上的手,不安分的颤动...
    夜闻绍寒阅读 518评论 4 5
  • 我们在时光里流浪 兜兜转转 擦肩而过 看够了流放孤独 人来人往 一路上跌跌撞撞 直到遇见了你 在这座城
    慕星读者OR独者阅读 162评论 0 1
  • ​ 前几天跟同事聊了一个话题,简单的用一句话来总结的就是:姑娘都是现实版的樊胜美。我没有怎么认真看过《欢乐颂》,也...
    巨梨App阅读 523评论 0 0