Pytorch for freshman

Pytorch

Basic

Lambda Function

Anonymous functioni that’s not meant to be stored.

Lambda Functions with Practical Examples in Python

Device configuration

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

Tensor

Tensor and NumPy Transfer

np_array = np.array(data)
x_np = torch.from_numpy(np_array)
t = torch.ones(5)
n = t.numpy()

Tensors on the CPU and NumPy arrays can share their underlying memory locations, and changing one will change the other

Automatic Differentiation

There are reasons you might want to disable gradient tracking:

  • To mark some parameters in your neural network at frozen parameters. This is a very common scenario for finetuning a pretrained network
  • To speed up computations when you are only doing forward pass, because computations on tensors that do not track gradients would be more efficient.
with torch.no_grad():

Optimizing Model Parameters

optimizer.zero_grad() # reset the gradients of model parameters
loss.backward() # compute the gradients of tensors
optimizer.step() # update the parameters

Coding Order

Device Configuration(GPU)

  1. Hyper Parameters
  2. Data Preprocessing and Data Loader
  3. Model Declaration
  4. Loss and Optimizer
  5. Train and Test

Train & Test

model.train() # be used in training, dropout and batch normalization
model.eval() # be used in testing
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值