In the section: Calculate Masked Self-Attention, the statement:
maskedSelfAttention(encodings_matrix, mask)
is used. Are you somehow invoking the forward function? Maybe the class wants a call function?
Can you post a screen capture image where you see that code, along with some surrounding context?
Here is the screen shot: the last python statement is what puzzled me. The class definition does not contain a 'call ’ function. Unless the superclass contains that, I am not understanding how the call to the class instance (maskedSelfAttention) is defined. The class definition for ‘forward’ has the correct signature, but I don’t see any way that is being invoked. Probably my understanding of python is faulty?
I have not looked at this course yet, but have an obvious question: is the point of it that it introduces you to PyTorch in addition to Attention Models in PyTorch or does it presuppose a knowledge of PyTorch? Note that PyTorch is pretty thin on the ground here in DLAI land: the only other course or specialization that uses it is the GANs specialization and that has been the exception up to this point.
Hello, @fredm73,
Besides Paul’s comments on forward()
, if you define a class, like the simple example below,
class ClassABC:
pass
ClassABC
isn’t an instance yet, only after you “call” it, does it become an instance:
class_abc_instance = ClassABC()
If you do these two prints, you will see that only the second one tells you it is an object.
print(1, ClassABC)
print(2, class_abc_instance)
Furthermore, if __init__
is defined in the class, or in the superclass, you can give it a list of arguments when “calling” the class:
class ClassABC:
def __init__(self, message):
print(message)
class_abc_instance = ClassABC("Hello World!")
If these interest you, then “Python Class Tutorials” are good keywords for you to search for some 30-minute or 1-hour tutorials! Knowing class helps you custom-build model, read others’ works, or do research with Pytorch.
Do simple experiments with simple codes like mine here in this post helps, too!
Cheers,
Raymond
I think the instructor tells in the first coding lab. As you inherit from nn Module, it automatically calls the forward method in the superclass.
Explained here - Build the Neural Network — PyTorch Tutorials 2.6.0+cu124 documentation