Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying a simple demo code of tensorflow from github link.
I'm currently using python version 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.

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
like image 303
Suraksha Ajith Avatar asked Feb 09 '17 05:02

Suraksha Ajith


People also ask

How do I fix TypeError only integer scalar arrays can be converted to a scalar index?

It usually can concatenate row-wise and column-wise. By default NumPy's concatenate function concatenate row-wise, to do so it requires iterable (tuple or list) to concatenate. Solution: To solve this error you need to convert array 1 and array 2 in to tuple or list.

What does only integer scalar arrays can be converted to a scalar index mean?

Solution. The error you have faced is this: TypeError: only integer scalar arrays can be converted to a scalar index. Python. This means that the index you are using to refer to an array element is wrong.

How do you fix TypeError only size 1 arrays can be converted to Python scalars?

TypeError occurs when you pass an array instead of passing a single value in the function or when you are working on NumPy and matplotlib. pyplot. To fix this, add the code and it returns vector results.

What is a scalar array?

A scalar array is a fixed-length group of consecutive memory locations that each store a value of the same type. You access scalar arrays by referring to each location with an integer starting from zero. In D programs, you would usually use scalar arrays to access array data within the operating system.


2 Answers

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.

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.

like image 77
Von Avatar answered Oct 06 '22 22:10

Von


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

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.

Replace the above two lines in board.py with the following two lines and the error will disappear.

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
like image 5
bhaskarc Avatar answered Oct 06 '22 23:10

bhaskarc