其他分享
首页 > 其他分享> > 自定义打印类信息:def __repr__(self)

自定义打印类信息:def __repr__(self)

作者:互联网

Example:

import torch
import torch.nn.functional as F
from torch.nn.modules.module import Module
from torch.nn.parameter import Parameter

class GraphConvolution(Module):
    def __init__(self, in_features, out_features, dropout=0., act=F.relu):
        super(GraphConvolution, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.dropout = dropout
        self.act = act
        self.weight = Parameter(torch.FloatTensor(in_features, out_features))
        self.reset_parameters()

    def reset_parameters(self):
        torch.nn.init.xavier_uniform_(self.weight)

    def forward(self, input, adj):
        input = F.dropout(input, self.dropout, self.training)
        support = torch.mm(input, self.weight)
        output = torch.spmm(adj, support)
        output = self.act(output)
        return output

    # def __repr__(self):
    #     return self.__class__.__name__ + ' (' \
    #            + str(self.in_features) + ' -> ' \
    #            + str(self.out_features) + ')'
if __name__ =='__main__':
    gc = GraphConvolution(in_features=10, out_features=10, dropout=0., act=F.relu)
    print(gc)

加 def __repr__(self)

GraphConvolution (10 -> 10)

不加 def __repr__(self) :

GraphConvolution()

 

标签:__,features,自定义,self,torch,def,out
来源: https://www.cnblogs.com/BlairGrowing/p/16066265.html