pytorch使用

这篇博客介绍了PyTorch的基本概念和操作,包括张量与NumPy的互换,张量的视图操作,广播机制,计算图的使用以及如何在CUDA上进行GPU计算。此外,还展示了如何计算梯度和应用到线性回归问题。文章通过实例详细解释了PyTorch中nn.Linear全连接层的用法。
摘要由CSDN通过智能技术生成

基本介绍

Tensors and relation to numpy

import torch
import numpy as np
# we create tensors in a similar way to numpy nd arrays
x_numpy = np.array([0.1, 0.2, 0.3])
x_torch = torch.tensor([0.1, 0.2, 0.3])
print('x_numpy,x_torch')
print(x_numpy, x_torch)
print()
#to and from numpy, pytorch
print('to and from numpy and pytorch')
print(torch.from_numpy(x_numpy), x_torch.numpy())
print()
#wen can do basic operations like +-*/
y_numpy = np.array([3, 4, 5.])
y_torch = torch.tensor([3, 4, 5.])
print('x+y=')
print(x_numpy+y_numpy, x_torch+y_torch)
print()
#many functions that are in numpy are also in pytorch
print('norm')#norm-- 范数
print(np.linalg.norm(x_numpy), torch.norm(x_torch))
print()
#to apply an operation along a dimension
#we use the dim keword argument instead of axis
print('mean along the 0th dimension')
x_numpy = np.array([[1, 2], [3, 4]])
y_torch = torch.tensor([[1, 2], [3, 4]])
print(np.mean(x_numpy, axis=0), torch.mean(x_torch, dim=0))

输出

// 输出
x_numpy,x_torch
[0.1 0.2 0.3] tensor([0.1000, 0.2000, 0.3000])

to and from numpy and pytorch
tensor([0.1000, 0.2000, 0.3000], dtype=torch.float64) [0.1 0.2 0.3]

x+y=
[3.1 4.2 5.3] tensor([3.1000, 4.2000, 5.3000])

norm
0.37416573867739417 tensor(0.3742)

mean along the 0th dimension
[2. 3.] tensor(0.2000)

Tensor.view

# 和numpy的reshape一样
N, C, W, H = 10000, 3, 28, 28
X = torch.randn((N, C, W, H))
print(X.shape)
print(X.view(N, C, 784).shape)
print(X.view(-1, C, 784).shape)  # automatically choose the 0th

输出

#output
torch.Size([10000, 3, 28, 28])
torch.Size([10000, 3, 784])
torch.Size([10000, 3, 784])

broadcast :

x = torch.empty(5, 1, 4, 1)
y = torch.empty(3, 1, 1)
print((x+y).size())

输出

#output
torch.Size([5, 3, 4, 1])

computation Graph

torch 在计算的时候,会自动建一个图,把计算过程简称一张图

# computation Graph

输出

#output

CUDA语法 使用gpu计算数据

# code
cpu = torch.device("cpu")
gpu = torch.device("cuda")

x = torch.rand(10)
print(x)
x = x.to(gpu)  # to gpu
print(x)
x = x.to(cpu)  # to cpu
print(x)

输出

#output
tensor([0.1262, 0.0894, 0.9269, 0.5523, 0.7255, 0.0495, 0.4450, 0.9474, 0.5318,
        0.0641])
tensor([0.1262, 0.0894, 0.9269, 0.5523, 0.7255, 0.0495, 0.4450, 0.9474, 0.5318,
        0.0641], device='cuda:0')
tensor([0.1262, 0.0894, 0.9269, 0.5523, 0.7255, 0.0495, 0.4450, 0.9474, 0.5318,
        0.0641])

Pytorch 梯度的计算

using gradient 简单计算梯度

视频 21分钟

Linear Regression 做线性回归

。。。。未完待续

常用模块

nn.Linear

参考资料

参考博客1

全连接层的简单实现(只有一层)

# Creat Fully Connected Network
class NN(nn.Module):
    def __init__(self, input_size, num_classes):
        super(NN, self).__init__()
        self.fc1 = nn.Linear(input_size, num_classes,bias=False)


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


net = NN(input_size=208*28, num_classes=10)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值