Skip to content
DeepForgeby svx
All problems
MediumCalculusca-205

Reverse-Mode AD Graph Cost

Problem

For a fully connected network with layer sizes layers = [n0, n1, ..., nL], the forward pass performs sum(n_i * n_{i+1}) multiply-adds.

Reverse-mode AD costs about twice the forward pass in the backward sweep, so the total is 3 * sum(n_i * n_{i+1}). Return the total operation count.

Hint

Reverse mode costs roughly the same order as the forward pass, with about 2x the constant.

Test cases

case 1
in: [[2,3]]
expected: 18
case 2
in: [[3,4,2]]
expected: 60
case 3
in: [[5]]
expected: 0
case 4
in: [[1,1,1]]
expected: 6
Starter codePython
def reverse_ad_cost(layers):
    # Your code here
    pass

4 test cases · run your code against them in the editor