Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chap4 (CNN_working_details.ipynb) -- Squeeze on model output #73

Open
Camaltra opened this issue Aug 31, 2023 · 1 comment
Open

Chap4 (CNN_working_details.ipynb) -- Squeeze on model output #73

Camaltra opened this issue Aug 31, 2023 · 1 comment

Comments

@Camaltra
Copy link

On the cell to train the model

def train_batch(x, y, model, opt, loss_fn):
    model.train()
    prediction = model(x)
    batch_loss = loss_fn(prediction, y)
    batch_loss.backward()
    optimizer.step()
    optimizer.zero_grad()
    return batch_loss.item()

Prediction have the outcome of [1, 1] while we give the y_train

y_train = torch.tensor([0, 1]) 

We got a miss matching size and the code doesn't run

Error Trigger

Using a target size (torch.Size([1])) that is different to the input size (torch.Size([1, 1])) is deprecated. Please ensure they have the same size.

To fix it, either

y_train = torch.tensor([0, 1]).to(device).float().unsqueeze(1)
def train_batch(x, y, model, opt, loss_fn):
    model.train()
    prediction = model(x)
    batch_loss = loss_fn(prediction.squeeze(0), y)
    batch_loss.backward()
    optimizer.step()
    optimizer.zero_grad()
    return batch_loss.item()
@malatang20001210
Copy link

COOL!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants