Python 类型错误:只有整数标量数组可以转换为标量索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42128830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: only integer scalar arrays can be converted to a scalar index
提问by Suraksha Ajith
I am trying a simple demo code of tensorflow from github link.
I'm currently using python version 3.5.2
我正在尝试来自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.
I ran into this error when I tried board.py in command-line. I have installed all the dependencies that are required for this to run.
我在命令行中尝试 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
回答by Von
you can modify the function:
你可以修改函数:
def _read32(bytestream):
dt = numpy.dtype(numpy.uint32).newbyteorder('>')
return numpy.frombuffer(bytestream.read(4), dtype=dt)
new version:
新版本:
def _read32(bytestream):
dt = numpy.dtype(numpy.uint32).newbyteorder('>')
return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]
add [0]
in the end.
最后添加[0]
。
This appears to be an issue with the latest version of Numpy. A recent change made it an error to treat a single-element array as a scalar for the purposes of indexing.
这似乎是最新版本的 Numpy 的问题。最近的一项更改使得将单元素数组视为用于索引目的的标量是错误的。
回答by bhaskarc
The code link you have provided uses a separate file named input_data.py
to download data from MNIST using the following two lines in board.py
您提供的代码链接使用一个单独的文件命名input_data.py
为使用以下两行从 MNIST 下载数据board.py
import input_data
mnist = input_data.read_data_sets("/tmp/data/",one_hot=True)
Since MNIST data is so frequently used for demonstration purposes, Tensorflow provides a way to automatically download it.
由于 MNIST 数据如此频繁地用于演示目的,Tensorflow 提供了一种自动下载它的方法。
Replace the above two lines in board.py
with the following two lines and the error will disappear.
用board.py
下面两行替换上面两行,错误就会消失。
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
回答by Stephen Rauch
This file is likely corrupt:
此文件可能已损坏:
Z:/downloads/MNIST dataset\train-images-idx3-ubyte.gz
Let's analyze the error you posted.
让我们分析您发布的错误。
This, indicates that code is currently working with the file in question:
这表明代码当前正在处理相关文件:
Extracting Z:/downloads/MNIST dataset\train-images-idx3-ubyte.gz
Traceback
indicates that a stack trace follows:
Traceback
指示堆栈跟踪如下:
Traceback (most recent call last):
This, indicates that you read your data sets from 'Z:/downloads/MNIST dataset'
:
这表明您从'Z:/downloads/MNIST dataset'
以下位置读取数据集:
File "board.py", line 3, in <module>
mnist = input_data.read_data_sets(r'Z:/downloads/MNIST dataset', one_hot=True)
This, indicates that the code is extracting images:
这表明代码正在提取图像:
File "Z:\downloads\tensorflow_demo-master\tensorflow_demo-master\input_data.py", line 150, in read_data_sets
train_images = extract_images(local_file)
This, indicates that the code is expected to read rows * cols * num_images
bytes:
这表明代码应该读取rows * cols * num_images
字节:
File "Z:\downloads\tensorflow_demo-master\tensorflow_demo-master\input_data.py", line 40, in extract_images
buf = bytestream.read(rows * cols * num_images)
This is the line that errors:
这是错误的行:
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
I expect size
is the problematic value and was calculated on the previous line of the stacktrace.
我期望size
是有问题的值,是在堆栈跟踪的前一行计算的。
I can see at least two ways to proceed.
我可以看到至少有两种方法可以继续。
Delete the offending file and see if the problem goes away. This would allow you to verify that the file is somehow corrupt.
Use a debugger to step into the code and then inspect the values used to calculate the offending variable. Use the knowledge gained to proceed from there.
删除有问题的文件,看看问题是否消失。这将允许您验证文件是否以某种方式损坏。
使用调试器进入代码,然后检查用于计算违规变量的值。使用获得的知识从那里开始。