其他分享
首页 > 其他分享> > TypeError: only integer scalar arrays can be converted to a scalar index

TypeError: only integer scalar arrays can be converted to a scalar index

作者:互联网

今天从github看到大神把吴恩达讲sparse autoencoder的MATLAB代码改写到python上,链接如下:https://github.com/jatinshah/ufldl_tutorial
遂直接copy,但是程序好像用的是python2.7写的,改了不少print语句,在运行的时候遇到些错误:

错误位置在 load_MNIST.py 中的 images = images.reshape(num_images, num_rows * num_cols) 。我琢磨这错误是说索引的type不对,后来发现前面代码中给索引赋值是用的numpy

        num_images = np.fromfile(f, dtype=np.dtype('>i4'), count=1)
        num_rows = np.fromfile(f, dtype=np.dtype('>i4'), count=1)
        num_cols = np.fromfile(f, dtype=np.dtype('>i4'), count=1)
      

把索引强制改成int,程序没报错…这个大佬写的程序里面有很多这样的类型问题…

        num_images = int( np.fromfile(f, dtype=np.dtype('>i4'), count=1))
        num_rows = int(np.fromfile(f, dtype=np.dtype('>i4'), count=1))
        num_cols = int(np.fromfile(f, dtype=np.dtype('>i4'), count=1))

这个错误出现的地方是在load数据的时候,主要问题是Windows打开文件是需要文件类型的,显然大佬的程序里是没注意到咱这样的小菜鸟是在win的环境下使用的,所以改一下load的文件名就ok,把 横线 改成

images = load_MNIST.load_MNIST_images('data/mnist/train-images.idx3-ubyte')

标签:index,TypeError,dtype,i4,images,scalar,num,np,fromfile
来源: https://blog.csdn.net/sinat_24259567/article/details/98208281