Why can't I convert to LongTensor

import torch
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split

设置类别的个数

NUM_CLASSES = 4
NUM_FRATURES = 2
RANDEM_SEED = 42

创建数据集

X_blob,y_bolb = make_blobs(n_samples=1000,
n_features=NUM_FRATURES,
centers=NUM_CLASSES,
cluster_std=1.5,
random_state=RANDEM_SEED,)

sklearn的数据集使用的是numpy,所以要转为Tensors。

X_blob = torch.from_numpy(X_blob).type(torch.float)
y_bolb = torch.from_numpy(y_bolb).type(torch.LongTensor)
print(y_bolb.dtype)

切割训练和测试训练集

X_blob_train,X_blob_test,y_blob_train,y_blob_test = train_test_split(X_blob,y_bolb,test_size=0.2,random_state=RANDEM_SEED)

可视化数据集

plt.figure(figsize=(12,10))
plt.scatter(X_blob[:,0],X_blob[:,1],c=y_bolb,cmap=plt.cm.RdYlBu)

--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) Cell In[143], line 22 19 y_pred = torch.softmax(y_logits,dim=1).argmax(dim=1) 21 # 计算损失(交叉熵)(X为训练,y为测试) —> 22 loss = loss_fn(y_pred,y_blob_test) 23 # 准确性 24 acc = acccracy_fn(y_true=y_blob_train,y_pred=y_pred) File [d:\python_3.11\Lib\site-packages\torch\nn\modules\module.py:1501](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/module.py:1501), in Module._call_impl(self, *args, **kwargs) [1496](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/module.py:1496) # If we don’t have any hooks, we want to skip the rest of the logic in [1497](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/module.py:1497) # this function, and just call forward. [1498](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/module.py:1498) if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks [1499](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/module.py:1499) or _global_backward_pre_hooks or _global_backward_hooks [1500](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/module.py:1500) or _global_forward_hooks or _global_forward_pre_hooks): → [1501](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/module.py:1501) return forward_call(*args, **kwargs) [1502](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/module.py:1502) # Do not call functions when jit is used [1503](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/module.py:1503) full_backward_hooks, non_full_backward_hooks = , File [d:\python_3.11\Lib\site-packages\torch\nn\modules\loss.py:1174](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/loss.py:1174), in CrossEntropyLoss.forward(self, input, target) [1173](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/loss.py:1173) def forward(self, input: Tensor, target: Tensor) → Tensor: → [1174](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/loss.py:1174) return F.cross_entropy(input, target, weight=self.weight, [1175](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/loss.py:1175) ignore_index=self.ignore_index, reduction=self.reduction, [1176](file:///D:/python_3.11/Lib/site-packages/torch/nn/modules/loss.py:1176) label_smoothing=self.label_smoothing)

[3027](file:///D:/python_3.11/Lib/site-packages/torch/nn/functional.py:3027) if size_average is not None or reduce is not None: [3028](file:///D:/python_3.11/Lib/site-packages/torch/nn/functional.py:3028) reduction = _Reduction.legacy_get_string(size_average, reduce) → [3029](file:///D:/python_3.11/Lib/site-packages/torch/nn/functional.py:3029) return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing) RuntimeError: “log_softmax_lastdim_kernel_impl” not implemented for ‘Long’

Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings

nn.CrossEntropyLoss expects floating point inputs for the logits and does accept integer types for the targets only.