我正在尝试从github link开始的一个简单的张量流演示代码
我目前正在使用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.
我在命令行中尝试使用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
答案 0 :(得分:35)
你可以修改功能:
def _read32(bytestream):
dt = numpy.dtype(numpy.uint32).newbyteorder('>')
return numpy.frombuffer(bytestream.read(4), dtype=dt)
新版本:
def _read32(bytestream):
dt = numpy.dtype(numpy.uint32).newbyteorder('>')
return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]
最后添加[0]
。
这似乎与Numpy的最新版本有关。最近的更改使得将单元素数组视为用于索引的标量的错误。
答案 1 :(得分:5)
您提供的代码链接使用名为input_data.py
的单独文件,使用board.py
import input_data
mnist = input_data.read_data_sets("/tmp/data/",one_hot=True)
由于MNIST数据经常用于演示目的,因此Tensorflow提供了一种自动下载的方法。
使用以下两行替换board.py
中的上述两行,错误将消失。
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
答案 2 :(得分:2)
此文件可能已损坏:
Z:/downloads/MNIST dataset\train-images-idx3-ubyte.gz
让我们分析您发布的错误。
这表示代码当前正在处理相关文件:
Extracting Z:/downloads/MNIST dataset\train-images-idx3-ubyte.gz
Traceback
表示堆栈跟踪如下:
Traceback (most recent call last):
这表示您从'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)
这表示代码正在提取图像:
File "Z:\downloads\tensorflow_demo-master\tensorflow_demo-master\input_data.py", line 150, in read_data_sets
train_images = extract_images(local_file)
这表示代码应该读取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)
这是错误的行:
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
我希望size
是有问题的值,并且是在堆栈跟踪的前一行计算的。
我至少可以看到两种方法。
删除有问题的文件,看看问题是否消失。这将允许您验证文件是否以某种方式损坏。
使用调试器进入代码,然后检查用于计算有问题变量的值。利用所获得的知识从那里开始。