TypeError:只能将整数标量数组转换为标量索引

Sur*_*ith 20 python numpy python-3.x mnist tensorflow

我正在从github链接尝试一个简单的tensorflow演示代码.
我目前正在使用python版本3.5.2

Z:\downloads\tensorflow_demo-master\tensorflow_demo-master>py Python
3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32<br> Type "help", "copyright", "credits" or "license" for more information.
Run Code Online (Sandbox Code Playgroud)

当我在命令行中尝试board.py时遇到了这个错误.我已经安装了运行它所需的所有依赖项.

def _read32(bytestream):
    dt = numpy.dtype(numpy.uint32).newbyteorder('>')
    return numpy.frombuffer(bytestream.read(4), dtype=dt)

def extract_images(filename):
    """Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
    print('Extracting', filename)
    with gzip.open(filename) as bytestream:
        magic = _read32(bytestream)
        if magic != 2051:
            raise ValueError(
                'Invalid magic number %d in MNIST image file: %s' %
                (magic, filename))
        num_images = _read32(bytestream)
        rows = _read32(bytestream)
        cols = _read32(bytestream)
        buf = bytestream.read(rows * cols * num_images)
        data = numpy.frombuffer(buf, dtype=numpy.uint8)
        data = data.reshape(num_images, rows, cols, 1)
    return data

Z:\downloads\tensorflow_demo-master\tensorflow_demo-master>py board.py
Extracting  Z:/downloads/MNIST dataset\train-images-idx3-ubyte.gz
Traceback (most recent call last):  
File "board.py", line 3, in <module>
    mnist = input_data.read_data_sets(r'Z:/downloads/MNIST dataset', one_hot=True)  
File "Z:\downloads\tensorflow_demo-master\tensorflow_demo-master\input_data.py", line 150, in read_data_sets
    train_images = extract_images(local_file) 
File "Z:\downloads\tensorflow_demo-master\tensorflow_demo-master\input_data.py", line 40, in extract_images
    buf = bytestream.read(rows * cols * num_images) 
File "C:\Users\surak\AppData\Local\Programs\Python\Python35\lib\gzip.py", line 274, in read
    return self._buffer.read(size)
TypeError: only integer scalar arrays can be converted to a scalar index
Run Code Online (Sandbox Code Playgroud)

小智 35

你可以修改功能:

def _read32(bytestream):
    dt = numpy.dtype(numpy.uint32).newbyteorder('>')
    return numpy.frombuffer(bytestream.read(4), dtype=dt)
Run Code Online (Sandbox Code Playgroud)

新版本:

def _read32(bytestream):
    dt = numpy.dtype(numpy.uint32).newbyteorder('>')
    return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]
Run Code Online (Sandbox Code Playgroud)

添加[0]到底.

这似乎与Numpy的最新版本有关.最近的更改使得将单元素数组视为用于索引的标量的错误.


bha*_*arc 5

您提供的代码链接使用一个单独的文件命名input_data.py,该文件使用以下两行代码从MNIST下载数据:board.py

import input_data 
mnist = input_data.read_data_sets("/tmp/data/",one_hot=True)
Run Code Online (Sandbox Code Playgroud)

由于MNIST数据经常用于演示目的,因此Tensorflow提供了一种自动下载数据的方法。

将以上两行替换为board.py以下两行,该错误将消失。

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
Run Code Online (Sandbox Code Playgroud)