송준석
송준석

Reputation: 1031

index 11513 is out of bounds for axis 0 with size 10000

I'm practicing a simple MNIST example, and I get an error like the title, and I do not know what index 11513 means. Below is the complete code.

np.random.seed(3)

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_val = x_train[50000:]
y_val = y_train[50000:]
x_train = x_train[:50000]
y_train = y_train[:50000]


x_train = x_train.reshape(50000, 784).astype('float32') / 255.0
x_val = x_val.reshape(10000, 784).astype('float32') / 255.0
x_test = x_test.reshape(10000, 784).astype('float32') / 255.0

train_rand_idxs = np.random.choice(50000, 700)
val_rand_idxs = np.random.choice(10000, 300)
x_train = x_train[train_rand_idxs]
y_train = y_train[train_rand_idxs]
x_val = x_val[train_rand_idxs]#***This is where the error occurred***
y_val = y_val[train_rand_idxs]

y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
y_val = np_utils.to_categorical(y_val)

model = Sequential()
model.add(Dense(units=2 , input_dim= 28*28, activation='relu'))
model.add(Dense(units=10 , activation='softmax'))

model.compile(loss='categorical_crossentropy' , optimizer='sgd' , metrics=         ['accuracy'])

hist = model.fit(x_train, y_train, epochs =1000 , batch_size=10 ,     validation_data =(x_val, y_val))

Upvotes: 0

Views: 3958

Answers (1)

andrew_reece
andrew_reece

Reputation: 21264

Your x_val is reshaped with only 10000 rows:

x_val = x_val.reshape(10000, 784).astype('float32') / 255.0

But train_rand_idxs has index values up to 50000:

train_rand_idxs = np.random.choice(50000, 700)

When you attempt to subset x_val with your train indices:

x_val = x_val[train_rand_idxs]

you are getting an error because some of the indices sampled in [0,50000) are larger than the [0,10000) range of x_val indices.

Try sampling x_val with x_val[val_rand_idxs] instead.

Upvotes: 1

Related Questions