pytorch奇怪错误
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().)
今天在这里遇到了一个奇怪的bug
import numpy as np# 创建一个简单的数组
array = np.array([1, 2, 3, 4, 5])# 使用反向切片创建负步幅数组
reversed_array = array[::-1]print("原始数组:", array)
print("反向切片后的数组:", reversed_array)
print("反向数组的步幅:", reversed_array.strides)
结果如下
import torch
aa = torch.FloatTensor(array)
bb = torch.FloatTensor(reversed_array)
正确用法
import torch
aa = torch.FloatTensor(array)
bb = torch.FloatTensor(reversed_array.copy())
这个结果是