It would be nice to see your train_generator code for clarity, but it does not seem to be a torch DataLoader. In this case, you should probably convert your arrays to tensors manually. There are several ways to do so:

  • torch.from_numpy(numpy_array) - for numpy arrays;
  • torch.as_tensor(list) - for common lists and tuples;
  • torch.tensor(array) should also work but the above ways will avoid copying the data when possible.
Answer from dx2-66 on Stack Overflow
🌐
PyTorch Forums
discuss.pytorch.org › t › attributeerror-numpy-ndarray-object-has-no-attribute-log › 117472
AttributeError: 'numpy.ndarray' object has no attribute 'log' - PyTorch Forums
April 8, 2021 - It seems you are trying to pass a numpy array to F.nll_loss, while a PyTorch tensor is expected. I’m not sure how y_pred is calculated, but note that using numpy array would detach them from the computation graph, so you should stick to PyTorch tensors and operations, if possible · Remove ...
🌐
PyTorch Forums
discuss.pytorch.org › t › attributeerror-numpy-ndarray-object-has-no-attribute-dim › 16026
AttributeError: 'numpy.ndarray' object has no attribute 'dim' - PyTorch Forums
April 5, 2018 - Hello Team, i am new to the forum and to Pytorch, i want to predict 3 real values Y1, Y2 & Y3 from input values X1, X2…X10 using the below model, but i get the error in title: File “/home/abdelmoula/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py”, line 833, in linear if input.dim() == 2 and bias is not None: AttributeError: ‘numpy.ndarray’ object has no attribute ‘dim’ Can you please on what is wrong ? thank you ######################## MODEL: import pandas as pd import to...
🌐
GitHub
github.com › pytorch › pytorch › issues › 41657
AttributeError: 'numpy.ndarray' object has no attribute 'dim' · Issue #41657 · pytorch/pytorch
July 19, 2020 - return torch.argmax(self.dqn(state)) File "/home/veds12/anaconda3/envs/AI/lib/python3.6/site-packages/torch/nn/modules/module.py", line 550, in __call__ result = self.forward(*input, **kwargs) File "/home/veds12/anaconda3/envs/AI/lib/python3.6/site-packages/torch/nn/modules/container.py", line 100, in forward input = module(input) File "/home/veds12/anaconda3/envs/AI/lib/python3.6/site-packages/torch/nn/modules/module.py", line 550, in __call__ result = self.forward(*input, **kwargs) File "/home/veds12/anaconda3/envs/AI/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 87, in forward return F.linear(input, self.weight, self.bias) File "/home/veds12/anaconda3/envs/AI/lib/python3.6/site-packages/torch/nn/functional.py", line 1608, in linear if input.dim() == 2 and bias is not None: AttributeError: 'numpy.ndarray' object has no attribute 'dim'
Author   veds12
🌐
GitHub
github.com › pytorch › vision › issues › 7414
Load MNIST from torchvision AttributeError: 'numpy.ndarray' object has no attribute 'numpy' · Issue #7414 · pytorch/vision
March 11, 2023 - 🐛 Describe the bug I can not load MNIST from torchvision. AttributeError: 'numpy.ndarray' object has no attribute 'numpy' def build_transforms_mnist(normalize=False): if normalize: ...
Author   rikonaka
🌐
PyTorch Forums
discuss.pytorch.org › t › how-to-fix-attributeerror-numpy-ndarray-object-has-no-attribute-cpu › 158960
How to fix: "AttributeError: 'numpy.ndarray' object has no attribute 'cpu'" - PyTorch Forums
August 11, 2022 - Hi All I have data from a dataloader which I get a data and corresponding label from it. I am trying to normalize the labels within a certain range using T.tonumpy_denormalize , however I get this error AttributeError: 'numpy.ndarray' object has no attribute 'cpu'.
Find elsewhere
🌐
GitHub
github.com › open-edge-platform › anomalib › issues › 2452
[Bug]: AttributeError: 'numpy.ndarray' object has no attribute 'to' · Issue #2452 · open-edge-platform/anomalib
December 5, 2024 - PyTorch version: [e.g. 1.9.0] CUDA/cuDNN version: [e.g. 11.1] GPU models and configuration: [e.g. 2x GeForce RTX 3090] Any other relevant information: [e.g. I'm using a custom dataset] ... def pre_process(self, image: np.ndarray) -> torch.Tensor: """Pre process the input image.
Author   Septembit
🌐
Stack Overflow
stackoverflow.com › questions › 74551169 › getting-attributeerror-numpy-ndarray-object-has-no-attribute-dim-when-conve
python - getting AttributeError: 'numpy.ndarray' object has no attribute 'dim' when converting tensorflow code to pytorch - Stack Overflow
Python Neural Network: 'numpy.ndarray' object has no attribute 'dim' ... torch.stft is a PyTorch function and expects a Tensor as the input. You must convert your NumPy array into a tensor and then pass that as the input.
🌐
PyTorch Forums
discuss.pytorch.org › t › attributeerror-numpy-ndarray-object-has-no-attribute-mode › 127130
AttributeError: 'numpy.ndarray' object has no attribute 'mode' - PyTorch Forums
July 19, 2021 - I guess your code expects img_group to be a list (or another container) containing PIL.Images, which provide the mode attribute · I don’t think you would necessarily need to transform the inputs first to PIL.Images, as the method seems to return numpy arrays.
🌐
PyTorch Forums
discuss.pytorch.org › t › attributeerror-numpy-ndarray-object-has-no-attribute-cpu › 79512
AttributeError: 'numpy.ndarray' object has no attribute 'cpu' - PyTorch Forums
May 3, 2020 - Hi, I am having a problem in plotting the accuracy of trained model, I adopted the following code for plotting the accuracy from tutorial Finetuning Torchvision Models — PyTorch Tutorials 2.2.0+cu121 documentation. Here is the code, ohist = [] shist = [] ohist = [h.cpu().numpy() for h in epoch_accuracy_o] shist = [h.cpu().numpy() for h in epoch_accuracy_s] print(ohist) print(shist) plt.title("Validation Accuracy vs.
🌐
PyTorch Forums
discuss.pytorch.org › vision
Creating tensor TypeError: can't convert np.ndarray of type numpy.object_ - vision - PyTorch Forums
July 4, 2021 - X = final_df.values y = df['Target'].values from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.4, random_state=42) train_inputs = torch.tensor(X_train,dtype=torch.float).tag("#iot", "#network","#data","#train") train_labels ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-fix-cant-assign-a-numpyndarray-to-a-torchfloattensor
How to Fix "Can't Assign a numpy.ndarray to a torch.FloatTensor"? - GeeksforGeeks
July 23, 2025 - The error "Can't assign a numpy.ndarray to a torch.FloatTensor" occurs when you attempt to assign a NumPy array to a PyTorch tensor directly, which is not allowed. PyTorch and NumPy are different libraries with different data structures, so ...
🌐
PyTorch Forums
discuss.pytorch.org › reinforcement-learning
AttributeError: 'numpy.ndarray' object has no attribute 'dim' from torch/nn/functional.py - reinforcement-learning - PyTorch Forums
January 10, 2020 - I’m working on a DDPG implementation and getting AttributeError: 'numpy.ndarray' object has no attribute 'dim' from my Actor class. Based on answer here https://discuss.pytorch.org/t/attributeerror-numpy-ndarray-object-has-no-attribute-dim/16026/2, I tried using Variable(), however, the args are Tensors already and I get the TypeError: expected np.ndarray (got Tensor) error.