Skip to content

intro to pytorch #3

@enryH

Description

@enryH

Regarding two questions regarding the exercises. Please see the two comments:

  • initialise weights to zero: check out this code -> zero_() method call on paramters (the underscore tell you that is changing the state as far as I know)

  • if you make your custom module, it has the attributes you give it (and some methods it inherits). So a nn.Linear instance has attributes weight and bias, not your custom class bases on nn.Module

    class LinearRegression(nn.Module):
    
     def __init__(self):
         """Everything stateful which you need to use your model goes here."""
         super().__init__()  # needs to be here for API reasons -> call nn.Module.__init__
         self.linear_reg = nn.Linear(1, 1)
    
     def forward(self, x):  # now with input
         return self.linear_reg(x)
    
    
     lin_reg = LinearRegression()
     lin_reg.linear_reg.weight # access weight of underlying nn.Linear instance 
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions