gpt4 book ai didi

python - 类型错误 : only integer scalar arrays can be converted to a scalar index

转载 作者:IT老高 更新时间:2023-10-28 20:51:00 27 4
gpt4 key购买 nike

我正在尝试 github link 中的 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.

我在命令行中尝试 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

最佳答案

你可以修改函数:

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 的问题。最近的一项更改使得将单元素数组视为标量以进行索引是错误的。

关于python - 类型错误 : only integer scalar arrays can be converted to a scalar index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42128830/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com