ValueError: Target size (torch.Size([2, 200])) must be the same as input size (torch.Size([2, 5]))

import sklearn
from sklearn.datasets import make_circles
import torch
from torch import nn

n_sample = 1000

device = “cpu”

x, y = make_circles(n_sample,
noise=0.03,
random_state=42

                )

x,y

import pandas as pd

circles = pd.DataFrame({“X1”: x[:, 0], “X2”: x[:,1],“label”:y})
circles.head(10)
import matplotlib.pyplot as plt

plt.scatter(x=x[:,0],y=x[:,1],c=y,cmap=plt.cm.RdYlBu)
X_sample = x[0]

y_sample = y[0]

X_sample
X = torch.from_numpy(x).type(torch.float)

y = torch.from_numpy(y).type(torch.float)
from sklearn.model_selection import train_test_split

X_train, y_train, X_test,y_test = train_test_split(X,
y,
test_size=0.2,
random_state=42)

y_train.squeeze(dim=1)
class CircleModelV0(nn.Module):
def init(self):
super().init()
self.layer_1 = nn.Linear(in_features=2, out_features=5)
self.layer_2 = nn.Linear(in_features=5, out_features=2)
def forward(self, x):
return self.layer_2(self.layer_1(X))

model_0 = CircleModelV0()
model_0
with torch.inference_mode():
untrained_pred = model_0(X_train)
untrained_pred
loss_fn = nn.BCEWithLogitsLoss()

optimizer = torch.optim.SGD(params=model_0.parameters(),
lr=0.01
)

loss_fn,optimizer
def accuracy_fn(y_true, y_pred):
correct = torch.eq(y_true, y_pred).sum().item()
acc = (correct/len(y_pred)) * 100
return acc
model_0.eval()

with torch.inference_mode():

y_logits = model_0(X_test.to(device))[:5]

y_logits.shape
y_preds_probs = torch.sigmoid(y_logits)

y_preds_probs

torch.round(y_preds_probs)
y_pred = torch.round(y_preds_probs)

y_pred_label = torch.round(torch.sigmoid(model_0(X_test)))

epochs = 100

for epochs in range(epochs):
model_0.train()
y_logtis = torch.round(X_train).squeeze()
y_pred = torch.round(torch.sigmoid(y_logits))
loss = loss_fn(y_logits,y_train)
optimizer.zero_grad()
loss.backward()
optimizer.step()

The error message points to a shape mismatch between the model output and the target, which are expected to have the same shape for nn.BCEWithLogitsLoss.
Check both tensors and try to isolate why the shape is different.

Your code is unfortunately neither executable nor properly formatted, you also didn’t explain your use case, so would need to try to narrow down the shape mismatch a bit more.