C4W4 - Lab 2: Unable to get the expected results

I am not getting expected results for MyResidual function and resfg as shown in the below screenshot. There is not much information / instructions to complete it.

I had the same struggle with MyResidule (your block [23]). Line 605 of the Trax implementation might give you some idea (and also you’ll need the ‘None’ argument).

With resfg (your block [29]), note the hint suggests using MyResidule or tl.Residual. If you implement ‘MyResidual’ correctly then it should takes care of adding the input to the ouptut and returns the sum.

Hope this helps.

I agree, it’s unclear from the previous diagrams and examples how MyResidual() under Part 1.2 should work. I made these notes to walk myself through it:

  • initial stack state:
    [x, n, m] = ([1], 'n', 'm')
  • use tl.Branch(arg1, arg2). As @yhu18 points out, line 623 of the trax source code indicates the number of arguments, their order and where the MyResidual()layer” parameter will be used when called a bit later in the notebook as layer=bl_add1. Additionally, the default parameter value in the call to Residual() in line 605 points out a sensible value for the other argument.
  • When mr = MyResidual(bl_add1) is defined and mr([x, n, m]) is called, tl.Branch(None, bl_add1) is called and the stack becomes, roughly:
    ([1], bl_add1([1]) , 'n', 'm') = ([1], [2], 'n', 'm')
  • Finally, as expected, tl.Add() grabs the two items at the top of the stack and adds them:
    ([1] + [2], 'n', 'm') = ([3], 'n', 'm')