Getting this error Expected input batch_size (256) to match target batch_size (0)

So i have a program, that i want to train not from datasets, but from user’s inputs. I use Tkinter to draw numbers and then send this data to model, the gui program outputs 2d array, that i convert to tensor and unsqueeze. How to solve this problem?

import torch
import numpy as np


class Method(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.Sequential(
            nn.Conv2d(1, 64, 1),
            nn.ReLU(),
            nn.Conv2d(64, 128, 1),
            nn.ReLU(),
            nn.Conv2d(128, 256, 1),
            nn.ReLU(),
            nn.Flatten(),
            nn.Linear(1024, 10)
        )

    def forward(self, x):
        return self.model(x)


device = torch.device("cpu")
clf = Method().to(device)
opt = torch.optim.Adam(clf.parameters(), lr=1e-3)
loss_fn = nn.CrossEntropyLoss()

def train(total):
    data,label=total
    X=torch.tensor(data,dtype=torch.float32)
    y=torch.tensor(np.float32(label),dtype=torch.float32)
    X.unsqueeze_(0)
    print(X,y)
    print(X.shape,y.shape)
    X, y = X.to(device), y.to(device)
    yhat = clf(X)
    loss = loss_fn(yhat, y)

    # Apply backprop
    opt.zero_grad()
    loss.backward()
    opt.step()
    print(f"loss is {loss.item()}")
    with open('model_state2.pt', 'wb') as f:
        save(clf.state_dict(), f)```

here's the stacktrace 
```Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\akurt\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\akurt\PycharmProjects\MethodDraw\GUI.py", line 49, in <lambda>
    submit_button=tk.Button(app,text="Обучить",command=lambda :gtpd())
                                                               ^^^^^^
  File "C:\Users\akurt\PycharmProjects\MethodDraw\GUI.py", line 36, in gtpd
    Model.train(total)
  File "C:\Users\akurt\PycharmProjects\MethodDraw\Model.py", line 38, in train
    loss = loss_fn(yhat, y)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\akurt\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\akurt\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\nn\modules\loss.py", line 1174, in forward
    return F.cross_entropy(input, target, weight=self.weight,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\akurt\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\nn\functional.py", line 3029, in cross_entropy
    return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Expected input batch_size (256) to match target batch_size (0).```

Based on the error message it seems you are passing an empty target:

criterion = nn.CrossEntropyLoss()
x = torch.randn(256, 10)
y = torch.randint(0, 10, (0,))

loss = criterion(x, y)
# ValueError: Expected input batch_size (256) to match target batch_size (0).

or a scalar:

y = torch.tensor(0)
loss = criterion(x, y)
# ValueError: Expected input batch_size (256) to match target batch_size (0).

while the model outputs logits for 256 samples.

Unsqueezing the batch dimension in the target won’t help, since you would run into another shape mismatch:

y = y.unsqueeze(0)
print(y.shape)
# torch.Size([1])
loss = criterion(x ,y)
# ValueError: Expected input batch_size (256) to match target batch_size (1).

so check why the target seems to contain a single value while 256 samples were used.