ValueError: At least one stride in the given numpy array is negative解决方案

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().)

我的出错代码片段(来自于本人正在攻关的科研问题):

                else:
                    path = os.path.join(self.results_dir,'NODALresultsummaryarrayRF1_32x32_3.npy')
                    boundary_F = output_loader(path)
                    boundary_F = boundary_F[:,:,0:2]
                    boundary_F = np.flip(boundary_F,axis=0)
                    # boundary_F = boundary_F.copy()
                    # print(boundary_F.shape)
                    boundary_F = torch.tensor(boundary_F,dtype=inputtorch_type)

出错是因为np.filp操作。当然,本质上这算个bug(网友提示:内存不连续导致),torch.tensor无法对flip后的numpy加载(截至20221118)
那么,需要拷贝操作才能解决这个问题:

                else:
                    path = os.path.join(self.results_dir,'NODALresultsummaryarrayRF1_32x32_3.npy')
                    boundary_F = output_loader(path)
                    boundary_F = boundary_F[:,:,0:2]
                    boundary_F = np.flip(boundary_F,axis=0)
                    boundary_F = boundary_F.copy()
                    # print(boundary_F.shape)
                    boundary_F = torch.tensor(boundary_F,dtype=inputtorch_type)

这个代码就不再报错了。

你可能感兴趣的:(python)