From 34a762b666599885e28c6d44702e767b85bece23 Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Tue, 21 May 2024 19:50:17 -0300 Subject: [PATCH] fix shape one hot cross entropy --- norch/nn/__pycache__/loss.cpython-38.pyc | Bin 2717 -> 2782 bytes norch/nn/functional.py | 3 --- norch/nn/loss.py | 4 ++++ test.py | 14 +++++++++----- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/norch/nn/__pycache__/loss.cpython-38.pyc b/norch/nn/__pycache__/loss.cpython-38.pyc index b8133b99ff9fe85bc2549c254e8f67c3b96ca772..9d254ebaa066892910f2cba4ea505f0f10902851 100644 GIT binary patch delta 359 zcmbO$dQX%$l$V!_0SHd1_@;f@$Q#1Ocwll8n+Nx!8m27v6y|KEqA8QNvZ-n_E#R(U z$>LbZSj$wyvVdnHLk*J%!vfxg4BQO0ta&~_)f_d9Ssd9+#akx-WK%ZBul^WNEn5nE zHq!*gq7^{BY#^I}Y@f;Q?D~dy^wzLRgY1hz*f)W(IB)W7c3o3~x`1}_0qxjR!e7H! z!_>@F!x}Fz`6K&&M#0G&IYK$ui&Bd-5(`o%vvT&idx64)k<*KMOO20ky^Kzi&vHsDi?Hx9^8IIF;$swH=3(Mt;sRktK1K-^0gyN|kUd$L>ofoZ ClR*3c diff --git a/norch/nn/functional.py b/norch/nn/functional.py index 9493591..78b1f53 100644 --- a/norch/nn/functional.py +++ b/norch/nn/functional.py @@ -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) \ No newline at end of file diff --git a/norch/nn/loss.py b/norch/nn/loss.py index 8d7b0f9..b0e7898 100644 --- a/norch/nn/loss.py +++ b/norch/nn/loss.py @@ -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: diff --git a/test.py b/test.py index 7ad7e2d..23c7928 100644 --- a/test.py +++ b/test.py @@ -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]) \ No newline at end of file