The following information is provided prior to the first routine we are asked to implement in the week 4 lab on transformers.
I am going to show first an error I get when I implement the routine in a way I believe is wrong, because it provides some useful debugging info, then an error I get when I implement it in a way I believe is correct. I implemented it in the correct way first, in reality.
The unit test is below
from public_tests import *
get_angles_test(get_angles)
# Example
position = 4
d_model = 8
pos_m = np.arange(position)[:, np.newaxis]
dims = np.arange(d_model)[np.newaxis, :]
get_angles(pos_m, dims, d_model)
The debugging output
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-20-2c76efaf3fad> in <module>
1 from public_tests import *
2
----> 3 get_angles_test(get_angles)
4
5 # Example
~/work/W4A1/public_tests.py in get_angles_test(target)
11
12 assert type(result) == np.ndarray, "You must return a numpy ndarray"
---> 13 assert result.shape == (position, d_model), f"Wrong shape. We expected: ({position}, {d_model})"
14 assert np.sum(result[0, :]) == 0
15 assert np.isclose(np.sum(result[:, 0]), position * (position - 1) / 2)
AssertionError: Wrong shape. We expected: (4, 16)
Okay, well, that doesn’t surprise me. But what that does inform me, is that the position is supposed to form dimension zero.
result[pos,dims]
Is the desired dimension
Okay, I’m done with the definitely wrong way, that I knew was wrong from the start, now that I’ve proved the point that the position is along the column.
I’ve transposed it (actually, removed a transpose, since I had the dimensions right initially)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-22-2c76efaf3fad> in <module>
1 from public_tests import *
2
----> 3 get_angles_test(get_angles)
4
5 # Example
~/work/W4A1/public_tests.py in get_angles_test(target)
12 assert type(result) == np.ndarray, "You must return a numpy ndarray"
13 assert result.shape == (position, d_model), f"Wrong shape. We expected: ({position}, {d_model})"
---> 14 assert np.sum(result[0, :]) == 0
15 assert np.isclose(np.sum(result[:, 0]), position * (position - 1) / 2)
16 even_cols = result[:, 0::2]
AssertionError:
Here I’m seeing that if I set the position equal to zero and sum along the rows, it is supposed to sum to zero. That’s a Discrete Fourier sum in terms of sines and cosines of the following segment of the table
A DFT sums to zero if and only if the signal being transformed is zero. However, a DFT can be a dirac delta function (zero everywhere but at one frequency). This is a DFT where the amplitude of every component is one. In other words, a constant signal. So I actually computed this sum analytically, to check, and it is approximately one, not approximately zero. Handwritten calculation attached.
Could someone please let me know what you think? Either if something is going wrong conceptually, with this calculation, or with the unit test?
I’m very tired right now, but fortunately there are also a couple of days to look into this.
Thanks,
Steven Dorsher