Python 索引11513超出大小为10000的轴0的界限

Python 索引11513超出大小为10000的轴0的界限,python,matplotlib,machine-learning,mnist,Python,Matplotlib,Machine Learning,Mnist,我正在练习一个简单的MNIST示例,我得到了一个类似标题的错误,我不知道索引11513是什么意思。 下面是完整的代码 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_

我正在练习一个简单的MNIST示例,我得到了一个类似标题的错误,我不知道索引11513是什么意思。 下面是完整的代码

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))

您的
x_val
仅用10000行重新格式化:

x_val = x_val.reshape(10000, 784).astype('float32') / 255.0
但是
train_rand_idxs
的索引值高达50000:

train_rand_idxs = np.random.choice(50000, 700)
当您试图用列车索引将
x_val
子集时:

x_val = x_val[train_rand_idxs]
出现错误是因为在
[050000)
中采样的某些索引大于
[010000)
索引的
范围


尝试使用
x_val[val\u rand\u idxs]采样
x_val
取而代之。

索引11513是列表中的11514个元素。根据错误,您的列表只有10000个元素。哈哈……我只是把火车误认为是验证。谢谢。很高兴我能提供帮助。如果此答案解决了您的问题,请单击答案左侧的复选标记将其标记为接受。