-
I am working on node classification model with test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=True) Accuracy function is: def compute_correct(pred, expected, size, batch):
def check(pred_i, exp_i):
for p, e in zip(pred_i, exp_i):
if p != e:
return False
return True
correct = 0
wrong = 0
for i in range(0, size):
pred_i = pred[batch==i]
exp_i = expected[batch==i]
if check(pred_i, exp_i):
correct += 1
else:
wrong += 1
return correct, wrong The result is considered as correct only if all nodes of specific graph are classified correctly (it is important in my problem), so I noticed that with
My model: class ChebNetLinear(nn.Module):
def __init__(
self,
in_channels : int,
hidden_channels : int,
out_channels : int,
K : int,
cheb_depth : int,
linear_depth : int,
linear_channels : int,
normalization : Optional[str] = None,
bias : Optional[bool] = True,
droprate : Optional[float] = 0.0):
super(ChebNetLinear, self).__init__()
# create ChebConv layers
self._convs = create_chebnet_convolutions(
in_channels,
hidden_channels,
hidden_channels,
K,
cheb_depth,
normalization,
bias,
droprate)
# create Linear layers
self._linear = create_linear_layers(
hidden_channels,
linear_channels,
out_channels,
linear_depth)
self._relu = nn.ReLU()
self._dropout = nn.Dropout(p=droprate)
self._logsoftmax = nn.LogSoftmax(dim=1)
def forward(self,
x,
edge_index,
edge_weight,
batch : Optional[Tensor] = None):
for i in range(len(self._convs)):
x = self._convs[i](x, edge_index, edge_weight, batch)
x = self._relu(x)
x = self._dropout(x)
for i in range(len(self._linear) - 1):
x = self._linear[i](x)
x = self._relu(x)
x = self._linear[-1](x)
x = self._logsoftmax(x)
return x Test function: def test(loader):
total_loss = 0
total_correct = 0
total_wrong = 0
with torch.no_grad():
model.eval()
for data in loader:
data = data.to(device)
y = model(
data.x,
data.edge_index,
data.edge_weight,
data.batch)
loss = criterion(y, data.y.argmax(dim=1))
total_loss += loss.item() * data.num_graphs
pred = y.argmax(dim=1)
expected = data.y.argmax(dim=1)
c,w = compute_correct(pred, expected, data.num_graphs, data.batch)
total_correct += c
total_wrong += w
return total_loss / len(test_data), total_correct / (total_correct + total_wrong) Is this normal? I cannot find any error in my code. As far as i know, test accuracy and loss should be the same on different execution with or without shuffle. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 13 replies
-
How many |
Beta Was this translation helpful? Give feedback.
Your total loss is not correctly calculated. It should be
in case your graphs are differently sized. This fixes one of the issues for me.
The other issue is related to
lambda_max
. Since you are usingNone
normalization, automaticlambda_max
inferral may be different depending on the batch you have sampled since by default we compute it via2 * edge_weight.max()
.fixes this for me.