Lesson 9 Discussion & Wiki (2019)

p = torch.linspace(0.01,1,100)
pcts=[0.3, 0.7]
pcts = tensor([0] + listify(pcts))
pcts = torch.cumsum(pcts, 0)

for i,o in enumerate(p):
    idx = (o >= pcts).nonzero().max()
    actual_pos = (o-pcts[idx]) / (pcts[idx+1]-pcts[idx])
    print(i,o, idx,actual_pos)

rewriting the same piece of code, now you can print and see the values.

scheds = [sched_cos(0.3, 0.6), sched_cos(0.6, 0.2)]
scheds[0](0.5)

you are combining two cos functions smoothly and the condition is that you want it start at 0.3 and go up to 0.6(for 30%) and then back down to 0.2(for 70%). (these are the y values on the graph)
So you basically have those three points. But you don’t know where the corresponding x values for these points are. This is controlled by what percentage you allocate to each of the schedulers.
pcts = [0.3,0.7] - first 30% sched_cos(0.3, 0.6) and last 70% sched_cos(0.6, 0.2)
pcts = [0.0,0.3,1.0] , because of the cumsum
starting point - sp; ending point - ep
for scheduler1: sp =0, ep =0.3
for scheduler2: sp =0.3, ep =1.0
actual_pos =( x - starting_point) / (ending_point - staring_point)
Hope that helps. :slight_smile:

i have a question on the last iteration of the loop:

for i,o in enumerate(p):
    idx = (o >= pcts).nonzero().max()
    actual_pos = (o-pcts[idx]) / (pcts[idx+1]-pcts[idx])
    print(i,o, idx,actual_pos)

it should return idx =2. Which should break the code as pcts[3] doesn’t exist. i don’t get how it works ?
i feel it is the equal comparison of floating point numbers that is causing it(but that fells odd, i think i’m wrong. missing something!!! )
Any help :slight_smile:

@jeremy Here is a nitpick. I noticed that in the video at 33 minutes, that the Excel version of NLL is using Log10. Pytorch uses the natural log. I tried to replicate the functions from the spreadsheet and they didn’t match.

Hi
I was going through the lesson 9. I wanted to know how the no of iterations i.e. (n-1)//bs + 1 was derived. The expression is correct for all the cases but I am trying to know how the expression came in the first place. It holds true for both even and odd numbers

>  for epoch in range(epochs):
>     for i in range((n-1)//bs + 1):
>         start_i = i*bs

Can someone please explain why we need super().setattr(k,v) in our DummyModule() class? Also which class’s setattr is it calling? Thanks guys.

Hello,

I’m having trouble understanding the lines

sm_pred = log_softmax(pred)
def nll(input, target): return -input[range(target.shape[0]), target].mean()
loss = nll(sm_pred, y_train)

I’m used to thinking of “likelihood” as being the probability of the data given the parameters, yet the second line uses the true target for the calculation. Why is this? Can someone help clarify what’s going on here?

1 Like

Hi @Rosst, that is a great question!

Loss functions for classification problems need the target labels and the predicted probabilities as inputs, because their computation compares the actual vs. predicted distribution of labels.

Here, the nll (negative log likelihood) loss function takes inputs sm_pred (the predicted labels) and y_train (the target labels).

Hi @cbenett could you please post a snippet showing code you are referring to? In general, .super() refers to the parent class. So the code is referring to the setattr method whichever class DummyModule() inherits from. But if DummyModule() doesn’t explicitly inherit from another class, I’m as confused as you, and I second your question!

It’s using integer array indexing to get the logp for the correct target; I was puzzled at this for some time too when I first encountered it here What is torch.nn really?

1 Like

Hi @cbenett. The super().setattr(k,v) sets all attributes of the DummyModule() object to their corresponding values (as given in init). If you comment that line and then try to create a DummyModule() object it will throw an error!

So here,super() refers to the ‘object’ itself in python. To make it clearer you can create your DummyModule class as this : class DummyModule(object) . This is same as class DummyModule().
We can’t say self.setattr(k,v) since this command will lead to infinite recursion.

Now, you may be wondering that why doesn’t the init method does the job of registering the values to their respective attribute. In this case it doesn’t, because whenever setattr method is explicitly written in a class, it is called instead of the normal mechanism(of setting the value of attributes)

Can someone please explain the purpose of setattr in runner init. runner

It adds callbacks to the runner objects so you can refer them like runner.name_of_the_callback(…)

1 Like

In this lesson we implemented negative log likelihood (nll), but I wonder how backpropagation is calculated using our function, because in our function we just did array look up and mean of those variables, how is gradient calculated on this function and how PyTorch handles it since we just gave it a array look up

nll

how do we find which portion of the code in lesson is in python or in pytorch?

Is anyone facing error as below for cross entropy calculation?

I was able to resolve the issue by converting y_train to long tensor

I have not seen error like this for anyone else on the forum. Any idea what is causing this weirdness?

Have you modified something up the chain, as when i look at y_train in my notebook it comes up as torch.LongTensor. While in your case, it is FloatTensor which is why you need to convert to LongTensor before processing it to mean function.

There is nothing that I modified. Even the results I get when loading the dataset shows different type.


Github link

Hi Santosh,

Your previous post had y_train coming up as Float Tensor, so something has changed between that part of the code and the code shared per your latest post? I cannot see all the code so can’t comment accurately.

Cheers,
Dev.

Thank you!


Could anybody help me about this index error? I copy the source code but …