Value Error (At least one stride in the given numpy array is negative) Remains After Copying Array

huangapple go评论70阅读模式
英文:

Value Error (At least one stride in the given numpy array is negative) Remains After Copying Array

问题

我使用以下命令对从另一个Python文件导入的NumPy数组进行了排序:

import numpy as np
from test import c_A
cA = c_A.reshape(10000, 1)
c = np.sort(cA, axis=0)[::-1]

然后我遇到了以下错误:

ValueError: 给定的NumPy数组中至少有一个步幅为负数,目前不支持具有负步幅的张量。(您可能可以通过使用array.copy()来解决这个问题,制作数组的副本。)

因此,我复制了数组,并相应调整了我的代码:

cA = c_A.reshape(10000, 1)
cA2 = torch.from_numpy(cA.copy())
c = np.sort(cA2, axis=0)[::-1]

相同的错误仍然存在,并且显示“torch”未定义。
我正在使用Nvidia Modulus,这是一个使用PyTorch的DL框架。

在线上看到的复制数组方法并没有起作用。
我应该怎么做?
数组应该保持为大小为[N,1]的NumPy数组字典。

英文:

I sorted a numpy array that I imported from another python file using the following commands:

import numpy as np
from test import c_A
cA=c_A.reshape(10000,1)
c=np.sort(cA,axis=0)[::-1]

I then get the following error:

> ValueError: At least one stride in the given numpy array is negative, and tensors with negative strides are not currently supported. (You can probably work around this by making a copy of your array with array.copy().)

So, I copied the array, and adjusted my code accordingly:

cA=c_A.reshape(10000,1)
cA2 = torch.from_numpy(cA.copy())
c=np.sort(cA2,axis=0)[::-1]

The same error remains, and it says "torch" is not defined.
I am using Nvidia Modulus, a DL framework that uses PyTorch.

The copied array method that I have been seeing online, which did not work.
How to I go about this?
The array should also remain as a dictionary of numpy array of size [N,1].

答案1

得分: 1

你的错误提示是torch未定义,它未在你的代码中导入,让我们添加它,同时确保在你的系统上已安装它 pip install torch

import torch
import numpy as np
from test import c_A

cA = c_A.reshape(10000, 1)
cA2 = torch.from_numpy(cA.copy())
c, _ = torch.sort(cA2, dim=0, descending=True)
英文:

Your error say that torch is not defined, it has not been imported in your code, lets just add it, also be sure to have have it installed on your system pip install torch

import torch
import numpy as np
from test import c_A

cA = c_A.reshape(10000,1)
cA2 = torch.from_numpy(cA.copy())
c, _ = torch.sort(cA2, dim=0, descending=True)

huangapple
  • 本文由 发表于 2023年6月2日 03:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定