APEX

如何安装

git clone https://github.com/NVIDIA/apex.git
cd apex
pip3 install --no-cache-dir --global-option="--pyprof" --global-option="--cpp_ext" --global-option="--cuda_ext" ./

 google colab install apex amp

try:
  import apex
except Exception:
  ! git clone https://github.com/NVIDIA/apex.git
  % cd apex
  !pip install --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" .
  % cd ..

code

import apex  # OK
from apex import amp  # error

error

ImportError: cannot import name 'amp' from 'apex' (unknown location)

add --user

!pip install --no-cache-dir --global-option="--pyprof" --global-option="--cpp_ext" --global-option="--cuda_ext" ./ --user

still wrong......

如何使用

1) 训练、保存模型

2) 测试

Pytorch使用

1) 训练

from torch.cuda.amp import autocast as autocast

# 创建model,默认是torch.FloatTensor
model = Net().cuda()
optimizer = optim.SGD(model.parameters(), ...)

# 在训练最开始之前实例化一个GradScaler对象
scaler = GradScaler()

for epoch in epochs:
    for input, target in data:
        optimizer.zero_grad()

        # 前向过程(model + loss)开启 autocast
        with autocast():
            output = model(input)
            loss = loss_fn(output, target)

        # Scales loss. 为了梯度放大.
        scaler.scale(loss).backward()

        # scaler.step() 首先把梯度的值unscale回来.
        # 如果梯度的值不是 infs 或者 NaNs, 那么调用optimizer.step()来更新权重,
        # 否则,忽略step调用,从而保证权重不更新(不被破坏)
        scaler.step(optimizer)

        # 准备着,看是否要增大scaler
        scaler.update()
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2021-09-10
  • 2022-12-23
  • 2021-05-03
  • 2022-12-23
猜你喜欢
  • 2021-09-20
  • 2021-10-30
  • 2022-12-23
  • 2021-05-20
  • 2022-12-23
  • 2021-11-12
  • 2021-08-19
相关资源
相似解决方案