Hi Standshik!



 standshik:

How do I package the idea of topk together so that I can create ‘mypackage’, import it and use mypackage.topk(k) instead of nn.ReLU()?


If it were me, I wouldn’t bother packaging it – I would just define topk
as a function (rather than a class).  You could define it (either as a
function or a class) in a separate package and import it (but how to
do that is a python question, rather than specific to pytorch).


    def forward(self, x):                          
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out



No need to instantiate a self.relu() replacement – just define your
topk function and call it in forward():
    def forward(self, x):                          
        out = self.fc1(x)
        # out = self.relu(out)
        out = my_topk_function (out, my_value_for_k)
        out = self.fc2(out)
        return out

Best.
K. Frank