pytorch Autograd [1]

J·2021년 6월 20일
0

pytorch

목록 보기
17/23

How autograd encodes the history

공식설명은 아래와 같다.

Autograd is reverse automatic differentiation system. Conceptually, autograd records a graph recording all of the operations that created the data as you execute operations, giving you a directed acyclic graph whose leaves are the input tensors and roots are the output tensors. By tracing this graph from roots to leaves, you can automatically compute the gradients using the chain rule.

Internally, autograd represents this graph as a graph of Function objects (really expressions), which can be apply() ed to compute the result of evaluating the graph. When computing the forwards pass, autograd simultaneously performs the requested computations and builds up a graph representing the function that computes the gradient (the .grad_fn attribute of each torch.Tensor is an entry point into this graph). When the forwards pass is completed, we evaluate this graph in the backwards pass to compute the gradients.

An important thing to note is that the graph is recreated from scratch at every iteration, and this is exactly what allows for using arbitrary Python control flow statements, that can change the overall shape and size of the graph at every iteration. You don’t have to encode all possible paths before you launch the training - what you run is what you differentiate.

Autograd는 reverse automatic differentiation system이다. (역방향으로 자동으로 미분을 해주는 시스템) 개념적으로 autograd는 graph를 기록한다. 그 graph는 연산을 수행할 때마다 데이터를 만들어내는 모든 operation들을 기록하고, directed acyclic graph(방향이 있고, 순환하지 않는 그래프)를 준다. 이 그래프는 root가 output이고, leaves가 input이다.
내부적으로 autograd는 이 graph를 Function object들의 graph로 나타낸다. 이를 통해 graph를 evaluate한 결과를 계산할 때 apply()를 사용할 수 있다. forward pass를 계산할 때 autograd는 동시에 주어진 연산을 수행하고, gradient를 계산하는 함수를 나타내는 그래프를 build up 한다. (각 torch.Tensor의 grad_fn attribute는 graph의 entry point이다.) forward pass가 완료되면 gradient를 계산하기 위해 이 그래프를 backward pass로 연산합니다.
가장 중요한 것은 이 그래프는 매 iteration마다 새롭게 처음부터 재생성된다는 것입니다. 이 특징으로부터 전체적인 shape과 graph의 size를 변경할 수 있는 arbitrary python control flow statements를 사용할 수 있게 된다. 동작하는 것이 미분하는 것이 되기 때문에 training을 시작하기 전에 모든 가능한 경로를 encode할 필요가 없다.

Locally disabling gradient computation

locally disable gradient computation을 위해 파이썬의 몇몇개의 mechanism을 사용할 수 있습니다.
코드의 한 block의 gradient를 비활성화하는 방법으로는 no-grad mode나 inference mode가 있습니다. requires_grad와 같은 tensor의 field를 setting을 통해 더 섬세하게 subgraph를 비활성화 할 수 있다.
이 뿐만 아니라 evaluation mode에 대해서 묘사하겠다. 이 방법은 실제로 gradient computation을 비활성화하는데 사용되지는 않지만 이 이름으로 인해 아래의 세가지 방법과 함께 사용된다.

Reference

  1. https://pytorch.org/docs/stable/notes/autograd.html
profile
I'm interested in processing video&images with deeplearning and solving problem in our lives.

0개의 댓글