TuckER模型|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
【TuckER模型|TuckER模型 pytorch损失函数】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
文章图片
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
文章图片
tensorboard --logdir runs
文章图片
图片.png
推荐阅读
- pytorch|使用pytorch从头实现多层LSTM
- Flutter的ListView
- Pytorch|Pytorch AlexNet Fashion-MNIST
- 一般模型化关系——从模型是什么到如何起作用的基本答案
- pytorch|YOLOX 阅读笔记
- Pytorch学习|sklearn-SVM 模型保存、交叉验证与网格搜索
- 旅途碎碎念
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 时间管理的任务模型
- 《DOM知识点总结》