pytorch: A 60 Minute blitz笔记

本文是PyTorch的60分钟快速入门笔记,涵盖了基础的张量操作、自动微分、神经网络构建及训练,并强调了GPU训练的重要性。笔记详细解释了张量的使用、NumPy桥接、自动微分的工作原理,以及如何在PyTorch中建立和优化神经网络模型。
摘要由CSDN通过智能技术生成

pytorch: A 60 Minute blitz笔记

0. What
  • A replacement for NumPy to use the power of GPUs
  • a deep learning research platform that provides maximum flexibility and speed
1. Basic
1.1 tensors
x = torch.empty(5, 3) # unitialized
x = torch.rand(5, 3)
x = torch.zeros(5, 3, dtype=torch.long)
x = torch.tensor([5.5, 3]) # construct from data
x = torch.randn_like(x, dtype=torch.float)    # override dtype!
print(x.size())
> torch.Size([5, 3]) # support all tuple operations

Size() object support all tuple operations

1.2 Operations
  • add
print(x + y) # opt1
print(torch.add(x, y)) # opt2
y.add_(x) # add x to y

Any operation thart mutates a tensor in-place is post-fixed with an _. For example: x.copy_(y),x.t_(), will change x

  • index: standard Numpy-like
print(x[:, 1])
  • resizing: torch.view
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)

use .item() to get the value as a python number(for one element tensor)

1.3 Numpy Bridge

The Torch Tensor and NumPy array will share their underlying memory locations, and changing one will change the other.

  • Tensor -> Array: ts.numpy()
  • Array -> Tensor: torch.from_numpy(ar)
  • CUDA tensor: using .to(device, dtype, ...) method
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值