TuckER模型 pytorch损失函数

TuckER模型

class TuckER(torch.nn.Module):
    def __init__(self, d, d1, d2, **kwargs):
        super(TuckER, self).__init__()

        self.E = torch.nn.Embedding(len(d.entities), d1, padding_idx=0)
        self.R = torch.nn.Embedding(len(d.relations), d2, padding_idx=0)
        self.W = torch.nn.Parameter(torch.tensor(np.random.uniform(-1, 1, (d2, d1, d1)), 
                                    dtype=torch.float, device="cuda", requires_grad=True))

        self.input_dropout = torch.nn.Dropout(kwargs["input_dropout"])
        self.hidden_dropout1 = torch.nn.Dropout(kwargs["hidden_dropout1"])
        self.hidden_dropout2 = torch.nn.Dropout(kwargs["hidden_dropout2"])
        self.loss = torch.nn.BCELoss()  ###损失函数

        self.bn0 = torch.nn.BatchNorm1d(d1)
        self.bn1 = torch.nn.BatchNorm1d(d1)
        

    def init(self):
        xavier_normal_(self.E.weight.data)
        xavier_normal_(self.R.weight.data)

    def forward(self, e1_idx, r_idx):
        e1 = self.E(e1_idx)
        x = self.bn0(e1)
        x = self.input_dropout(x)
        x = x.view(-1, 1, e1.size(1))

        r = self.R(r_idx)
        W_mat = torch.mm(r, self.W.view(r.size(1), -1))
        W_mat = W_mat.view(-1, e1.size(1), e1.size(1))
        W_mat = self.hidden_dropout1(W_mat)

        x = torch.bmm(x, W_mat) 
        x = x.view(-1, e1.size(1))      
        x = self.bn1(x)
        x = self.hidden_dropout2(x)
        x = torch.mm(x, self.E.weight.transpose(1,0))
        pred = F.sigmoid(x)
        return pred

self.loss = torch.nn.BCELoss()
loss = model.loss(predictions, targets) ##predictions是Sigmoid二分类

Examples::

        >>> m = nn.Sigmoid()
        >>> loss = nn.BCELoss()
        >>> input = torch.randn(3, requires_grad=True)
        >>> target = torch.empty(3).random_(2)
        >>> output = loss(m(input), target)
        >>> output.backward()

目标:可视化loss和标量值
pytorch可视化,安装tensorboardX和tensorflow
pip install tensorflow (服务器上已经安装了1.4.0版本)
pip install tensorboardX

6102062-b0b340175d0b7e64.png
tensorflow已经安装了1.4.0版本

使用tensorboardX,画出pytorch框架下的数值函数变化图
参考文章: Pytorch使用tensorboardX可视化。超详细!!!

from tensorboardX import SummaryWriter  ##引用该模块

model.init()
opt = torch.optim.Adam(model.parameters(), lr=self.learning_rate)
writer = SummaryWriter('runs')  ##放在优化之后

###在每个epcoh中添加这个标量
writer.add_scalar('train_loss', np.mean(losses), epoch)

###关闭
writer.close()  
tensorboard --logdir runs
6102062-56a54d4c2bc1fdc8.png
tensorboard --logdir runs
6102062-8fdf7a0c502c861f.png
图片.png

转载于:https://www.jianshu.com/p/21c2c24b5425

猜你喜欢

转载自blog.csdn.net/weixin_34319999/article/details/91140276