fix shape one hot cross entropy

This commit is contained in:
lucasdelimanogueira 2024-05-21 19:50:17 -03:00
parent ed05e57cf4
commit 34a762b666
4 changed files with 13 additions and 8 deletions

View file

@ -27,7 +27,4 @@ def one_hot_encode(x, num_classes):
target_idx = int(x.tensor.contents.data[i])
one_hot[i][target_idx] = 1
if x.numel < 2:
one_hot = one_hot[0]
return norch.Tensor(one_hot)

View file

@ -45,6 +45,7 @@ class CrossEntropyLoss(Loss):
target = norch.one_hot_encode(target, num_classes)
logits = norch.softmax(input, dim=0)
target = target.reshape(logits.shape)
cost = -(logits.log() * target).sum()
else:
@ -52,6 +53,7 @@ class CrossEntropyLoss(Loss):
assert target.shape == input.shape, \
"Input and target shape does not match: {} and {}".format(input.shape, target.shape)
logits = norch.softmax(input, dim=0)
target = target.reshape(logits.shape)
cost = -(logits.log() * target).sum()
@ -65,6 +67,7 @@ class CrossEntropyLoss(Loss):
batch_size = input.shape[0]
logits = norch.softmax(input, dim=1)
target = target.reshape(logits.shape)
cost = -(logits.log() * target).sum() / batch_size
else:
@ -74,6 +77,7 @@ class CrossEntropyLoss(Loss):
batch_size = input.shape[0]
logits = norch.softmax(input, dim=1)
target = target.reshape(logits.shape)
cost = -(logits.log() * target).sum() / batch_size
if input.requires_grad:

14
test.py
View file

@ -23,13 +23,15 @@ class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(784, 5)
self.sigmoid = nn.Sigmoid()
self.sigmoid1 = nn.Sigmoid()
self.fc2 = nn.Linear(5, 10)
self.sigmoid2 = nn.Sigmoid()
def forward(self, x):
out = self.fc1(x)
out = self.sigmoid(out)
out = self.sigmoid1(out)
out = self.fc2(out)
out = self.sigmoid2(out)
return out
@ -49,10 +51,11 @@ for epoch in range(epochs):
target = target
x = x.to(device)
target = target.to(device).unsqueeze(-1)
target = target.to(device)
outputs = model(x)
print(outputs.shape, target.shape, x.shape)
outputs = model(x).squeeze(-1)
print(x.shape, outputs.shape, target.shape)
loss = criterion(outputs, target)
optimizer.zero_grad()
@ -70,6 +73,7 @@ for epoch in range(epochs):
print('f1 depois', model.fc1.bias)
print('f2 depois', model.fc2.bias)
print('\n\n')
exit()
print(f'Epoch [{epoch + 1}/{epochs}], Loss: {loss[0]:.4f}')
loss_list.append(loss[0])