Positional_encoding (initialization)

Hi @TMosh and @paulinpaloalto

How to initialize ? zero didnt work and
no value for k given.

{moderator edit - solution code removed}

I freely admit this is all a little confusing. But it would be a good idea to study the “docstring” for the get_angles function more carefully. They tell you everything you need to know there to be able to call it correctly based on the information you are given in positional_encoding. The value of the k argument is determined by the value of d, right? But it’s not just a scalar. You may also find np.arange useful. In fact if you look at the test cell for get_angles, they gave you an example of how to use it for this purpose.

1 Like

Thanks for reply. Actually, still struggling to debug this
get_angles have 3 parameters
But in positional_encoding…I tried
but lead to another error

{moderator edit - solution code removed}

def get_angles(pos, k, d):
“”"
Get the angles for the positional encoding

Arguments:

    pos -- Column vector containing the positions [[0], [1], ...,[N-1]]
    k --   Row vector containing the dimension span [[0, 1, 2, ..., d-1]]
    d(integer) -- Encoding size

Also tried

{moderator edit - solution code removed}

That’s one step closer, but you’re still not really understanding the arguments to get_angles. At least you’ve now made the first argument a list, but that means it is a 1D object. It needs to be a 2D column vector, right? The docstring literally says that. So you need a reshape there. Then you’re still passing a scalar for the k argument, but that is supposed to be a 2D row vector. It literally says that also in the docstring for get_angles.

instead of reshaping within,
I just used [
]

but didnt help

{moderator edit - solution code removed}

Well, does that actually work? Apparently not. Try printing the shape of [np.arange(42)]. Is it a 2D column vector?

This is how debugging works. You can’t just wonder what something does: you have to actually verify the behavior at a more granular level rather than treating the whole thing as a black box.

out = np.arange(10)
print(out)
print(type(out))
print(f"out.shape {out.shape}")
out = [np.arange(10)]
print(out)
print(type(out))
print(f"out.shape {out.shape}")

Running the above gives this:

[0 1 2 3 4 5 6 7 8 9]
<class 'numpy.ndarray'>
out.shape (10,)
[array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])]
<class 'list'>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-71d5a5451bab> in <module>
      6 print(out)
      7 print(type(out))
----> 8 print(f"out.shape {out.shape}")

AttributeError: 'list' object has no attribute 'shape'

Hmmmmm. Perhaps a bit more thought is required. :nerd_face:

thx for your advice and effort to explain in detail…But

I checked the working
image

{moderator edit - solution code removed}

Since position is just scalar

{moderator edit - solution code removed}

{moderator edit - solution code removed}

One step at a time, I guess. Well, what kind of vector is the k argument supposed to be? We’ve been over this, right?

Solution found: encoding size is d and not positions !

Glad to hear you got to the solution. Notice that I mentioned the relationship between k and d in my very first reply on this thread. It’s also clearly specified in the docstring for the get_angles function. The information is there for you to see, but it requires that you read it carefully. Being in a hurry when you read the instructions generally ends up wasting more time than it saves. Just sayin’ :nerd_face: …

1 Like