我正在练习一个简单的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))
答案 0 :(得分:0)
您的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]
您收到错误,因为[0,50000)
中采样的某些指数大于[0,10000)
指数的x_val
范围。
尝试使用x_val
代替x_val[val_rand_idxs]
。