我在Pytorch中使用lstm,我想计算训练的准确性。我在这里找到了一个类似的问题:Calculate the accuracy every epoch in PyTorch,但这不是我想要的,因为我不想手动应用任何随机阈值。还有其他方法可以根据我得到的损失来计算准确性吗?
这是我制作的模型。输出为一维张量的形式,其中包含从0到1的值。我正在尝试找出模型训练的准确性。
class fullstop(nn.Module):
def __init__(self):
super(fullstop, self).__init__()
self.seq1=nn.LSTM(input_size=300,hidden_size=200)
self.fc1=nn.Linear(400,400)
self.relu1 = nn.ReLU()
self.dout = nn.Dropout(0.2)
self.fc2 = nn.Linear(400, 100)
self.prelu = nn.PReLU(1)
self.out = nn.Linear(100, 1)
self.out_act = nn.Sigmoid()
self.fcf=nn.Linear(4,1)
def forward(self,input1,input2):
prefix1,h1=self.seq1(input1)
suffix1,h2=self.seq1(input2)
prefix1=Variable(prefix1)
suffix1=Variable(suffix1)
result=torch.cat((prefix1,suffix1),-1)
a1 = self.fc1(result)
h1 = self.relu1(a1)
dout = self.dout(h1)
a2 = self.fc2(dout)
h2 = self.prelu(a2)
a3 = self.out(h2)
y = self.out_act(a3)
t = torch.rand(len(input1), 4, 1)[:, :, -1]
tf=F.sigmoid(self.fcf(t))
t_final=tf[:,-1]
return t_final
FullStop=fullstop()
for t in range(num_epochs):
# Forward pass
y_pred = FullStop(X1,X2)
loss = loss_fn(y_pred, y_actual)
if t % 10 == 0:
print("Epoch ", t, "loss1: ", loss.item())
hist[t] = loss.item()
# Zero out gradient, else they will accumulate between epochs
optimiser.zero_grad()
# Backward pass
loss.backward()
# Update parameters
optimiser.step()